Create schema (c) generating mode (Builder)

Builder mode of origin

Suppose you create a game house House facilities, construction of the house consists of several parts, and each part to be varied.
If you are using the most intuitive design method, change every part of the house, it will lead to re-amend houses built ......

Motivation (Motivation)

In software systems, sometimes it faced "a complex object" to create work, which usually consists of various parts of sub-objects with a certain algorithm; due to changes in demand, the various parts of this complex subject often face dramatic changes, but the algorithm combining them is relatively stable.
How to respond to this change? How to provide a "package mechanism" to isolate the change in "various parts of the complex objects", thereby maintaining the system of "stable construction algorithm" does not change as needs change?

Intent (Intent)

Construction represents the phase separation, so that the same build process can create different representations of a complex object. - "Design Patterns" GoF

Structure (Structure)



Collaboration (Collaborations)

application

Builder do not care what it was like concrete door, the walls look like, it's just the definition of the east-west axis.

public abstract class House
{
}

public abstract class Builder
{
    public abstract void BuildDoor();

    public abstract void BuildWall();

    public abstract House GetHouse();
}

Relative change system portion ConcreteBuilder:

public class RomanHouse : House
{
}

public class RomanHouseBuilder : Builder
{
    public override void BuildDoor()
    {
        throw new Exception("The method or operation is not implemented.");
    }

    public override void BuildWall()
    {
        throw new Exception("The method or operation is not implemented.");
    }

    public override House GetHouse()
    {
        return new RomanHouse();
    }
}
// Other Builder

The system is relatively stable part of the Director :( client)

Director and abstract classes Builder is bound tight, but in it there is no information of any specific ConcreteBuilder. That, Director will not be dependent on the specific change, when ConcreteBuilder changed, Director do not change.

public class GameManager
{
    public static House CreatHouse(Builder builder)
    {
        builder.BuildDoor();
        builder.BuildWall();

        return builder.GetHouse();
    }
}

The client program:

private static void Main(string[] args)
{
    string assemblyName = ConfigurationSettings["BuilderAssebmly"];
    string builderName = ConfigurationSettings["BuilderClass"];

    Assembly assembly = Assembly.Load(assemblyName);
    Type t = assembly.GetType("builderName");
    Builder builder = Activator.CreateInstance(t) as Builder;
    House h = GameManager.CreatHouse(builder);
}

Several points Model Builder

Builder mode is mainly used for "step by step to build a complex object." In this one "step by step" is a stable algorithm (i.e. Director, as in the above example GameManager), and each portion (i.e. ConcreteBuilder) complex objects is constantly changing.
Where is the change in the point, where --Builder encapsulation mode mainly in response to changes in "various parts of the complex object" in frequent demand. The disadvantage is that it is difficult to deal with "step by step build algorithms" demand change. (Such as housing construction if you frequently change, then the Builder pattern does not make sense)
AbstractFactory mode to solve the "series object" needs changes, to address the changing needs of Builder mode "target portion". Composite Pattern Builder mode usually used in combination.

Builder application in the .NET Framework

In ASP.Net, we write in a Page class that inherits from System.Web.UI.Page. In fact, a Page Builder, which is a container. There are many ways it is the so-called BuilderPart () method, for example: OnInit (), OnLoad (), OnPreRender (), Render (), etc., which are virtual methods, we can go to rewrite, to provide our own implementation . When we compiled into dll bin folder, we generate our own ConcreteBuilder an example. The system used was actually Page base class.

Guess you like

Origin www.cnblogs.com/springsnow/p/11303198.html