One design pattern every day-7 builder Pattern)

Source: Internet
Author: User
One design pattern every day-7 builder pattern (builder) I. Actual Problems

When discussing the factory method mode, I mentioned an application framework for exporting data, but it does not involve the specific implementation of exporting data. In this case, the builder mode is used to export data to text, XML and other specific formats.

When exporting data in text or XML format, it generally has its own format. For example, the exported file has three parts, the file header, content, and tail.

Ii. Problem Analysis

Regardless of the export format, three parts are required, including the file header, content, and tail, and their content is the same. That is, their construction algorithms are fixed, but the results are different. Can we separate the algorithms (build) from the results (appearance?

Iii. Generator Mode 1. Definition

Separates the construction of a complex object from its representation, so that different representations can be created during the same construction process.

2. Problem splitting

No matter which export format requires the file header, content, and tail, the build process mentioned in the definition is the specific steps of each format are different. Next, use the generator mode to solve the above problems:

3. Class diagram:

Based on the split of the problem, because different export formats have the same construction process (algorithm), we can write the construction header, content, and tail methods as an interface, this interface is implemented to reuse the exported algorithms in different formats.

The class Chart content is relatively simple and will not be described one by one.

Source code:
public interface Builder {    /**     *     * @param content     */    public void buildContent(String content);    /**     *     * @param header     */    public void buildHeader(String header);    /**     *     * @param tail     */    public void buildTail(String tail);    public void getResult();}
Builder Interface
Public class txtbuilder implements builder {public txtbuilder () {} private string header; private string content; private string tail; Public void finalize () throws throwable {}/***** @ Param content */Public void buildcontent (string content) {This. content = content; system. out. println ("construct TXT text content:" + content);}/***** @ Param header */Public void buildheader (string header) {This. header = header; system. out. println ("construct the header content in TXT format:" + header);}/***** @ Param tail */Public void buildtail (string tail) {This. tail = tail; system. out. println ("construct the tail content in TXT format:" + tail);} public void getresult () {system. out. println ("returned results in TXT format:"); system. out. println (header); system. out. println (content); system. out. println (tail );}}
Txtbuilder builder Interface
Public class xmlbuilder implements builder {public xmlbuilder () {} private string header; private string content; private string tail; Public void finalize () throws throwable {}/***** @ Param content */Public void buildcontent (string content) {This. content = content; system. out. println ("construct detailed content in XML format");}/***** @ Param header */Public void buildheader (string header) {This. header = header; system. out. println ("construct header content in XML format");}/***** @ Param tail */Public void buildtail (string tail) {This. tail = tail; system. out. println ("Building tail content in XML format");} public void getresult () {system. out. println ("returned results in XML format"); system. out. println (header); system. out. println (content); system. out. println (tail );}}
Xmlbuilder builder Interface
public class Director {    public Builder m_Builder;    public void finalize() throws Throwable {    }    /**     *      * @param builder     */    public Director(Builder builder){        this.m_Builder = builder;    }    /**     *      * @param tail     * @param content     * @param header     */    public void construct(String tail, String content, String header){        this.m_Builder.buildHeader(header);        this.m_Builder.buildContent(content);        this.m_Builder.buildTail(tail);    }}
Ctor, used to guide how to construct
Public class client {public static void main (string ARGs []) {builder xmlbuilder = new xmlbuilder (); builder txtbuilder = new txtbuilder (); Director ctor = new direxml( xmlbuilder ); director. construct ("I am the header", "I am the content", "I am the tail"); xmlbuilder. getresult ();}}
Client client 4. Mode 1. Mode idea

Generator ModeThe construction process is unified and fixed,The changed part is placed in the generator part., As longIf you configure different generators, you can build different products in the same build process.

Important:

1. The generator mode consists of two parts:Component construction and assembly, overall Construction Algorithm.
2. Separate the changed part and the unchanged part.

Of course, in actual projects, the use of generators is often not so simple. After all, the generator mode is used to construct complex objects.

2. Interaction between mentor and Generator

In the generator mode, the interaction between the mentor and the generator is completed through the buildpart method of the generator. In the previous example, the mentor and the generator do not have much interaction, in real project development, the mentor usually implements complicated algorithms or computation processes. In practice, there may be some situations:

  • When running the instructor, the operations will be performed according to the steps of the overall build algorithm. You may first run the operations of the first few steps. In a certain step, you need to create a specific part object, then, call the builder to create a specific part. At the same time, pass the data obtained from the previous operation to the builder, because the data may be required when components are created and assembled in the builder.
  • After the builder creates a specific part object, it returns the object to the instructor. The instructor continues subsequent algorithm operations and may use the object already created.
  • The entire build algorithm has been completed.

From the above description, we can see that there must be interaction between the mentor and the generator. This is only one of the possible situations. How to implement it depends on the specific needs, this depends on the project. You only need to grasp the idea of the design model.

V. Summary

Generator nature:Separate the overall construction algorithm and Component Structure.

 

Blog writing is not easy, reprinted with the source: http://www.cnblogs.com/xiemubg/p/6107517.html

One design pattern every day-7 builder Pattern)

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.