Java design pattern-constructor pattern (Builder)

Source: Internet
Author: User

Java design pattern-constructor pattern (Builder)

The build mode is the object creation mode. The construction mode separates the internal objects of a product from the production process of the product, so that a building process can generate product objects with different internal representations.
Object Construction:
In some cases, an object has some important properties and cannot be used as a complete product until they have no proper values. For example, an email contains the sender address, recipient address, subject, content, and appendix. However, this email cannot be sent before the minimum recipient address is assigned a value.
In some cases, some properties of an object must be assigned values in a certain order to make sense. Before a property is assigned a value, another property cannot be assigned a value. These situations make the construction of nature involve complex business logic. At this time, this object is equivalent to a product to be built, and these properties are equivalent to the parts of the product, the process of building the product is the process of building parts. Due to the complex process of building parts, the construction process of these parts is often "Externalized" to another object called builder, the builder object returned to the client is a product object where all parts have been built.
The construction model creates all the parts one by one using a director object and a specific builder object to create a complete product object. The builder mode hides the structure of the product and the construction process of the parts of the product from the client, and separates the responsibility for directing the construction process from the responsibility of the specific parts of the builder, achieve responsibility division and encapsulation.
Constructor mode structure:
Signature + uPggs/bSu7j2s + signature/ztcS497j21 + signature/O1xMrHvt/Signature/A4LHY0OvKtc/Signature + dKqx/Signature + signature/cS/0 + signature/gt/Signature + signature + example/gudi1xNK70KnA4KOsy/examples + examples/examples + templates/M5daqyra1xMrHvt/examples + examples/ region + region + u/region + tcS9qNTsx + region/Fybj4vt/region + region/T0NK7uPay + region + DTprXEvt/region/cS/tcTB47z + o6y2 + MO/ 09DSu7j2weO8/r7Nz + DTprXY1NrL + dPQtcS9qNTs1d + 9x8mrwO/T0NK7uPa9qNTst723qKGjPGJyIC8 + DQq0 + placement = "brush: java; ">Public class ConcreteBuilder implements Builder {private Product product = new Product ();/*** Product part construction method 1 */public void builderPart1 () {System. out. println ("build the first part"); product. setPart1 ("id: 123");}/*** product part construction method 2 */public void builderPart2 () {System. out. println ("build second part"); product. setPart2 ("Name: transformers");}/*** Product return Method */public product retrieveResult () {return Product ;}}

Public class Director {/*** holding the currently used constructor object */private Builder builder; public Director (Builder builder) {super (); this. builder = builder;} public void construct () {builder. builderPart1 (); builder. builderPart2 ();}}
public static void main(String[] args) {        Builder builder = new ConcreteBuilder();        Director director = new Director(builder);        director.construct();        Product product = builder.retrieveResult();        System.out.println(product.getPart1()+" "+product.getPart2());    }

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.