重构25-Introduce Design By Contract checks(契约式设计)
public class CashRegister {
public Double TotalOrder(List<Product> products, Customer customer) {
Double orderTotal =sum(products);
customer.Balance += orderTotal;
return orderTotal;
}
Double sum(List<Product> products){
Double sum=0d;
for(Product p:List<Product>){
sum+=p.Price;
}
return sum;
}
}
public class CashRegister {
public Double TotalOrder(List<Product> products, Customer customer) {
if (customer == null)
throw new IllegalArgumentException("customer Customer cannot be null");
if (products.size() == 0)
throw new IllegalArgumentException("Must have at least one product to total products");
Double orderTotal = sum(products);
customer.Balance += orderTotal;
if (orderTotal == 0)
throw new ArrayIndexOutOfBoundsException("orderTotal Order Total should not be zero");
return orderTotal;
}
Double sum(List<Product> products){
Double sum=0d;
for(Product p:List<Product>){
sum+=p.Price;
}
return sum;
}
}
重构25-Introduce Design By Contract checks(契约式设计)的更多相关文章
- 重构第25天 引入契约设计(Introduce Design By Contract checks)
理解:本文中的”引入契约式设计”是指我们应该对应该对输入和输出进行验证,以确保系统不会出现我们所想象不到的异常和得不到我们想要的结果. 详解:契约式设计规定方法应该对输入和输出进行验证,这样你便可以保 ...
- JML契约式设计——第三单元学习小结
一.前言 本单元作业都是关于JML(Java Modeling Language),JML是一种契约式设计(Design by Contract)的语言,契约式设计的主要目的是希望程序员能够在设计程序 ...
- 契约式设计(DbC)感想(二)
契约式设计6大原则的理解 在<Design by Contract原则与实践>中,作者定义了契约式设计的6大原则: 区分命令和查询: 将基本查询和派生查询区分开: 针对每个派生查询,设定一 ...
- 契约式设计(DbC)感想(一)
契约式设计可以理解为正则编程的一种实践: 如果用我的三脚猫能力将这种实践方法形式化的话,大致如下(如有不正确处,请不吝指正): 1.对于方法Method的precondition & post ...
- 契约式设计 契约式编程 Design by contract
Design by contract - Wikipedia https://en.wikipedia.org/wiki/Design_by_contract What is the use of & ...
- 《Design by Contract for Embedded Software》 翻译
原文: Design by Contract for Embedded Software (state-machine.com) Design by Contract is the single mo ...
- PHP 面向对象编程和设计模式 (1/5) - 抽象类、对象接口、instanceof 和契约式编程
PHP高级程序设计 学习笔记 2014.06.09 什么是面向对象编程 面向对象编程(Object Oriented Programming,OOP)是一种计算机编程架构.OOP 的一条基本原则是计算 ...
- [译文]Domain Driven Design Reference(三)—— 模型驱动设计的构建模块
本书是Eric Evans对他自己写的<领域驱动设计-软件核心复杂性应对之道>的一本字典式的参考书,可用于快速查找<领域驱动设计>中的诸多概念及其简明解释. 其它本系列其它文章 ...
- [译文]Domain Driven Design Reference(四)—— 柔性设计
本书是Eric Evans对他自己写的<领域驱动设计-软件核心复杂性应对之道>的一本字典式的参考书,可用于快速查找<领域驱动设计>中的诸多概念及其简明解释. 其它本系列其它文章 ...
随机推荐
- does not support ASP.NET compatibility 错误
The service cannot be activated because it does not support ASP.NET compatibility. ASP.NET compatibi ...
- tableviewcell 系统工具删除:左滑删除,选中多个删除
在编辑tableview的时候,我们有时候会需要编辑对一些cell进行删除(增加),在这个时候,我们最好用系统的方法来进行增删的操作 // // text1Controller.m // text / ...
- SNMP MIB中的含read-create节点的表的实现
做过snmp/mib开发的知道,常见的节点类型一般只有no-accessible,read-only,read-write三种访问类型.snmp V2中引入了一种新的访问类型:read-create. ...
- urlrewritingnet 域名http状态302 问题(转)
UrlRewritingNet is an Url rewriting tool for ASP .Net and Elmahis a module for logging unhandled err ...
- Backbone
app.js作为backbone 业务代码主模块,内容很简单,在页面加载完之后,对AppView进行了实例化
- NSNotificationCenter需要注意的几个问题
NSNotificationCenter是iOS中常用的消息通知机制,不过在使用过程中有几点需要注意的问题. 直接贴Apple 的官方文档吧: A notification center delive ...
- C#高性能大容量SOCKET并发(一):IOCP完成端口例子介绍(转)
原文地址 http://blog.csdn.net/SQLDebug_Fan/article/details/17556353 例子主要包括SocketAsyncEventArgs通讯封装.服务端实现 ...
- EWARM STM32 向量表偏移设置
system_stm32f2xx.c #ifdef VECT_TAB_SRAM SCB->VTOR = SRAM_BASE | VECT_TAB_OFFSET; /* Vector Table ...
- Hex-Rays Decompiler Tips and tricks Volatile memory
https://www.hex-rays.com/products/decompiler/manual/tricks.shtml First of all, read the troubleshoot ...
- 729 - The Hamming Distance Problem
// 题意: // 输入两个整数N, H,按照字典序输出所有长度为N,恰好包含H个1的01串 // 规模:1<=H<=N<=16 // 算法A:2^N枚举,输出1的个数为H的.采 ...