There are many methods to create objects. Common methods include constructors, static factory methods, and factory methods. The preceding three methods are not suitable for creating objects that require multiple parameters. Some of them are optional. Readers may think of building an object prototype through an empty constructor, and then calling the getter and setter methods to inject status data into the object. This method may seem good, but there may still be a risk that the object has been used before its State data is completely written.
The builder mode separates the internal representation of a product from the product generation process, so that a building process generates product objects with different internal representations. The construction mode allows the product's internal appearance to change independently, so that the customer does not have to know the details of the product's internal composition. The construction model enforces a step-by-step construction process.
The builder mode involves three roles:
Product: the object to be built;
Builder: the process of building abstract products;
Directors: build different products as needed.
Taking the process of creating a file on the Internet as an example, let's take a look at the product to be created, that is, the file object. A file consists of three parts:
[Java]
Public class MyFile {
Private String head;
Private String content;
Private String end;
Public void setFcontent (Strig content ){
This. content = content;
}
Public void setEnd (String end ){
This. end = end;
}
Public void setHead (String head ){
This. head = head;
}
Public void write (String path) throws IOException {
File f = new File (path );
PrintWriter out = new PrintWriter (new FileWriter (f ));
Out. println (head );
Out. println (content );
Out. println (end );
Out. close ();
}
}
Let's look at the creator. Here is an interface:
[Java]
Public interface FileBuilder {
Void buildHead ();
Void buildContent ();
Void buildEnd ();
MyFile getResult ();
}
Finally, the Director is used to implement the object creation process:
[Java]
Public class FileDirector {
Private FileBuilder filebuilder;
Public FileDirector (FileBuilder filebuilder ){
This. filebuilder = filebuilder;
}
Public MyFile construct (){
Filebuilder. buildHead ();
Filebuilder. buildContent ();
Filebuilder. buildEnd ();
Return filebuilder. getResult ();
}
}
Two specific creators are used to create XML files and TXT files respectively:
[Java]
Public class XmlFileBuilder implements FileBuilder {
Private MyFile file = new MyFile ();;
Public void buildHead (){
File. setHead ("<? Xml version = \ "1.0 \"?> <Content> ");
}
Public void buildContent (){
File. setContent ("<node> value </node> ");
}
Public void buildEnd (){
File. setEnd ("</content> ");
}
Public MyFile getResult (){
Return file;
}
}
Public class TxtFileBuilder implements FileBuilder {
Private MyFile file = new MyFile ();;
Public void buildHead (){
File. setHead ("this it the txt file head ");
}
Public void buildContent (){
File. setContent ("this it the txt file content ");
}
Public void buildEnd (){
File. setEnd ("this it the txt file end ");
}
Public MyFile getResult (){
Return file;
}
}
Finally, the test code is as follows:
[Java]
Public void test (){
FileBuilder builder = new XmlFileBuilder ();
FileDirector ctor = new FileDirector (builder );
MyFile file = director. construct ();
// The code for using objects is omitted.
}
Looking at the code above, we can create different objects by replacing the builder implementation. The object creation process is well hidden and will not expose the uncreated objects.
As a developer, you must have used the tool class StringBuilder or StringBuffer provided by JDK. In fact, StringBuilder and StringBuffer use the builder mode. Www.2cto.com
More JAVA Design Patterns
By fancy888