Spring: A Developer's Notebook筆記和小結(jié)(1)
Spring: A Developer's Notebook筆記和小結(jié)(1)
關(guān)鍵詞: spring/**
作者:Willpower
來(lái)源:Rifoo Technology(http://www.rifoo.com)
時(shí)間:2005-12-25
備注:轉(zhuǎn)載請(qǐng)保留以上聲明
**/
這本書(shū)是一個(gè)以代碼和實(shí)戰(zhàn)為主的書(shū),全書(shū)在構(gòu)建一個(gè)過(guò)山車(chē)訂購(gòu)系統(tǒng),體育商店可以用來(lái)對(duì)它們的過(guò)山車(chē)進(jìn)行管理和訂購(gòu)。第一節(jié)作者先硬編碼了兩個(gè)有依賴(lài)關(guān)系的類(lèi)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是一個(gè)普通的JAVABEAN,用來(lái)表述一個(gè)過(guò)山車(chē)實(shí)體,包括制造商,型號(hào),規(guī)格,序列號(hào),重量和狀態(tài)等樹(shù)型。
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;//商店名
//添加過(guò)山車(chē)到數(shù)組
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; }
//得到全部過(guò)山車(chē)
public List getBikes( ) { return bikes; }
//得到某一個(gè)序列號(hào)的過(guò)山車(chē)
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;
}
}
這個(gè)類(lèi)描敘了租用一臺(tái)過(guò)山車(chē)的操作,storeName是傳入的商店名稱(chēng)。它對(duì)客戶(hù)端來(lái)說(shuō)是一個(gè)門(mén)面,把所租用的過(guò)山車(chē)都放在一個(gè)List數(shù)組中存放起來(lái),然后對(duì)外提供getBikes和getBike兩個(gè)方法,可以讓客戶(hù)端知道目前所租用的所有過(guò)山車(chē)和某一個(gè)序列號(hào)的過(guò)山車(chē)是什么。
我們?cè)倏纯从脩?hù)接口是如何調(diào)用的:
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( ));
//調(diào)用門(mén)面的方法
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( );
}
}
運(yùn)行結(jié)果:
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.
大家看出問(wèn)題了嗎?
1 門(mén)面RentABike靜態(tài)的創(chuàng)建了一個(gè)商店里的租用的過(guò)山車(chē),所以到時(shí)候如果一個(gè)新的過(guò)山車(chē)被引入的話,那么就需要手工修改RentABike的代碼,比如我們?cè)偌右粋€(gè)Bike2.java,屬性和Bike.java不一樣。我們還需要在RentABike的類(lèi)里實(shí)例化它才行,這樣就造成了硬編碼
2 這個(gè)模型Bike.java是很難測(cè)試的,因?yàn)锽ikes數(shù)組是固定的
3 這用戶(hù)接口和門(mén)面之間是強(qiáng)耦合的,它們存在一個(gè)硬編碼的依賴(lài),大家注意CommandLineView.java中的這兩行代碼:
private RentABike rentaBike;
public CommandLineView( ) {rentaBike = new RentABike("Bruce's Bikes"); }
所以,這一段程序雖然能夠完成一個(gè)簡(jiǎn)單的租用過(guò)山車(chē)的操作,但是卻不是一個(gè)易維護(hù)和擴(kuò)展的。
Trackback: http://tb.blog.csdn.net/TrackBack.aspx?PostId=567619
posted on 2006-03-02 17:05 都市淘沙者 閱讀(1204) 評(píng)論(0) 編輯 收藏 所屬分類(lèi): Spring+Struts+Hibernate