笔记:Gof设计模式--Builder
2012-05-29 21:56
218 查看
1、意图
Separate the construction of a complex object from its representation the same construction process can create different representations.
2、适应性
Use the builder pattern when
• the algorithm for creating a complex object should be independent of the parts that make up the object and
how they're assembled.
• the construction process must allow different representations for the object that's constructed.
3、结构
[align=left] [/align]
4、示例代码
The MazeBuilder class defines the following interface for building mazes:
The subclass StandardMazeBuilder is an implementation that builds simple mazes. It keeps track of the maze it's building in the variable _currentMaze.
Separate the construction of a complex object from its representation the same construction process can create different representations.
2、适应性
Use the builder pattern when
• the algorithm for creating a complex object should be independent of the parts that make up the object and
how they're assembled.
• the construction process must allow different representations for the object that's constructed.
3、结构
[align=left] [/align]
4、示例代码
The MazeBuilder class defines the following interface for building mazes:
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(); };
The subclass StandardMazeBuilder is an implementation that builds simple mazes. It keeps track of the maze it's building in the variable _currentMaze.
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; };
// The StandardMazeBuilder constructor simply initializes _currentMaze. StandardMazeBuilder::StandardMazeBuilder () { _currentMaze = 0; } void StandardMazeBuilder::BuildMaze () { _currentMaze = new Maze; } Maze* StandardMazeBuilder::GetMaze () { return _currentMaze; }
// The BuildRoom operation creates a room and builds the walls around it: 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); }
Maze* MazeGame::CreateMaze (MazeBuilder& builder) { builder.BuildMaze(); builder.BuildRoom(1); builder.BuildRoom(2); builder.BuildDoor(1, 2); return builder.GetMaze(); }
相关文章推荐
- 设计模式笔记(1 ABSTRACT FACTORY & BUILDER)
- 步步为营 .NET 设计模式学习笔记 十、Builder(建造者模式)
- (6)设计模式笔记[Builder模式]
- 设计模式笔记(二)----Builder模式
- 笔记:Gof设计模式--Abstract Factory
- 笔记:Gof设计模式--Prototype
- 设计模式C++学习笔记之十(Builder建造者模式)
- [设计模式整理笔记 五] 创建者模式(Builder)
- .Net设计模式学习笔记(四):建造者模式(Builder Pattern)
- GoF设计模式学习笔记 第二回 Iterator
- builder设计模式学习笔记
- 设计模式实例(Lua)笔记之四(Builder 模式)
- GOF 设计模式(学习Hander First 设计模式 笔记)
- [设计模式笔记]一. 创建型模式--4.Builder模式(一)
- Android设计模式学习笔记(Builder模式)
- 设计模式(Design Patterns)笔记之Builder模式 yakuu(原作)
- 【转载】 笔记--设计模式精解c++-GoF 23 种设计模式解析
- 设计模式学习笔记八:建造者模式(Builder Pattern)
- 设计模式之Builder 的学习笔记
- Builder生成器(创建型设计模式)面向对象设计模式纵横谈讲座笔记之四