熱點推薦:
您现在的位置: 電腦知識網 >> 編程 >> Java編程 >> Java開源技術 >> 正文

Spring筆記和小結(一)

2022-06-13   來源: Java開源技術 
    這本書是一個以代碼和實戰為主的書全書在構建一個過山車訂購系統體育商店可以用來對它們的過山車進行管理和訂購第一節作者先硬編碼了兩個有依賴關系的類CommandLineViewjava和RentABikejava我們先看看源代碼

    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 frame String 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筆記和小結(四)


From:http://tw.wingwit.com/Article/program/Java/ky/201311/29032.html
    推薦文章
    Copyright © 2005-2022 電腦知識網 Computer Knowledge   All rights reserved.