Willpower's Java Zone

SUN:The Network is The Computer

原创 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"); }

所以,这一段程序虽然能够完成一个简单的租用过山车的操作,但是却不是一个易维护和扩展的。

发表于 @ 2005年12月31日 22:54:00|评论(loading...)

新一篇: Spring: A Developer's Notebook笔记和小结(2) | 旧一篇: StrutsCatalog系列(6)--文件下载

用户操作
[即时聊天] [发私信] [加为好友]
willpower
订阅我的博客
XML聚合  FeedSky
订阅到鲜果
订阅到Google
订阅到抓虾
willpower的公告
除了注明是收藏文章外,其他均为原创或翻译。有任何问题可以联系我:caiyi0903 at hotmail dot com,也真诚希望有同样爱好的朋友可以和我交换blog连接。 CSDN咨询顾问团成员 CSDN咨询顾问团成员
文章分类
收藏
    常去网站
    Apache Jakarta
    BEA dev2dev在线
    CSDN
    IBM DeveloperWorks
    Java Matrix
    JavaResearch
    JAVA视线论坛
    SpringFramework中文论坛
    中国JAVA开发网
    中国系统分析师
    朋友圈
    CSDN编辑 高曹
    f9inux的专栏
    Rifoo Technology
    sabrina的blog
    博文视点 周老师
    博文视点 晓菲
    李德成 PeckerCheng
    李琨的AJAX中国
    存档
    软件项目交易
    Csdn Blog version 3.1a
    Copyright © willpower