Monday, May 14, 2012

Design Pattern Study Notes 5 - Builder


























1) interface to define build methods.
============== Builder.java ================

package test.pattern.builder;
/**
 *  interface
 *
 */
public interface Builder {
public abstract void doPart1();
public abstract void doPart2(String para);
public abstract void doPart3(int para);
public abstract Product getProduct();
}

2) to create product object
============== Director.java =============
package test.pattern.builder;

public class Director {

private Builder builder;

public Director(Builder builder) {
this.builder = builder;
}

public Product construct(String para, int para2) {
builder.doPart1();
builder.doPart2(para);
builder.doPart3(para2);
Product product = builder.getProduct();
return product;
}
}

3) product class
============== Product.java =============
package test.pattern.builder;

public class Product {

private String component1;
private int component2;
        // getter and setter methods
}

4) concrete builder: 
a) implement the methods defined in above interface to build the product step by step
b) return the product instance

============== ConcreteBuilder1.java ==================
package test.pattern.builder;

public class ConcreteBuilder1 implements Builder {
Product prod = new Product();

public void doPart1() {
System.out.println("execute builder1:part1");
}

public void doPart2(String para) {
System.out.println("execute builder1:part2:" + para);
prod.setComponent1(para);
}

public void doPart3(int para) {
System.out.println("execute builder1:part3:" + para);
prod.setComponent2(para);
}

public Product getProduct() {
return prod;
}

}

=============== ConcreteBuilder2.java ==============
package test.pattern.builder;

public class ConcreteBuilder2 implements Builder {
Product prod = new Product();

public void doPart1() {
System.out.println("execute builder2:part1");
}

public void doPart2(String para) {
System.out.println("execute builder2:part2:" + para);
prod.setComponent1(para);
}

public void doPart3(int para) {
System.out.println("execute builder2:part3:" + para);
prod.setComponent2(para);
}

public Product getProduct() {
return prod;
}
}

5) test class
============== Client.java =================
package test.pattern.builder;

public class Client {

public static void main(String[] args) {
Director director = new Director(new ConcreteBuilder1());
Product prod = director.construct("do something", 4);
System.out.println("got: " + prod.getComponent1());
System.out.println("got: " + prod.getComponent2());

director = new Director(new ConcreteBuilder2());
prod = director.construct("do something else", 5);
System.out.println("got: " + prod.getComponent1());
System.out.println("got: " + prod.getComponent2());
}

}

This is the output:
execute builder1:part1
execute builder1:part2:do something
execute builder1:part3:4
got: do something
got: 4
execute builder2:part1
execute builder2:part2:do something else
execute builder2:part3:5
got: do something else
got: 5


No comments:

Post a Comment