这本书是一个以代码和实战为主的书
全书在构建一个过山车订购系统
体育商店可以用来对它们的过山车进行管理和订购
第一节作者先硬编码了两个有依赖关系的类CommandLineView
java和RentABike
java
我们先看看源代码
Example Bikejava
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 frameString serialNo double weight String status)
{
thismanufacturer = manufacturer;
thismodel = model;
thisframe = frame;
thisserialNo = serialNo;
thisweight = weight;
thisstatus = 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) {thismanufacturer = manufacturer;}
public String getModel( ) { return model; }
public void setModel(String model) { thismodel = model; }
public int getFrame( ) { return frame; }
public void setFrame(int frame) { thisframe = frame; }
public String getSerialNo( ) { return serialNo; }
public void setSerialNo(String serialNo) { thisserialNo = serialNo; }
public double getWeight( ) { return weight; }
public void setWeight(double weight) { thisweight = weight; }
public String getStatus( ) { return status; }
public void setStatus(String status) { thisstatus = status; }
}
可以看出Bikejava是一个普通的JAVABEAN用来表述一个过山车实体包括制造商型号规格序列号重量和状态等树型
Example RentABikejava
import javautil*;
public class RentABike
{
private String storeName;
final List bikes = new ArrayList( );
public RentABike(String storeName)
{
thisstoreName = storeName;//商店名 //添加过山车到数组
bikesadd(new Bike(Shimano Roadmaster Fair));
bikesadd(new Bike(Cannondale F XTR Excellent));
bikesadd(new Bike(Trek Fair)); }
public String toString( ) { return RentABike: + storeName; } //得到全部过山车
public List getBikes( ) { return bikes; } //得到某一个序列号的过山车
public Bike getBike(String serialNo)
{
Iterator iter = bikesiterator( );
while(iterhasNext( ))
{
Bike bike = (Bike)iternext( );
if(serialNoequals(bikegetSerialNo( ))) return bike;
}
return null;
}
}
这个类描叙了租用一台过山车的操作storeName是传入的商店名称它对客户端来说是一个门面把所租用的过山车都放在一个List数组中存放起来然后对外提供getBikes和getBike两个方法可以让客户端知道目前所租用的所有过山车和某一个序列号的过山车是什么
我们再看看用户接口是如何调用的
Example CommandLineViewjava
import javautil*;
public class CommandLineView
{
private RentABike rentaBike;
public CommandLineView( ) {rentaBike = new RentABike(Bruces Bikes); }
public void printAllBikes( )
{
Systemoutprintln(rentaBiketoString( ));//调用门面的方法
Iterator iter = rentaBikegetBikes( )iterator( );
while(iterhasNext( ))
{
Bike bike = (Bike)iternext( );
Systemoutprintln(biketoString( ));
}
}
public static final void main(String[] args)
{
CommandLineView clv = new CommandLineView( );
clvprintAllBikes( );
}
}
运行结果
C:\RentABikeApp\out> java CommandLineView
RentABike: Bruces Bikes
Bike : manufacturer Shimano
: model Roadmaster
: frame
: serialNo
: weight
: status Fair
Bike : manufacturer Cannondale
: model F XTR
: frame
: serialNo
: weight
: status Excellent
Bike : manufacturer Trek
: model
: frame
: serialNo
: weight
: status Fair
大家看出问题了吗?
门面RentABike静态的创建了一个商店里的租用的过山车所以到时候如果一个新的过山车被引入的话那么就需要手工修改RentABike的代码比如我们再加一个Bikejava属性和Bikejava不一样我们还需要在RentABike的类里实例化它才行这样就造成了硬编码
这个模型Bikejava是很难测试的因为Bikes数组是固定的
这用户接口和门面之间是强耦合的它们存在一个硬编码的依赖大家注意CommandLineViewjava中的这两行代码
private RentABike rentaBike;
public CommandLineView( ) {rentaBike = new RentABike(Bruces Bikes); }
所以这一段程序虽然能够完成一个简单的租用过山车的操作但是却不是一个易维护和扩展的
Spring笔记和小结(二)
Spring笔记和小结(三)
Spring笔记和小结(四)