|
Thursday, 20 January 2005 21:16 |
Separate the construction of a complex object from its representation so that the same construction process can create different representations.

Source Code
/**
* The interface Product defines interface to create parts of the
* Product.
*/
public interface Builder {
/**
* Construct part of the complex Product.
*/
public void buildPart();
/**
* Construct the Product.
*
* @return the constructed product
*/
public Product getProduct();
}
/**
* The ConcreteBuilder is the product of a concrete builder.
*
*/
public class ConcreteBuilder implements Product {
}
/**
* The ConcreteBuilderBuilder creates and assembles parts of
* the Product.
*
*/
public class ConcreteBuilderBuilder implements Builder {
/**
* Reference to Product being constructed
*/
private ConcreteBuilder concreteBuilder;
/**
* Construct part of the complex Product.
*/
public void buildPart() {
// put your code here
}
/**
* Construct the Product.
*
* @return the constructed product
*/
public Product getProduct() {
return concreteBuilder;
}
}
/**
* The ConcreteBuilderClient initialized the Director with a
* Concrete Bulder to create the Product and gets result from the Builder.
*
*/
public class ConcreteBuilderClient {
/**
* Use the Builder to create the Product.
*/
public void createProduct() {
ConcreteBuilderBuilder builder = new ConcreteBuilderBuilder();
new Director(builder).construct();
Product product = builder.getProduct();
}
}
/**
* The class Director manages Product creation using Builder.
*/
public class Director {
/**
* Reference to Builder currently used
*/
private Builder builder;
/**
* Create a new Director instance.
*
* @param builder the builder which will create the product
*/
public Director(Builder builder) {
this.builder = builder;
}
/**
* Construct the Product using the Builder.
*/
public void construct() {
builder.buildPart();
}
}
/**
* The interface Product defines a complex object that is
* constructed part-by-part with Builder.
*/
public interface Product {
}
|
|
Last Updated on Thursday, 04 August 2005 17:13 |