Design Mode note generator (Builder) mode Builder

Source: Internet
Author: User

Design Mode note generator (Builder) mode Builder

 

 

// Builder mode --- object Creation Mode

 

/*

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

2: Motivation

3: Applicability:

1> when creating complex objects, algorithms should be independent of the components of the objects and their assembly methods.

2> when the constructor must allow different representations of the object to be constructed.

4: structure:

Director:

Builder -----------------------------> Builder:

Construct () BuildPart ()

{For all objects in structure |

{Builder-> BuildPart ()} |

ConcreteBuilder:---> Product

BuildPart ()

GetResult ()

5: participants:

1> Builder: specify an abstract interface for each part of a Product object.

2> ConcreteBuilder:

1) Implement the Builder interface to construct and assemble each part of the product.

2) Define and define the representation it creates.

3) provides an interface for searching products.

3> Director: constructs an object using the Builder interface.

4> Product:

1) indicates a complex object to be constructed. ConcreteBuilder creates an internal representation of the product and defines its assembly process.

2) a class that includes defining components, including the interfaces for assembling these components into the final product.

6: collaboration:

1> the customer creates a ctor object and configures it with the Builder object it wants.

2> once the product component is generated, the guide will notify the generator.

3> the generator processes the request from the guide and adds parts to the product.

4> the customer retrieves the product from the generator. (Get product)

The following figure shows the collaboration between the Builder and Director and the customer:

AClient aDirector aConcreteBuilder

|

New ConcreteBuilder ----------------------------> |

New Director (aCon...) ---> |

|

Construct () ------------> | BuildPartA () ----------> |

| BuildPartB () -----------> |

| BuildPartC () -----------> |

|

GetResult () ------------------------------------> |

|

 

7: effect:

1> it allows you to change the internal representation of a product.

Builder only provides abstract interfaces for ctor to hide the internal assembly process, so you want to change

Internally, you only need to define a new Builder.

2> it separates the constructor code from the representation code.

ConcreteBuilder already provides all the code for creating a product. Different ctor can reuse it.

Build different products based on the same part set.

3> it allows you to control the construction process more precisely.

Unlike the creation mode of a production product, the Builder mode constructs a product in the next step under the control of the Guide.

Therefore, the construction process can be controlled more precisely.

8: Implementation:

1> there must be an abstract Builder class that provides Director with interfaces created for each part of the product. ConcreteBuilder

You only need to perform operations on the part that you are interested in.

2> assemble and construct interfaces: Builder interfaces must be common enough.

One thing to consider is that PartA objects may be used when the Builder creates PartB,

Therefore, you can return a PartA object from PartA, and then pass in PartB by Director.

3> why is there no abstract class for a product? Generally, the representations of products generated by the survival tool differ greatly, making it difficult to provide public parent classes.

9: Sample Code :*/

 

// Define an abstract class. All interfaces are empty operations and are not declared as pure virtual functions. Because ConcreteBuilder only needs to implement the interfaces that you are interested in.

Class MazeBuilder

{

Public:

Virtual void BuildMaze (){}

Virtual void BuildRoom (int room ){}

Virtual void BuildDoor (int roomFrom, int roomTo ){}

Virtual Maze * GetMaze () {return 0 ;}

Protected:

MazeBuilder ();

};

 

// Director, create a maze based on the input Builder

Maze * MazeGame: CreateMaze (MazeBuilder & builder)

{

// How to create the file here, without the need to pay attention to the implementation details at the bottom

Builder. BuildMaze ();

Builder. BuildRoom (1 );

Builder. BuildRoom (2 );

Builder. BuildDoor (1, 2 );

 

Return builder. GetMaze ();

}

 

 

Class StandardMazeBuilder: public MazeBuilder

{

Public:

StandardMazeBuilder ();

 

Virtual void BuildMaze ();

Virtual void BuildRoom (int );

Virtual void BuildDoor (int, int );

Virtual Maze * GetMaze ();

 

Private:

Direction CommonWall (Room *, Room *);

Maze * _ currentMaze;

};

 

 

// Specific builder

StandardMazeBuilder: StandardMazeBuilder ()

{

_ CurrentMaze = 0;

}

 

Void StandardMazeBuilder: BuildMaze ()

{

_ CurrentMaze = new Maze;

}

 

Maze * StandardMazeBuilder: GetMaze ()

{

Return _ currentMaze;

}

 

Void StandardMazeBuilder: BuildRoom (int n)

{

If (! _ CurrentMaze-> RoomNo (n ))

{

Room * room = new Room (n );

_ CurrentMaze-> AddRoom (room );

 

Room-> SetSide (North, new Wall );

Room-> SetSide (South, new Wall );

Room-> SetSide (East, new Wall );

Room-> SetSide (West, new Wall );

}

}

 

Void StandardMazeBuilder: BuildDoor (int n1, int n2)

{

Room * r1 = _ currentMaze-> RoomNo (n1 );

Room * r2 = _ currentMaze-> roomNo (n2 );

Door * d = new Door (r1, r2 );

 

R1-> SetSide (CommonWall (r1, r2), d );

R2-> SetSide (CommonWall (r2, r1), d );

}

 

// If we need a door that will pop up, we can do the following.

Void StandardMazeBuilder: BuildBoomedDoor (int n1, int n2)

{

Room * r1 = _ currentMaze-> RoomNo (n1 );

Room * r2 = _ currentMaze-> roomNo (n2 );

Door * d = new BoomedDoor (r1, r2 );

 

R1-> SetSide (CommonWall (r1, r2), d );

// It is better to implement CommonWall as a room member function, and r1.CommonWall (r2) is better understood.

R2-> SetSide (CommonWall (r2, r1), d );

}

 

// Very simple

 

Maze * maze; // Product

MazeGame game; // Director

StandardMazeBuilder builder; // Builder

 

Game. CreateMaze (builder );

Maze = builder. GetMaze ();

// The preceding two sentences can be combined:

Maze = game. CreateMaze (builder); // because GetMaze has been called in the create implementation

 

Class CountingMazeBuilder: public MazeBuilder

{

Public:

CountingMazeBuilder ();

 

Virtual void BuildMaze ();

Virtual void BuildRoom (int );

Virtual void BuildDoor (int, int );

Virtual void AddWall (int, Director );

 

Void GetCounts (int &, int &) const;

 

Private:

Int _ doors;

Int _ rooms;

};

 

CountingMazeBuilder: CountingMazeBuilder (): _ rooms (0), _ doors (0)

{

// _ Rooms = _ doors = 0; the initialization list should be used.

}

 

Inline void CountingMazeBuilder: BuildRoom (int)

{

++ _ Rooms;

}

 

Inline void CountingMazeBuilder: BuildDoor (int)

{

++ _ Doors;

}

 

Inline CountingMazeBuilder: GetCounts (int & rooms, int & doors) const

{

Rooms = _ rooms;

Doors = _ doors;

}

// This is a counting builder. If you need to count, you can simply put it in the base class. If you want to use it, call the method of the base class once.

 

 

 

 

 

 

 

 

 

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.