一个小问题 TestClient.java

03-12-24 Norberthu5
中: 《23种设计模式的java实现(提供源代码) 》

builder 模式中:

/*

* A test client to create a house

* but we do not know how the room and door be created

*/

public class TestClient {

public TestClient() {

}

public static void main(String[] args) {

House myHouse = new House(); ----我认为这行是多余,使人误解。应该修改为: House myHouse ;当然即使不修改,也没有任何错,除了浪费内存。

ConcreteHouseBuilderA myHouseBuilder = new ConcreteHouseBuilderA();

HouseDirector myHouseDirector = new HouseDirector();

myHouseDirector.CreateHouse(myHouseBuilder);

myHouse = myHouseBuilder.getHouse();

System.out.println("My house has room :" + myHouse.getRoomNumber());

System.out.println("My house has door :" + myHouse.getDoorNumber());

}

}

jxb8901
2003-12-25 09:36
我认为完全可以在需要变量时再声明。如:

House myHouse = myHouseBuilder.getHouse();
<p>

不过这也只是个人习惯啦。

猜你喜欢