使用state pattern替代if else(5)

2008-05-24 05:36:05.0     浏览:421     来源:chinaitlab
关键词:  ER  

写一 junit 测试类运行 class Customer consume() 方法。


package com.qujingbo.movie;

import junit.framework.TestCase;

/**
*

Title:junit测试类
*


*

Description:
*


*

Date:2006-10-14 19:32:57
*


*
*
@author EOMS 曲静波
*
@version 1.0
*/

public class CustomerTest extends TestCase {

private Customer customer = null ;

protected void setUp() throws Exception {
super .setUp();
customer
= new Customer();
}


protected void tearDown() throws Exception {
super .tearDown();
}


/*
* Test method for ''com.qujingbo.movie.Customer.consume()''
*/

public void testConsume() {
try {
customer.consume();
}
catch (MovieException e) {
System.out.println(
" 没有该类影片 " );
}

}


}


OK 。结果为:

普通影片租赁 10 天的价格 14.0

最新影片租赁 10 天的价格 30.0

儿童影片租赁 10 天的价格 12.0

普通影片租赁 10 天的积分 20.0

最新影片租赁 10 天的积分 30.0

儿童影片租赁 10 天的积分 15.0

最后我要说,我们用 OO 表示的租赁过程并不完整,因为顾客不一定只租赁一部影片,而要租赁多部影片,这样我们缺少一个 Rental (租赁类)。而只是为说明 state pattern 替代 if else ,所以我们没有添加 Rental (租赁类),若需要参考,请查阅《重构》第一章。 点击下载源码.

这是我第一次写技术文章,如果有适当的地方,请各位朋友提出各自见解。

email:qujingbo@gmail.com