CSDN博客

img yanghx2000

设计模式第3篇Builder(第二部分)

发表于2004/3/8 10:02:00  587人阅读

下面是重点

Builder具体实现

中国制造4轮汽车

package com.yjsoft.builder;

/**

*

* To change the template for this generated type comment go to

* Window>Preferences>Java>Code Generation>Code and Comments

* @author yanghx2004

*/

public class Car4WheelBuilder extends CarBuilder {

 

    /**

    *@see java.lang.Object#Object()

    */

    public Car4WheelBuilder() {

       m_car=new Car();

    }

 

    /* (non-Javadoc)

     * @see com.yjsoft.builder.Builder#buildSteer()

     */

    public void buildSteer() {

       // TODO Auto-generated method stub

       m_car.setSteer("4 car Stree.");

    }

 

    /* (non-Javadoc)

     * @see com.yjsoft.builder.Builder#buildEngine()

     */

    public void buildEngine() {

       // TODO Auto-generated method stub

       m_car.setEngine("Made in China.");

    }

 

    /* (non-Javadoc)

     * @see com.yjsoft.builder.Builder#buildWheel()

     */

    public void buildWheel() {

       // TODO Auto-generated method stub

       m_car.setWheel(4);

    }

}

 

小日本造的2轮汽车

package com.yjsoft.builder;

/**

*

* To change the template for this generated type comment go to

* Window>Preferences>Java>Code Generation>Code and Comments

* @author yanghx2004

*/

public class Car2WheelBuilder extends CarBuilder {

 

    /**

    *@see java.lang.Object#Object()

    */

    public Car2WheelBuilder() {

       m_car=new Car();

    }

 

    /* (non-Javadoc)

     * @see com.yjsoft.builder.Builder#buildSteer()

     */

    public void buildSteer() {

       // TODO Auto-generated method stub

       m_car.setSteer("2 wheel car.");

    }

 

    /* (non-Javadoc)

     * @see com.yjsoft.builder.Builder#buildEngine()

     */

    public void buildEngine() {

       // TODO Auto-generated method stub

       m_car.setEngine("Made in JP(GuiZi)");

    }

 

    /* (non-Javadoc)

     * @see com.yjsoft.builder.Builder#buildWheel()

     */

    public void buildWheel() {

       // TODO Auto-generated method stub

       m_car.setWheel(2);

    }

}

 

调用具体Builder

 

public class TestClass {

 

    public static void main(String[] args) {

 

       Director md = new Director(null);

       Car4WheelBuilder c4 = new Car4WheelBuilder();

       Car2WheelBuilder c2 = new Car2WheelBuilder();

       md.setBuilder(c4);

       md.construct();

       c4.getResult().show();

 

       md.setBuilder(c2);

       md.construct();

       c2.getResult().show();

    }

}

 

输出

 

关于具体Builder类实现请参考代码。

 

1.   总结

Java里面,线程Pooler与连接Pooler多利用到了Builder模式,详细实现请参考我先前发表的文章。

一般来说利用到某种“缓存”来创建的,利用Builder模式会方便很多,关于某种“缓存”只有自己体会了。

 

代码下载

0 0

相关博文

我的热门文章

img
取 消
img