您的位置:首页 > 编程语言 > Go语言

笔记: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:
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);
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: