Spring: A Developer's Notebook筆記和小結(1)

/**
作者: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"); }

所以,這一段程序雖然能夠完成一個簡單的租用過山車的操作,但是卻不是一個易維護和擴展的。

發佈了84 篇原創文章 · 獲贊 2 · 訪問量 16萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章