The builder mode of creational mode

Source: Internet
Author: User

1. Intentions

Separates the construction of a complex object from its representation so that the same build process can create different representations.

2. Aliases

No

3. Motivation

An RTF (Rich Text Format) document Interchange Format reader should be able to convert RTF to a variety of body formats. The reader can convert RTF documents into different ASCII text or into a body window component that can be edited interactively. But the problem is that the number of possible conversions is unlimited. This makes it easy to increase the number of new conversions without changing the RTF reader.

4. Applicability

Use the builder mode for the following situations:

    • When creating complex objects, the algorithm should be independent of the parts of the object and the way they are assembled.
    • When the construction process must allow the constructed object to have different representations.
5. Structure

The factory class pattern provides the pattern of creating a single class, while the builder pattern is to centralize the various products to manage, to create composite objects, so-called composite objects that refer to a class that has different properties, in fact the builder pattern is the combination of the previous abstract factory pattern and the last Test. Let's look at the code:

6. Code Examples

As before, a sender interface, two implementation classes MailSender and Smssender. At last:
Implementation code:
First, create a common interface for both:

publicinterface Sender {    publicvoidSend();}

Second, create the implementation class:

 Public  class mailsender implements Sender {    @Override     Public void Send() {System.out.println ("This is mailsender!"); }} Public  class smssender implements Sender {    @Override     Public void Send() {System.out.println ("This is SMS sender!"); }}

Finally, the Builder class:

 Public classBuilder {Privatelist<sender> list =NewArraylist<sender> (); Public void Producemailsender(intCount) { for(intI=0; i<count; i++) {List.add (NewMailSender ()); }    } Public void Producesmssender(intCount) { for(intI=0; i<count; i++) {List.add (NewSmssender ()); }    }}

Test class:

publicclass Test {    publicstaticvoidmain(String[] args) {        new Builder();        builder.produceMailSender(10);    }}

From this point of view, the builder pattern integrates many functions into a class that can create more complex things. So the difference with the engineering model is that the factory model is concerned with creating a single product, while the builder pattern is concerned with creating a conforming object, multiple parts. Therefore, the choice of the factory model or the builder model depends on the actual situation.

7. Related Modes
    • Abstract Factory is similar to builder because it can also create complex objects. The main difference is that the builder model focuses on constructing a complex object step by step. Abstract factory focuses on multiple series of product objects (simple or complex). Builder returns the product in the final step, and for the abstract factory, the product returns immediately.
    • Composite are usually built with builder.

Reference:

Http://openhome.cc/Gossip/DesignPattern/DecoratorPattern.htm
Http://item.jd.com/10057319.html
http://blog.csdn.net/zhangerqing/article/details/8239539

The builder mode of creational 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.