Java design pattern-builder mode

Source: Internet
Author: User

The builder pattern separates complex constructs from their representations, and the same build process can create different representations. The difference between the builder model and the abstract factory is that in the builder model there is a mentor, the mentor who manages the builders, the user interacts with the mentor, the Mentor contacts the builder, and finally gets the product we need.    That is, the builder model can enforce a process of construction in stages. The builder pattern is to encapsulate the complex internal creation inside, for external callers, as long as the incoming mentor and the builder can, how to build a product inside, the user does not have to know, different products need to define different builders. Concept: 1. Abstract Builder: This class is generally abstract or interface, it is responsible for describing the construction of the product (one or several parts), while providing a way to return the final product, such as the class called Carbuilder (void Makehead (the interface to build the front),    void Makebody (interface for building the body), void Makefoot (interface for building rear end), car Getcar (return a car)), three void method, is the assembly order of this vehicle, Getcar is to return the final product. 2. Concrete Builders (Implbuilder or Extendsbuilder): This class is implemented or inherited by the first step of the interface or abstract class, can exist there are many, such as Jeepbuilder (Jeep construction), Aodibuilder (Audi car construction) , when implementing Getcar (), returns an instance of the product, the instance properties are different 3. Product instance (Car): Refers to the object being built, there are multiple properties within the object, or multiple build step 4. Conductor (director): The conductor is also known as the Director, who is responsible for arranging the construction of complex objects, There is an association between the conductor and the abstract builder, and the method in the conductor (Createcar ()) invokes the construction steps of the abstract builder's implementation class to complete the construction of the complex object. The client generally only needs to instantiate a conductor, then instantiate a builder (the specific construction of a jeep or a concrete construction of a aodi vehicle), and then pass in the builder through the constructor or setter method of the conductor, invoking the construction process in the command class of the conductor.
Advantages of the construction mode: 1. Encapsulation when using builder mode, the client does not have to know the details of the internal composition of the product, such as the example above, and finally returns the car object, passing in different builders, returning different car objects, the client does not need to know the internal assembly step 2. The builder is independent, easy to expand easy to expand is reflected in the construction of different builders, as long as the constructor of the method of invocation, you can inherit the abstract class or implement the interface, to return to different builders, conducive to the expansion of the system 3. Easy to control the details risk because the concrete builders are independent, so Build objects can be customized DIY or refinement and so on, without worrying about the other modules will affect the use of the builder mode of the scene: 1. The same method, different execution order, produces different event results when the Builder Mode 2 can be used. Multiple steps or parts can be assembled into an object, But the resulting object content is not the same this mode can be used at this time 3. The product class is more complex, or the call to different order will produce different structures, you can use the Builder Mode 4. Other objects in the system are used during the creation of the object, which are not readily available in the creation of the Product object. You can also use this mode to create a window for the object's creation process. This method is not recommended, this method is only a compensation method, because an object is not easy to get, at the time of design has violated the original target Builder mode of attention to the part type and assembly order, this is the most different from the factory model, although the same as the creation of class mode, but the emphasis is different.

Java design pattern-builder mode

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.