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 instructor, the Mentor contacts the builder, and finally gets the product we need. That is, the builder model is able to enforce a process of construction that is carried out in steps.

The builder pattern is to encapsulate the complex internal creation inside. For external callers, the only way to get to the instructor and builder is to be able to build a product in detail, and users don't have to know that different products need to define different builders. Concept: 1. Abstract Builder: This class is generally an abstract class or an interface. It is responsible for describing the construction process (one or several ministries) of the narrative product. At the same time provide a way to return to the end product, such as this class called Carbuilder (void Makehead (construction of the front of the interface), void Makebody (building the interface of the body), void Makefoot (the construction of the tail of the interface), Car Getcar ( Return to a car), three void methods.    is the assembly order of this car, Getcar is returned to the last product. 2. Detailed builder (Implbuilder or Extendsbuilder): This class is implemented or inherited by the first step of the interface or abstract class, can exist there are many, for example Jeepbuilder (Jeep construction), Aodibuilder (Audi car construction) , the time to implement Getcar (). Returns an instance of a product with different instance Properties 3. Product instance (Car): Refers to the object being built. There are multiple properties within an object, or multiple build steps 4. Conductor (director): The conductor is also known as the Director, who is responsible for arranging the construction of complex objects, the relationship between the conductor and the abstract builder, and the method of the conductor (Createcar () Call the construction steps of the abstract builder's implementation class and complete the construction of the complex object. The client generally only needs to instantiate a conductor, then instantiate a builder (the detailed construction of a jeep or a detailed construction of a aodi car), and then pass in the builder through the constructor or setter method of the conductor. Call the construction process in the command class of the conductor.
The advantages of the construction model: 1. When using the builder mode, the client does not need 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 steps 2. Builders are independent. Easy to extend the extensibility of the different builders today, just like the steps of the builder's call. It is possible to inherit an abstract class or implement an interface. To return to different builders, which facilitates the expansion of the system 3. Easy to control the details risk as the detailed builder is independent, it is possible to customize the construction objects for DIY or refinement and so on. Without worrying about the other modules that will produce whatever affects the builder mode usage scenario: 1. The same method, different run order. When different event results are generated, the builder pattern can be used 2. 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 analogy is more complex, or the invocation of a different order will produce different structures. Ability to use Builder Mode 4. Other objects in the system are used during the creation of the object, which is not easy to get in the creation project of the Product object, and can also be used to create the object in this mode. 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 type of parts and assembly sequence. This is the biggest difference between his and the factory model. Although the same is true for creating class patterns, the focus is different.



Java design pattern-builder mode

Related Article

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.