Java Design Pattern learning 4 -- builder pattern [original]

Source: Internet
Author: User
Today, I looked at the construction mode and came up with a bit of experience.
Apply a theory first: the construction mode separates the internal appearance of a product from the product generation process, so that a building process generates product objects with different internal appearances. 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.
How can this problem be solved?
How should we design a big system? I think, for a good design, we should try not to use specific things for definition, just as in my previous article Article In the end, define types as abstract as possible. Remember, in Java, abstraction is the highest realm. All specific things are instantiated from abstraction and abstract things can exist, the actual things are also acceptable. What we want to design is an abstract structure. Each part of the abstract structure is also abstract. The abstract part can be implemented differently, so that the entire system can be implemented differently.
Speaking of this, I think of the abstract factory model. How are the two similar ?? Well, let's talk about why it is called the construction model.
In the abstract factory model, from an abstract factory to a specific factory to a series of specific products, the user is connected with the factory to get the product, while in the construction model, from an abstract builder to multiple specific builders and then to products, this is the same. However, in the construction mode, there is also a mentor, the mentor is responsible for managing the builder. the user is in contact with the mentor, and the mentor contacts the builder to get the product. Let's look at the last sentence of the theory. "The construction mode can enforce a step-by-step construction process." Do you understand? The construction mode "yes" enforces a step-by-step construction process. Note that it is OK, not necessary. Do you understand the differences between the abstract factory model and the construction model?
Next, we will extract a step-by-step online construction process:
Public interface builder {

Void buildparta ();
Void buildpartb ();
Void buildpartc ();

Product getresult ();
}

Public class director {

Private Builder;

Public Director (Builder ){
This. Builder = builder;
}
Public void construct (){
Builder. buildparta ();
Builder. buildpartb ();
Builder. buildpartc ();
}
}

Public class concretebuilder implements builder {

Part parta, partb, partc;
Public void buildparta (){
// Here is how to build partaCode

};
Public void buildpartb (){
// Here is how to build the partb code
};
Public void buildpartc (){
// Here is how to build the partb code
};
Public Product getresult (){
// Return the final assembly result
};

}

Public interface product {}
Public interface part {}
The following describes how to call builder:
Concretebuilder builder = new concretebuilder ();
Director dire= new director (builder );

Director. Construct ();
Product = builder. getresult ();

The construction mode is described here.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.