重构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对他自己写的<领域驱动设计-软件核心复杂性应对之道>的一本字典式的参考书,可用于快速查找<领域驱动设计>中的诸多概念及其简明解释. 其它本系列其它文章 ...
随机推荐
- MSSQL手札三 MSSQL存储过程
--存储过程完成一段sql代码的封装 create proc trim --参数列表,多个间用逗号分隔 ) as --自定义代码段 ) set @str1=LTRIM(RTRIM(@str)) pri ...
- HDU 4539郑厂长系列故事――排兵布阵(状压DP)
HDU 4539 郑厂长系列故事――排兵布阵 基础的状压DP,首先记录先每一行可取的所哟状态(一行里互不冲突的大概160个状态), 直接套了一个4重循环居然没超时我就呵呵了 //#pragma co ...
- POJ3321 Apple Tree (树状数组)
Apple Tree Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 16180 Accepted: 4836 Descr ...
- HDU 4493 Tutor (控制精度)
题意:给定12个数,求平均数. 析:这个题就是精度控制问题,如果控制精度,最好的办法就是用整型了. 代码如下: #include <cstdio> #include <string& ...
- servlet-3_0-final-spec
<?xml version="1.0" encoding="UTF-8"?> <xsd:schema xmlns="http://w ...
- https,https的本地测试环境搭建,asp.net结合https的代码实现,http网站转换成https网站之后遇到的问题
一:什么是https SSL(Security Socket Layer)全称是加密套接字协议层,它位于HTTP协议层和TCP协议层之间,用于建立用户与服务器之间的加密通信,确保所传递信息的安 ...
- My集合框架第一弹 LinkedList篇
package com.wpr.collection; import java.util.ConcurrentModificationException; import java.util.Itera ...
- JQery icheck 插件
<script type="text/javascript"> $(document).ready(function(){ var callbacks_list = $ ...
- JQuery EasyUI弹出对话框解决Asp.net服务器控件无法执行后台代码的方法(转)
原文:JQuery EasyUI弹出对话框解决Asp.net服务器控件无法执行后台代码的方法 jquery-easyui是一个基于jquery的图形界面插件,利用easyui可以创建很多好看的网页界面 ...
- 用Log4Net记录NHibernate中执行的SQL语句及执行时间
首页,在web.config中加入以下配置: <configuration> <configSections> <section name="log4net&q ...