Design Patterns ----Builder ( builder ) Mode
GoF : separates the build of a complex object from its representation, allowing the same build process to create different representations.
The builder mode is very much like the Absrtact Factory mode. Gof points out that the main difference between the two is that the builder model follows certain steps to create the object in a step-by-step way.
The builder pattern is designed to separate the process of building a complex object from his part. Because a complex object not only has a lot of components, there are a lot of small parts. He (the complex object) itself depends on these parts and parts to assemble. For example: A house that has many parts: rooms, windows, locks on doors and windows, and small parts such as bolts on Windows. The purpose of the builder model is to separate parts (doors, windows, etc.) from the assembly process (the building of the whole house).
Now suppose that a house is composed of multiple parts, the purpose of the builder pattern is to separate the creation of complex objects from the creation of parts. are represented by the builder class and the Director class, respectively.
Here is an example of building a house:
First, define an abstract class that defines how to create various parts of a complex object
Package Builder;
Public abstract class Housebuilder
{
public abstract void Buildroom (int roomno);//Build Room
public abstract void Builddoor (int room1, int room2);//Build Door
Get the Last House
Public abstract house Gethouse ();
}//end Abstract class Housebuilder
Building complex objects (houses) with director:
Package Builder;
public class Housedirector
{
public void Createhouse (housebuilder ConcreteBuilder)
{
Concretebuilder.buildroom (2);
Concretebuilder.builddoor (3);
}//end createhouse (...)
}//end class Housedirector
Builder implementation Concretehousebuilder: Building parts of a product (house) by completing builder
Package Builder;
public class Concretehousebuilder extends housebuilder
{
Private house House;
Public Concretehousebuilder ()
{
House = new House ();
}//end Concretehousebuilder ()
Number of rooms created
public void buildroom (int roomnumber)
{
House.roomnumber = Roomnumber;
}//end Buildroom
Number of doors created
public void Builddoor (int doornumber)
{
House.doornumber = Doornumber;
}
Go back to the house you created.
Public house Gethouse ()
{
return to house;
}//end Gethouse ()
}//end class Concretehousebuildera
The specific room:
Package Builder;
public class House
{
int roomnumber;
int doornumber;
Public house ()
{
Roomnumber = 0;
Doornumber = 0;
}//end House ()
public int Getroomnumber ()
{
return roomnumber;
}//end Getroomnumber ()
public int Getdoornumber ()
{
return doornumber;
}//end Getdoornumber ()
}//end Class House
The specific application of builder mode:
/*
* Builderpattern.java
*
* Created on March 28, 2006, 10:01
*
* To change this template, choose Tools | Template Manager
* and open the template in the editor.
*/
Package Builder;
/**
*
* @author Administrator
*/
public class Builderpattern
{
House House;
Concretehousebuilder housebuilder;
Housedirector Housedirecor;
Public Builderpattern ()
{
House = new House ();
Housebuilder = new Concretehousebuilder ();
Housedirecor = new Housedirector ();
}
public void Createhouse ()
{
Housedirecor.createhouse (housebuilder);
House = Housebuilder.gethouse ();
System.out.println ("My house has room:" + house.getroomnumber ());
System.out.println ("My house has door:" + house.getdoornumber ());
}//end Creathouse ()
public static void Main (string[] args)
{
System.out.println ("The Builder pattern!");
Builderpattern bp = new Builderpattern ();
Bp.createhouse ();
}
}//end class Builderpattern
The final result:
My house has Room:2
My house has Door:3
Finally, a UML diagram is given:
Summary:
When an object is composed of complex parts, we can try to use the builder mode to achieve it. The builder mode separates the assembly process of objects from the parts of objects. In fact, this is very good understanding. For example, the production of automobiles and automobiles is separate from the production of automobile engines.
trackback:http://tb.blog.csdn.net/trackback.aspx?postid=967906