笔记:Gof设计模式--Prototype
2012-05-29 22:50
155 查看
1、意图
Specify the kinds of objects to create using a prototypical instance, and create new objects by copying this prototype.
2、适应性
Use the Prototype pattern when a system should be independent of how its products are created, composed, and represented; and
• when the classes to instantiate are specified at run-time, for example, by dynamic loading; or
• to avoid building a class hierarchy of factories that parallels the class hierarchy of products; or
• when instances of a class can have one of only a few different combinations of state. It may be more convenient to install a corresponding number of prototypes and clone them rather
than instantiating the class manually, each time with the appropriate state.
3、结构
4、示例代码
We'll define a MazePrototypeFactory subclass of the MazeFactory class. MazePrototypeFactory will be initialized with prototypes of the objects it will create so that we don't have to subclass it just to change
the classes of walls or rooms it creates.
MazePrototypeFactory augments the MazeFactory interface with a constructor that takes the prototypes as arguments:
The member functions for creating walls, rooms, and doors are similar: Each clones a prototype and then initializes it. Here are the definitions of MakeWall and MakeDoor:
We can use MazePrototypeFactory to create a prototypical or default maze just by initializing it with prototypes of basic maze components:
An object that can be used as a prototype, such as an instance of Wall, must support the Clone operation. It must also have a copy constructor for cloning.
Specify the kinds of objects to create using a prototypical instance, and create new objects by copying this prototype.
2、适应性
Use the Prototype pattern when a system should be independent of how its products are created, composed, and represented; and
• when the classes to instantiate are specified at run-time, for example, by dynamic loading; or
• to avoid building a class hierarchy of factories that parallels the class hierarchy of products; or
• when instances of a class can have one of only a few different combinations of state. It may be more convenient to install a corresponding number of prototypes and clone them rather
than instantiating the class manually, each time with the appropriate state.
3、结构
4、示例代码
We'll define a MazePrototypeFactory subclass of the MazeFactory class. MazePrototypeFactory will be initialized with prototypes of the objects it will create so that we don't have to subclass it just to change
the classes of walls or rooms it creates.
MazePrototypeFactory augments the MazeFactory interface with a constructor that takes the prototypes as arguments:
class MazePrototypeFactory : public MazeFactory { public: MazePrototypeFactory(Maze*, Wall*, Room*, Door*); virtual Maze* MakeMaze() const; virtual Room* MakeRoom(int) const; virtual Wall* MakeWall() const; virtual Door* MakeDoor(Room*, Room*) const; private: Maze* _prototypeMaze; Room* _prototypeRoom; Wall* _prototypeWall; Door* _prototypeDoor; };
// The new constructor simply initializes its prototypes: MazePrototypeFactory::MazePrototypeFactory ( Maze* m, Wall* w, Room* r, Door* d ) { _prototypeMaze = m; _prototypeWall = w; _prototypeRoom = r; _prototypeDoor = d; }
The member functions for creating walls, rooms, and doors are similar: Each clones a prototype and then initializes it. Here are the definitions of MakeWall and MakeDoor:
Wall* MazePrototypeFactory::MakeWall () const { return _prototypeWall->Clone(); }
Door* MazePrototypeFactory::MakeDoor (Room* r1, Room *r2) const { Door* door = _prototypeDoor->Clone(); door->Initialize(r1, r2); return door; }
We can use MazePrototypeFactory to create a prototypical or default maze just by initializing it with prototypes of basic maze components:
MazeGame game; MazePrototypeFactory simpleMazeFactory( new Maze, new Wall, new Room, new Door ); Maze* maze = game.CreateMaze(simpleMazeFactory);
An object that can be used as a prototype, such as an instance of Wall, must support the Clone operation. It must also have a copy constructor for cloning.
class Door : public MapSite { public: Door(); Door(const Door&); virtual void Initialize(Room*, Room*); virtual Door* Clone() const; virtual void Enter(); Room* OtherSideFrom(Room*); private: Room* _room1; Room* _room2; };
Door::Door (const Door& other) { _room1 = other._room1; _room2 = other._room2; } void Door::Initialize (Room* r1, Room* r2) { _room1 = r1; _room2 = r2; } Door* Door::Clone () const { return new Door(*this); }
// The BombedWall subclass must override Clone and implement a corresponding // copy constructor. class BombedWall : public Wall { public: BombedWall(); BombedWall(const BombedWall&); virtual Wall* Clone() const; bool HasBomb(); private: bool _bomb; }; BombedWall::BombedWall (const BombedWall& other) : Wall(other) { _bomb = other._bomb; } Wall* BombedWall::Clone () const { return new BombedWall(*this); }
相关文章推荐
- 笔记:Gof设计模式--Flyweight
- 笔记:Gof设计模式--Strategy
- 笔记--设计模式精解c++-GoF 23 种设计模式解析
- C#面向对象设计模式学习笔记(5) - Prototype 原型模式(创建型模式)
- 设计模式学习笔记(六)——Prototype原型模式
- 笔记:Gof设计模式--Façade
- 笔记:Gof设计模式--Template Method
- 【HeadFirst 设计模式学习笔记】18 原型(Prototype)模式拾零
- HeadFirst 设计模式学习笔记19--原型(Prototype)模式拾零
- GOF设计模式阅读笔记之行为模型(上)
- GoF设计模式学习笔记 第二回 Iterator
- GOF 23 设计模式之 原型模式(prototype) 深复制与浅复制举例
- 笔记:Gof设计模式--Factory Method
- C# 中的设计模式2:Prototype(学习笔记)
- 笔记:Gof设计模式--Adapter
- 笔记:Gof设计模式--Composite
- 笔记:Gof设计模式--Memento
- [设计模式整理笔记 七] 原型模式(ProtoType)
- 笔记:Gof设计模式--Singleton
- 笔记:Gof设计模式--Mediator