Java design pattern (builder mode)

Source: Internet
Author: User

Builder mode: It separates the construction of a complex object from its representation, allowing the same build process to create different representations.

The builder pattern typically includes the following roles:

1. Builder: Abstract builder, gives an abstract interface to standardize the construction of each component of the Product object. This interface specifies which parts of a complex object to create, and does not involve the creation of specific object parts.
2. ConcreteBuilder: Builders, implements the builder interface, and, for different business logic, materializes the creation of parts of complex objects. After the completion of the construction process, provide an example of the product.
3. Director: Directing classes, invoking concrete builders to create parts of complex objects that do not involve specific product information in the instructor, are only responsible for ensuring that the parts of the object are created in a complete or sequential way.
4. Product: A complex object to create. is generally a more complex object, that is, the process of creating objects is more complex, generally there will be more code. In this class diagram, the product class is a concrete class, not an abstract class. In practical programming, a product class can consist of an abstract class with its different implementations, or it can consist of multiple abstract classes and their implementations.

/** * @authorHjN * Product Category*/ Public classProduct {PrivateString name; PrivateString type;  Public voidshowproduct () {System.out.println ("I Am" +name+ ", I Am" +type+ "type"); }         PublicString GetName () {returnname; }         Public voidsetName (String name) { This. Name =name; }         PublicString GetType () {returntype; }         Public voidsetType (String type) { This. Type =type; }          }
/**@author  hjn  * *publicabstractclass Builder {    publicabstractvoid  Setpro (String arg1, String arg2) ;      Public Abstract Product getproduct ();}
/** * @authorHjN * Builder **/ Public classConcreteBuilderextendsBuilder {PrivateProduct Pro =NewProduct (); @Override Public voidSetpro (String arg1, String arg2) {pro.setname (arg1);    Pro.settype (ARG2); } @Override PublicProduct getproduct () {returnPro; }}
/** * @authorHJN * Director Class*/ Public classDirector {PrivateBuilder bu =NewConcreteBuilder ();  PublicProduct getpintwproduct () {Bu.setpro ("Plug", "two holes"); returnbu.getproduct (); }     PublicProduct getpinthproduct () {Bu.setpro ("Plug", "three holes"); returnbu.getproduct (); }}
/**@author*/Publicclass  Client    {  Publicstaticvoid  main (string[] args) {        new  Director ();        = director.getpintwproduct ();        Product1.showproduct ();         = director.getpinthproduct ();        Product2.showproduct ();    }}

Benefits of using the Builder model:

1. Using the builder mode allows the client not to know the details of the internal composition of the product.
2. The concrete builder classes are independent of each other and are very advantageous to the expansion of the system.
3. Since the concrete builders are independent, the construction process can be progressively refined without any impact on the other modules.


When using the construction mode:

1. When creating complex objects, the construction order between the internal components of these objects is stable, but the internal components of the objects face complex changes.
2. The algorithm of the complex object to be created, independent of the object's components, also independent of the Assembly method of the component.


The difference between builder mode and factory model
As we can see, the builder pattern is very similar to the factory model, and in general, the builder model is only a "director class" role that is more than the factory model. In the class diagram of the builder pattern, if the director class is seen as the client of the final call, then the rest of the diagram can be seen as a simple factory pattern.
The builder pattern is typically used to create more complex objects than the factory model, because the object creation process is more complex, so the object creation process is independent of the new class-The Director class. In other words, the factory pattern is to encapsulate the entire creation of objects in the factory class, which is provided by the factory class to the client, and the builder's model typically only provides for the construction of individual components in the product class, and the concrete construction process is delivered to the Director class. The Director class is responsible for assembling individual components into products according to specific rules, and then delivering the assembled product to the client.

Summarize
The builder pattern is similar to the factory model, they are both builder models and the applicable scenarios are similar. In general, if the construction of the product is complex, then use the factory model, if the product is more complex to build, then use the builder mode.

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.