|
/** 作者:Willpower 来源:Rifoo Technology(http://www.rifoo.com) 时间:2005-12-25 备注:转载请保留以上声明 **/
这本书是一个以代码和实战为主的书,全书在构建一个过山车订购系统,体育商店可以用来对它们的过山车进行管理和订购。第一节作者先硬编码了两个有依赖关系的类CommandLineView.java和RentABike.java。我们先看看源代码:
Example 1-1. Bike.java
public class Bike { private String manufacturer; private String model; private int frame; private String serialNo; private double weight; private String status;
public Bike(String manufacturer, String model, int frame, String serialNo, double weight, String status) {
this.manufacturer = manufacturer; this.model = model; this.frame = frame; this.serialNo = serialNo; this.weight = weight; this.status = status; }
public String toString( ) { return "Bike : " + "manufacturer -- " + manufacturer + "\n: model -- " + model + "\n: frame -- " + frame + "\n: serialNo -- " + serialNo + "\n: weight -- " + weight + "\n: status -- " + status + ".\n"; } public String getManufacturer( ) { return manufacturer; }
public void setManufacturer(String manufacturer) { this.manufacturer = manufacturer; }
public String getModel( ) { return model; }
public void setModel(String model) { this.model = model; }
public int getFrame( ) { return frame; }
public void setFrame(int frame) { this.frame = frame; }
public String getSerialNo( ) { return serialNo; }
public void setSerialNo(String serialNo) { this.serialNo = serialNo; }
public double getWeight( ) { return weight; }
public void setWeight(double weight) { this.weight = weight; }
public String getStatus( ) { return status; }
public void setStatus(String status) { this.status = status; } }
可以看出,Bike.java是一个普通的JAVABEAN,用来表述一个过山车实体,包括制造商,型号,规格,序列号,重量和状态等树型。
Example 1-2. RentABike.java
import java.util.*; public class RentABike {
private String storeName; final List bikes = new ArrayList( );
public RentABike(String storeName) { this.storeName = storeName;//商店名 //添加过山车到数组 bikes.add(new Bike("Shimano", "Roadmaster", 20, "11111", 15, "Fair")); bikes.add(new Bike("Cannondale", "F2000 XTR", 18, "22222",12, "Excellent")); bikes.add(new Bike("Trek","6000", 19, "33333", 12.4, "Fair")); }
public String toString( ) { return "RentABike: " + storeName; } //得到全部过山车 public List getBikes( ) { return bikes; } //得到某一个序列号的过山车 public Bike getBike(String serialNo) { Iterator iter = bikes.iterator( ); while(iter.hasNext( )) { Bike bike = (Bike)iter.next( ); if(serialNo.equals(bike.getSerialNo( ))) return bike; } return null; } }
这个类描叙了租用一台过山车的操作,storeName是传入的商店名称。它对客户端来说是一个门面,把所租用的过山车都放在一个List数组中存放起来,然后对外提供getBikes和getBike两个方法,可以让客户端知道目前所租用的所有过山车和某一个序列号的过山车是什么。
我们再看看用户接口是如何调用的: Example 1-3. CommandLineView.java
import java.util.*; public class CommandLineView { private RentABike rentaBike; public CommandLineView( ) {rentaBike = new RentABike("Bruce's Bikes"); }
public void printAllBikes( ) { System.out.println(rentaBike.toString( )); //调用门面的方法 Iterator iter = rentaBike.getBikes( ).iterator( ); while(iter.hasNext( )) { Bike bike = (Bike)iter.next( ); System.out.println(bike.toString( )); } }
public static final void main(String[] args) { CommandLineView clv = new CommandLineView( ); clv.printAllBikes( ); } }
运行结果:
C:\RentABikeApp\out> java CommandLineView
RentABike: Bruce's Bikes Bike : manufacturer -- Shimano : model -- Roadmaster : frame -- 20 : serialNo -- 11111 : weight -- 15.0 : status -- Fair.
Bike : manufacturer -- Cannondale : model -- F2000 XTR : frame -- 18 : serialNo -- 22222 : weight -- 12.0 : status -- Excellent.
Bike : manufacturer -- Trek : model -- 6000 : frame -- 19 : serialNo -- 33333 : weight -- 12.4 : status -- Fair.
大家看出问题了吗? 1 门面RentABike静态的创建了一个商店里的租用的过山车,所以到时候如果一个新的过山车被引入的话,那么就需要手工修改RentABike的代码,比如我们再加一个Bike2.java,属性和Bike.java不一样。我们还需要在RentABike的类里实例化它才行,这样就造成了硬编码 2 这个模型Bike.java是很难测试的,因为Bikes数组是固定的 3 这用户接口和门面之间是强耦合的,它们存在一个硬编码的依赖,大家注意CommandLineView.java中的这两行代码: private RentABike rentaBike; public CommandLineView( ) {rentaBike = new RentABike("Bruce's Bikes"); }
所以,这一段程序虽然能够完成一个简单的租用过山车的操作,但是却不是一个易维护和扩展的。
|