1 overview
The Builder model (Builder pattern) is primarily used to "step through a complex object", where "step-by-step" is a stable algorithm, and parts of complex objects often change. Therefore, the builder model is mainly used to solve the demand change of "object part". This allows finer control over the process of object construction.
2 Example
In the production of mobile phones, for example, each cell phone is divided into screen screens, CPU, Battery. Now we want to produce two kinds of mobile phones, Apple machines and Samsung.
Apple:
Copy Code code as follows:
Package org.scott.builder.before.use;
Import java.util.ArrayList;
Import java.util.List;
/**
* @author Scott
* @version 2013-11-20
* @description
*/
public class Applephone {
list<string> parts = new arraylist<string> ();
public void Createcpu () {
Parts.add ("Cup:qualcomm");
}
public void Createscreen () {
Parts.add ("Screen:jdi");
}
public void Createbattery () {
Parts.add ("Battery:desai");
}
public void Show () {
System.out.print ("Product part information:");
for (String part:parts) {
System.out.print (part + "T");
}
}
}
Samsung:
Copy Code code as follows:
Package org.scott.builder.before.use;
Import java.util.ArrayList;
Import java.util.List;
/**
* @author Scott
* @version 2013-11-20
* @description
*/
public class Samsungphone {
list<string> parts = new arraylist<string> ();
public void Createcpu () {
Parts.add ("CUP:MTK");
}
public void Createscreen () {
Parts.add ("Screen:samsung");
}
public void Createbattery () {
Parts.add ("Battery:desai");
}
public void Show () {
System.out.print ("Product part information:");
for (String part:parts) {
System.out.print (part + "T");
}
}
}
Test client:
Copy Code code as follows:
Package org.scott.builder.before.use;
/**
* @author Scott
* @version 2013-11-20
* @description
*/
public class Builertest {
private static Applephone iphone = new Applephone ();
private static Samsungphone Samphone = new Samsungphone ();
public static void Main (String args[]) {
Iphone.createcpu ();
Iphone.createscreen ();
Iphone.createbattery ();
Iphone.show ();
Samphone.createcpu ();
Samphone.createscreen ();
Samphone.createbattery ();
Samphone.show ();
}
}
Did you find a problem? That is the production of mobile phone every process is the same, the exact name of the operation is the same, only the specific processing of each process is different, the process is unchanged, so a few steps, each process of the specific processing is changed, from this, we can be the constant extraction, to "change should be changed", will change, To give specific products to do.
How do you do it specifically? This time the builder model comes in handy.
First comes the phone's interface:
Copy Code code as follows:
Package org.scott.builder.after.use;
Import java.util.ArrayList;
Import java.util.List;
/**
* @author Scott
* @version 2013-11-20
* @description
*/
Public abstract class Phone {
protected list<string> parts = new arraylist<string> ();
public void Add (String part) {
Parts.add (part);
}
public void Show () {
System.out.print ("Product part information:");
for (String part:parts) {
System.out.print (part + "T");
}
}
}
Apple Phone class:
Copy Code code as follows:
Package org.scott.builder.after.use;
/**
* @author Scott
* @version 2013-11-20
* @description
*/
public class Applephone extends phone{
}
Samsung mobile Phone Class:
Copy Code code as follows:
Package org.scott.builder.after.use;
/**
* @author Scott
* @version 2013-11-20
* @description
*/
public class Samsungphone extends phone{
}
Then define the interface builder for the production step:
Copy Code code as follows:
Package org.scott.builder.after.use;
/**
* @author Scott
* @version 2013-11-20
* @description
*/
Public interface Builder {
public void buildcpu ();
public void Buildscreen ();
public void Buildbattery ();
Public Phone getphone ();
}
The builder of the Apple phone:
Copy Code code as follows:
Package org.scott.builder.after.use;
/**
* @author Scott
* @version 2013-11-20
* @description
*/
public class Applephonebuilder implements builder{
Private phone phone = new Applephone ();
@Override
public void Buildcpu () {
Phone.add ("Cup:qualcomm");
}
@Override
public void Buildscreen () {
Phone.add ("Screen:jdi");
}
@Override
public void Buildbattery () {
Phone.add ("Battery:desai");
}
@Override
Public Phone Getphone () {
return phone;
}
}
Samsung mobile phone's builder:
Copy Code code as follows:
Package org.scott.builder.after.use;
/**
* @author Scott
* @version 2013-11-20
* @description
*/
public class Samsungphonebuilder implements builder{
Private phone phone = new Samsungphone ();
@Override
public void Buildcpu () {
Phone.add ("CUP:MTK");
}
@Override
public void Buildscreen () {
Phone.add ("Screen:samsung");
}
@Override
public void Buildbattery () {
Phone.add ("Battery:desai");
}
@Override
Public Phone Getphone () {
return phone;
}
}
Guide the specific production of mobile phone Director:
Copy Code code as follows:
Package org.scott.builder.after.use;
/**
* @author Scott
* @version 2013-11-20
* @description
*/
public class Director {
Private Builder Builder;
Public Director (Builder Builder) {
This.builder = Builder;
}
public void construct () {
Builder.buildcpu ();
Builder.buildscreen ();
Builder.buildbattery ();
}
}
Finally, write a test class:
Copy Code code as follows:
Package org.scott.builder.after.use;
/**
* @author Scott
* @version 2013-11-20
* @description
*/
public class Buildertest {
private static Builder Iphonebuilder = new Applephonebuilder ();
private static Builder Samphonebuilder = new Samsungphonebuilder ();
public static void Main (string[] args) {
Director Director = new Director (Iphonebuilder);
Director.construct ();
Phone phone = Iphonebuilder.getphone ();
System.out.println ("iphone");
Phone.show ();
Director = new Director (Samphonebuilder);
Director.construct ();
Phone = Samphonebuilder.getphone ();
System.out.println ("\nsamsung");
Phone.show ();
}
}
Run Result:
Copy Code code as follows:
Iphone
Product part information: Cup:qualcomm Screen:jdi Battery:desai
SamSung
Product part information: CUP:MTK Screen:samsung Battery:desai
Here's two phone entity classes are empty, if this is the case, then they can be omitted, if the phone interface can also be omitted, the final remaining only Director, Builder, and the specific Bulider implementation class. And, Applephone class and Samsungphone class are related to two classes, their different mobile phone brands, if you encounter two or more classes without too many relationships, the public interface phone is not necessary, but this time, then the Builder interface of the provisions of the How do you determine the return value of the Getphone () method?
Regardless of whether the return value type is Applephone or samsungphone, the problem occurs because the type of the return result is not uniform. At this point, the phone can be defined as an empty interface (does not contain any method of the interface), and then let these have no relationship between the specific product classes to implement this interface, then the Builder interface of the Getphone () method of the return value type is still the Phone type, to solve the problem. In this case, however, there is no need to use the builder model.