Lecture 4th: builder Mode

Source: Internet
Author: User

2005.11.29 Li Jianzhong

Origin of builder Mode

Suppose you create a house facility in the game. The building of this House consists of several parts and each part must be changeable.

If the most intuitive design method is used, changes in each part of the house will lead to the re-correction of the house building ......

 

Motivation)

In a software system, sometimes it is faced with the creation of "a complex object", which is usually caused by the sub-objects of each partAlgorithmComponents; due to changes in requirements, each part of this complex object is often subject to drastic changes, but the algorithms that combine them are relatively stable.

How to deal with this change? How can we provide a "encapsulation mechanism" to isolate the changes of "various parts of complex objects", so as to keep the "stable Construction Algorithm" in the system from changing with demand?

 

Intent)

Separates the construction of a complex object from its representationSame build processYou can createDifferent Representation.

-- Design Pattern gof

 

Structure)

 

Collaborations)

 

Builder application in game framework

 

System Interface Builder

 

Builder does not need to care about what the specific door looks like and what the wall looks like. It just defines the axis.

Relatively stable system Director

 

Concretebuilder

Client

 

Director and abstract class Builder are tightly bound, but there is no specific concretebuilder information in it. That is to say, ctor will not be changed by specific dependencies. When concretebuilder changes, director does not need to be changed.

In addition, the client needs a new concretebuilder. When the concretebuilder changes, thisProgramYou still need to change the client. Here we can use a dynamic mechanism to avoid this change. We can read the name of the specific concretebuilder class and Assembly name from a configuration file, and then use dynamic reflection, so that the client program will not change any dependencies.

In this way, when concretebuilder needs to be changed, you only need to add a subclass of builder and modify the configuration file.

This is very compliant with the open and closed principle. It is open to expansion and closed to changes.

This also complies with the Dependency inversion principle. The building process of a house is a high-level abstraction, and the specific implementation details of the House depend on high-level abstraction. The high-level abstraction is relatively stable, and the underlying implementation details are quite varied.

 

Key Points of builder Mode

The builder mode is mainly used to "build a complex object by step ". In this example, "step-by-step" is a stable algorithm (namely Director, such as gamemanager in the preceding example), while various parts of complex objects (I .e. concretebuilder) change frequently.

Where is the change point, and where is the encapsulation? The Builder mode is mainly used to cope with frequent changes in requirements of "various parts of complex objects. Its disadvantage is that it is difficult to cope with changes in the demand for "step-by-step algorithm building. (For example, if the House structure changes frequently, this builder mode is meaningless)

The abstractfactory mode solves the demand changes of "series objects", and the builder mode solves the demand changes of "Object part. The builder mode is usually used in combination with the composite mode.

 

Builder application in. NET Framework

In ASP. NET, when we write a page class, this class inherits from system. Web. UI. Page. Page is actually a builder, which is a container. Many methods are called builderpart () methods, such as oninit (), onload (), onprerender (), and render (). They are all virtual methods, we can rewrite them to provide our own implementations. When we compile the DLL in the bin folder, we generate our own concretebuilder instance. In fact, the system uses the page base class.

2010.9.24

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.