设计模式中创建型模式 (二):原型模式
2008-04-17 13:03
190 查看
Prototype(原型模式)
简单的讲,就是一种克隆模式,使得一个系统独立于它的产品的创建、构成和表示。
它有和Abstract Factory和Builder一样的效果:它对客户隐藏了具体的产品类。另外,Prototype模式还有另外一些优点:
1、运行时刻增加和删除产品
2、用类动态配置应用。
它的主要缺陷是每一个Prototype的子类都必需实现Clone操作,这可能很困难。例如,当所考虑的类已经存在时就难以新增Clone操作。
当内部包括一些不支持拷贝或有循环引用的对象时,实现克隆可能也会很困难的。
我们将定义MazeFactory的子类MazePrototypeFactory.该子类将使用它要创建的对象的原型来为初始化,这样我们就不需要仅仅为了改变它所
创建的墙壁或房间的类而生子类了。
以下为示例代码:(VC6+sp6)下测试通过
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
//MapSite.h
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
#pragma warning(disable: 4786)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
#include <vector>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
#include <map>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
#include <iostream>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
using namespace std;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
//##ModelId=48059C280115
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
enum Direction
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
North, South, East, West
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)
};
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)
/**///////////////////////////////////////////////////////////////////////////
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
//申明所有类
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
class Door;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
class MapSite;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
class Maze;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
class MazeBuilder;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
class MazeFactory;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
class MazeGame;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
class Room;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
class StandardMazeBuilder;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
class Wall;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
class MazePrototypeFactory;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)
/**///////////////////////////////////////////////////////////////////////////
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
//##ModelId=48059C2800E6
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
class MapSite
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
public:
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
//##ModelId=48059C280105
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
virtual void Enter() = 0;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)
};
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
//##ModelId=48059C280172
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
class Wall: public MapSite
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
public:
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
//##ModelId=48059C280183
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
Wall()...{}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
virtual Wall* Clone() const;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
//##ModelId=48059C280184
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
virtual void Enter()...{printf("Wall::Enter() "); }
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)
};
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
//##ModelId=48059C280134
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
class Room: public MapSite
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
public:
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
//##ModelId=48059C280144
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
Room (int roomNo = 0)...{ _roomNumber = roomNo; }
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
//##ModelId=48059C280146
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
MapSite* GetSide(Direction d)const...{ return _sides[d]; }
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
//##ModelId=48059C280154
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
void SetSide (Direction d, MapSite* s)...{ _sides[d] = s; }
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
Room(const Room& other);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
virtual Room* Clone();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
virtual void Initialize(int n);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
//##ModelId=48059C280157
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
virtual void Enter()...{ printf("Room::Enter() "); }
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
//##ModelId=48059C280159
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
int GetRoomNo()const...{ return _roomNumber; }
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
private:
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
//##ModelId=48059C280164
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
MapSite* _sides[4];
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
//##ModelId=48059C280168
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
int _roomNumber;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)
};
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
//##ModelId=48059C280191
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
class Door: public MapSite
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
public:
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
//##ModelId=48059C2801A1
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
Door(Room* r1= 0, Room* r2= 0)...{ _room1 = r1;_room2 = r2; }
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
Door(const Door& other);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
virtual void Initialize(Room *r1, Room* r2);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
virtual Door* Clone() const;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
//##ModelId=48059C2801A4
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
virtual void Enter()...{ printf("Door::Enter() "); };
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
//##ModelId=48059C2801A6
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
Room* OtherSideFrom(Room*);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
private:
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
//##ModelId=48059C2801A9
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
Room * _room1;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
//##ModelId=48059C2801B2
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
Room * _room2;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
//##ModelId=48059C2801B6
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
bool _isOpen;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)
};
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
//##ModelId=48059C2801C0
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
class Maze
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
public:
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
//##ModelId=48059C2801D0
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
Maze()...{ }
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
Maze(const Maze& other);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
virtual Maze* Clone();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
//##ModelId=48059C2801D1
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
void AddRoom(Room* r)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
maproom[r->GetRoomNo()] = r;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
//##ModelId=48059C2801D3
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
Room* RoomNo(int no)...{ return maproom[no]; }
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
private:
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
//##ModelId=48059C2801EF
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
map<int, Room*> maproom;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
//
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)
};
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
//建造迷宫的程序将MazeFactory作为一个参数,这样程序员就能指定要创建的房间,
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
//墙壁和门等类
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
//##ModelId=48059C2801FF
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
class MazeFactory
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
public:
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
//##ModelId=48059C280200
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
MazeFactory()...{}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
//##ModelId=48059C280201
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
virtual Maze* MakeMaze()const
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
return new Maze;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
//##ModelId=48059C280203
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
virtual Wall* MakeWall()const
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
return new Wall;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
//##ModelId=48059C28020F
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
virtual Room* MakeRoom(int n)const
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
return new Room(n);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
//##ModelId=48059C280212
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
virtual Door* MakeDoor(Room* r1, Room* r2)const
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
return new Door(r1, r2);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)
};
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
//创建各个部件,作用类似于MazeGame::CreateMaze();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
//##ModelId=48059C28021E
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
class MazeBuilder
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
public:
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
//##ModelId=48059C28021F
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
virtual void BuildMaze()...{}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
//##ModelId=48059C280221
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
virtual void BuildRoom(int room)...{}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
//##ModelId=48059C28022F
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
virtual void BuildDoor(int roomFrom,int roomTo)...{}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
//##ModelId=48059C280233
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
virtual Maze* GetMaze()...{ return 0; }
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
protected:
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
//##ModelId=48059C280235
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
MazeBuilder()...{}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)
};
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
class StandardMazeBuilder: public MazeBuilder
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
public:
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
StandardMazeBuilder()...{ _currentMaze = 0; }
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
virtual void BuildMaze()...{ _currentMaze = (Maze*) new StandardMazeBuilder; }
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
virtual void BuildRoom(int);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
virtual void BuildDoor(int,int);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
virtual Maze* GetMaze()...{ return _currentMaze; }
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
private:
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
Direction CommonWall(Room*, Room*);//决定哪两个相邻的墙可以有门。
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
Maze* _currentMaze;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)
};
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
//创建迷宫的类
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
//##ModelId=48059C28023D
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
class MazeGame: public Maze
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
public:
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
//##ModelId=48059C28023F
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
Maze* CreateMaze();//普通方法创建迷宫,硬编码方式
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
//##ModelId=48059C28024D
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
Maze* CreateMaze2(MazeFactory& factory);//工厂类方式建造
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
Maze* CreateMaze3(StandardMazeBuilder& builder);//builder模式,建分部
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
Maze* CreateMaze4(MazePrototypeFactory& prototype);//原型模式
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
//##ModelId=48059C28024F
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
void printMaze()
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
//好像比较麻烦
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)
};
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
class MazePrototypeFactory: public MazeFactory
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
public:
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
MazePrototypeFactory(Maze* m,Wall* w, Room* r, Door* d)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
_prototypeMaze = m;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
_prototypeRoom = r;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
_prototypeWall = w;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
_prototypeDoor = d;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
virtual Maze* MakeMaze() const;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
virtual Room* MakeRoom(int n) const;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
virtual Wall* MakeWall() const;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
virtual Door* MakeDoor(Room* r1, Room* r2) const;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
private:
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
Maze* _prototypeMaze;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
Room* _prototypeRoom;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
Wall* _prototypeWall;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
Door* _prototypeDoor;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)
};
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
// MapSite.cpp : Defines the entry point for the console application.
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
//
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
#include "stdafx.h"
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
#include "MapSite.h"
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
Wall* Wall::Clone() const
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
return new Wall(*this);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
Room::Room(const Room& other)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
this->_roomNumber = other._roomNumber;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
memcpy(this->_sides,other._sides,sizeof(_sides));
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
Room* Room::Clone()
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
return new Room(*this);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
void Room::Initialize(int n)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
this->_roomNumber = n;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
Door::Door(const Door& other)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
_room1 = other._room1;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
_room2 = other._room2;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
void Door::Initialize(Room *r1, Room* r2)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
_room1 = r1;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
_room2 = r2;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
Door* Door::Clone() const
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
return new Door(*this);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
Maze::Maze(const Maze& other)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
maproom.clear();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
//maproom.insert(other.maproom.begin(),other.maproom.end());
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
map<int, Room*>::const_iterator it;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
it = other.maproom.begin();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
for ( ;it != other.maproom.end(); it++)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
maproom.insert(*it);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
Maze* Maze::Clone()
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
return new Maze(*this);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
void StandardMazeBuilder::BuildRoom(int n)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
if(!_currentMaze->RoomNo(n))
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
Room* room = new Room(n);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
_currentMaze->AddRoom(room);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
room->SetSide(North, new Wall);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
room->SetSide(South, new Wall);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
room->SetSide(East, new Wall);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
room->SetSide(West, new Wall);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
void StandardMazeBuilder::BuildDoor(int n1, int n2)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
Room* r1 = _currentMaze->RoomNo(n1);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
Room* r2 = _currentMaze->RoomNo(n2);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
Door* d = new Door(r1, r2);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
r1->SetSide(CommonWall(r1, r2), d);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
r2->SetSide(CommonWall(r2, r1), d);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
Direction StandardMazeBuilder::CommonWall(Room* r1, Room* r2)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
if(r1 == NULL || r2 == NULL )
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
return North;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
if(r1->GetSide(North) == r2->GetSide(South))
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
return North;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
if(r1->GetSide(South) == r2->GetSide(North))
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
return South;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
if(r1->GetSide(West) == r2->GetSide(East))
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
return West;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
if(r1->GetSide(East) == r2->GetSide(West))
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
return East;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
return North;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
//##ModelId=48059C28023F
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
Maze* MazeGame::CreateMaze()
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
Maze* aMaze = new Maze;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
Room* r1 = new Room(1);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
Room* r2 = new Room(2);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
Door* theDoor = new Door(r1,r2);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
aMaze->AddRoom(r1);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
aMaze->AddRoom(r2);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
r1->SetSide(North, new Wall);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
r1->SetSide(East, theDoor);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
r1->SetSide(South, new Wall);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
r1->SetSide(West, new Wall);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
r2->SetSide(North, new Wall);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
r2->SetSide(East, new Wall);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
r2->SetSide(South, new Wall);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
r2->SetSide(West, theDoor);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
return aMaze;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
//##ModelId=48059C28024D
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
Maze* MazeGame::CreateMaze2(MazeFactory& factory)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
Maze* aMaze = factory.MakeMaze();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
Room* r1 = factory.MakeRoom(1);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
Room* r2 = factory.MakeRoom(2);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
Door* aDoor = factory.MakeDoor(r1, r2);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
aMaze->AddRoom(r1);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
aMaze->AddRoom(r2);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
r1->SetSide(North, factory.MakeWall());
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
r1->SetSide(East, aDoor);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
r1->SetSide(South, factory.MakeWall());
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
r1->SetSide(West, factory.MakeWall());
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
r2->SetSide(North, factory.MakeWall());
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
r2->SetSide(East, factory.MakeWall());
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
r2->SetSide(South, factory.MakeWall());
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
r2->SetSide(West, aDoor);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
return aMaze;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
Maze* MazeGame::CreateMaze3(StandardMazeBuilder& builder)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
builder.BuildMaze();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
builder.BuildRoom(1);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
builder.BuildRoom(2);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
builder.BuildDoor(1, 2);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
return builder.GetMaze();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
Maze* MazeGame::CreateMaze4(MazePrototypeFactory& prototype)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
Maze* aMaze = prototype.MakeMaze();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
Room* r1 = prototype.MakeRoom(1);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
Room* r2 = prototype.MakeRoom(2);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
Door* aDoor = prototype.MakeDoor(r1, r2);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
aMaze->AddRoom(r1);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
aMaze->AddRoom(r2);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
r1->SetSide(North, prototype.MakeWall());
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
r1->SetSide(East, aDoor);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
r1->SetSide(South, prototype.MakeWall());
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
r1->SetSide(West, prototype.MakeWall());
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
r2->SetSide(North, prototype.MakeWall());
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
r2->SetSide(East, prototype.MakeWall());
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
r2->SetSide(South, prototype.MakeWall());
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
r2->SetSide(West, aDoor);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
return aMaze;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
Maze* MazePrototypeFactory::MakeMaze() const
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
return _prototypeMaze->Clone();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
Wall* MazePrototypeFactory::MakeWall() const
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
return _prototypeWall->Clone();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
Room* MazePrototypeFactory::MakeRoom(int n) const
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
Room* room = _prototypeRoom->Clone();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
room->Initialize(n);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
return room;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
Door* MazePrototypeFactory::MakeDoor (Room* r1, Room* r2) const
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
Door* door = _prototypeDoor->Clone();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
door->Initialize(r1, r2);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
return door;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
// testMapSite.cpp : Defines the entry point for the console application.
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
//
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
#include "stdafx.h"
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
#include "MapSite.h"
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
int main(int argc, char* argv[])
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
printf("Hello World! ");
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
MazeGame game;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
MazePrototypeFactory simpleMazeFactory(new Maze, new Wall, new Room, new Door);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
Maze* maze = game.CreateMaze4(simpleMazeFactory);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
return 0;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)
}
简单的讲,就是一种克隆模式,使得一个系统独立于它的产品的创建、构成和表示。
它有和Abstract Factory和Builder一样的效果:它对客户隐藏了具体的产品类。另外,Prototype模式还有另外一些优点:
1、运行时刻增加和删除产品
2、用类动态配置应用。
它的主要缺陷是每一个Prototype的子类都必需实现Clone操作,这可能很困难。例如,当所考虑的类已经存在时就难以新增Clone操作。
当内部包括一些不支持拷贝或有循环引用的对象时,实现克隆可能也会很困难的。
我们将定义MazeFactory的子类MazePrototypeFactory.该子类将使用它要创建的对象的原型来为初始化,这样我们就不需要仅仅为了改变它所
创建的墙壁或房间的类而生子类了。
以下为示例代码:(VC6+sp6)下测试通过
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
//MapSite.h
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
#pragma warning(disable: 4786)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
#include <vector>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
#include <map>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
#include <iostream>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
using namespace std;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
//##ModelId=48059C280115
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
enum Direction
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
North, South, East, West
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)
};
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)
/**///////////////////////////////////////////////////////////////////////////
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
//申明所有类
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
class Door;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
class MapSite;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
class Maze;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
class MazeBuilder;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
class MazeFactory;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
class MazeGame;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
class Room;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
class StandardMazeBuilder;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
class Wall;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
class MazePrototypeFactory;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)
/**///////////////////////////////////////////////////////////////////////////
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
//##ModelId=48059C2800E6
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
class MapSite
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
public:
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
//##ModelId=48059C280105
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
virtual void Enter() = 0;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)
};
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
//##ModelId=48059C280172
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
class Wall: public MapSite
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
public:
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
//##ModelId=48059C280183
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
Wall()...{}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
virtual Wall* Clone() const;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
//##ModelId=48059C280184
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
virtual void Enter()...{printf("Wall::Enter() "); }
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)
};
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
//##ModelId=48059C280134
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
class Room: public MapSite
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
public:
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
//##ModelId=48059C280144
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
Room (int roomNo = 0)...{ _roomNumber = roomNo; }
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
//##ModelId=48059C280146
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
MapSite* GetSide(Direction d)const...{ return _sides[d]; }
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
//##ModelId=48059C280154
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
void SetSide (Direction d, MapSite* s)...{ _sides[d] = s; }
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
Room(const Room& other);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
virtual Room* Clone();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
virtual void Initialize(int n);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
//##ModelId=48059C280157
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
virtual void Enter()...{ printf("Room::Enter() "); }
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
//##ModelId=48059C280159
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
int GetRoomNo()const...{ return _roomNumber; }
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
private:
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
//##ModelId=48059C280164
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
MapSite* _sides[4];
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
//##ModelId=48059C280168
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
int _roomNumber;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)
};
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
//##ModelId=48059C280191
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
class Door: public MapSite
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
public:
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
//##ModelId=48059C2801A1
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
Door(Room* r1= 0, Room* r2= 0)...{ _room1 = r1;_room2 = r2; }
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
Door(const Door& other);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
virtual void Initialize(Room *r1, Room* r2);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
virtual Door* Clone() const;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
//##ModelId=48059C2801A4
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
virtual void Enter()...{ printf("Door::Enter() "); };
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
//##ModelId=48059C2801A6
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
Room* OtherSideFrom(Room*);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
private:
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
//##ModelId=48059C2801A9
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
Room * _room1;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
//##ModelId=48059C2801B2
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
Room * _room2;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
//##ModelId=48059C2801B6
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
bool _isOpen;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)
};
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
//##ModelId=48059C2801C0
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
class Maze
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
public:
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
//##ModelId=48059C2801D0
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
Maze()...{ }
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
Maze(const Maze& other);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
virtual Maze* Clone();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
//##ModelId=48059C2801D1
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
void AddRoom(Room* r)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
maproom[r->GetRoomNo()] = r;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
//##ModelId=48059C2801D3
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
Room* RoomNo(int no)...{ return maproom[no]; }
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
private:
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
//##ModelId=48059C2801EF
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
map<int, Room*> maproom;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
//
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)
};
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
//建造迷宫的程序将MazeFactory作为一个参数,这样程序员就能指定要创建的房间,
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
//墙壁和门等类
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
//##ModelId=48059C2801FF
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
class MazeFactory
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
public:
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
//##ModelId=48059C280200
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
MazeFactory()...{}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
//##ModelId=48059C280201
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
virtual Maze* MakeMaze()const
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
return new Maze;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
//##ModelId=48059C280203
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
virtual Wall* MakeWall()const
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
return new Wall;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
//##ModelId=48059C28020F
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
virtual Room* MakeRoom(int n)const
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
return new Room(n);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
//##ModelId=48059C280212
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
virtual Door* MakeDoor(Room* r1, Room* r2)const
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
return new Door(r1, r2);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)
};
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
//创建各个部件,作用类似于MazeGame::CreateMaze();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
//##ModelId=48059C28021E
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
class MazeBuilder
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
public:
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
//##ModelId=48059C28021F
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
virtual void BuildMaze()...{}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
//##ModelId=48059C280221
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
virtual void BuildRoom(int room)...{}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
//##ModelId=48059C28022F
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
virtual void BuildDoor(int roomFrom,int roomTo)...{}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
//##ModelId=48059C280233
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
virtual Maze* GetMaze()...{ return 0; }
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
protected:
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
//##ModelId=48059C280235
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
MazeBuilder()...{}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)
};
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
class StandardMazeBuilder: public MazeBuilder
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
public:
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
StandardMazeBuilder()...{ _currentMaze = 0; }
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
virtual void BuildMaze()...{ _currentMaze = (Maze*) new StandardMazeBuilder; }
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
virtual void BuildRoom(int);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
virtual void BuildDoor(int,int);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
virtual Maze* GetMaze()...{ return _currentMaze; }
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
private:
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
Direction CommonWall(Room*, Room*);//决定哪两个相邻的墙可以有门。
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
Maze* _currentMaze;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)
};
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
//创建迷宫的类
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
//##ModelId=48059C28023D
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
class MazeGame: public Maze
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
public:
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
//##ModelId=48059C28023F
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
Maze* CreateMaze();//普通方法创建迷宫,硬编码方式
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
//##ModelId=48059C28024D
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
Maze* CreateMaze2(MazeFactory& factory);//工厂类方式建造
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
Maze* CreateMaze3(StandardMazeBuilder& builder);//builder模式,建分部
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
Maze* CreateMaze4(MazePrototypeFactory& prototype);//原型模式
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
//##ModelId=48059C28024F
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
void printMaze()
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
//好像比较麻烦
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)
};
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
class MazePrototypeFactory: public MazeFactory
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
public:
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
MazePrototypeFactory(Maze* m,Wall* w, Room* r, Door* d)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
_prototypeMaze = m;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
_prototypeRoom = r;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
_prototypeWall = w;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
_prototypeDoor = d;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
virtual Maze* MakeMaze() const;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
virtual Room* MakeRoom(int n) const;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
virtual Wall* MakeWall() const;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
virtual Door* MakeDoor(Room* r1, Room* r2) const;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
private:
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
Maze* _prototypeMaze;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
Room* _prototypeRoom;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
Wall* _prototypeWall;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
Door* _prototypeDoor;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)
};
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
// MapSite.cpp : Defines the entry point for the console application.
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
//
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
#include "stdafx.h"
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
#include "MapSite.h"
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
Wall* Wall::Clone() const
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
return new Wall(*this);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
Room::Room(const Room& other)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
this->_roomNumber = other._roomNumber;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
memcpy(this->_sides,other._sides,sizeof(_sides));
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
Room* Room::Clone()
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
return new Room(*this);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
void Room::Initialize(int n)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
this->_roomNumber = n;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
Door::Door(const Door& other)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
_room1 = other._room1;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
_room2 = other._room2;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
void Door::Initialize(Room *r1, Room* r2)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
_room1 = r1;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
_room2 = r2;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
Door* Door::Clone() const
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
return new Door(*this);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
Maze::Maze(const Maze& other)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
maproom.clear();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
//maproom.insert(other.maproom.begin(),other.maproom.end());
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
map<int, Room*>::const_iterator it;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
it = other.maproom.begin();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
for ( ;it != other.maproom.end(); it++)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
maproom.insert(*it);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
Maze* Maze::Clone()
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
return new Maze(*this);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
void StandardMazeBuilder::BuildRoom(int n)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
if(!_currentMaze->RoomNo(n))
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
Room* room = new Room(n);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
_currentMaze->AddRoom(room);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
room->SetSide(North, new Wall);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
room->SetSide(South, new Wall);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
room->SetSide(East, new Wall);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
room->SetSide(West, new Wall);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
void StandardMazeBuilder::BuildDoor(int n1, int n2)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
Room* r1 = _currentMaze->RoomNo(n1);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
Room* r2 = _currentMaze->RoomNo(n2);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
Door* d = new Door(r1, r2);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
r1->SetSide(CommonWall(r1, r2), d);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
r2->SetSide(CommonWall(r2, r1), d);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
Direction StandardMazeBuilder::CommonWall(Room* r1, Room* r2)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
if(r1 == NULL || r2 == NULL )
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
return North;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
if(r1->GetSide(North) == r2->GetSide(South))
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
return North;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
if(r1->GetSide(South) == r2->GetSide(North))
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
return South;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
if(r1->GetSide(West) == r2->GetSide(East))
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
return West;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
if(r1->GetSide(East) == r2->GetSide(West))
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
return East;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
return North;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
//##ModelId=48059C28023F
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
Maze* MazeGame::CreateMaze()
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
Maze* aMaze = new Maze;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
Room* r1 = new Room(1);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
Room* r2 = new Room(2);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
Door* theDoor = new Door(r1,r2);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
aMaze->AddRoom(r1);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
aMaze->AddRoom(r2);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
r1->SetSide(North, new Wall);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
r1->SetSide(East, theDoor);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
r1->SetSide(South, new Wall);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
r1->SetSide(West, new Wall);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
r2->SetSide(North, new Wall);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
r2->SetSide(East, new Wall);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
r2->SetSide(South, new Wall);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
r2->SetSide(West, theDoor);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
return aMaze;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
//##ModelId=48059C28024D
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
Maze* MazeGame::CreateMaze2(MazeFactory& factory)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
Maze* aMaze = factory.MakeMaze();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
Room* r1 = factory.MakeRoom(1);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
Room* r2 = factory.MakeRoom(2);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
Door* aDoor = factory.MakeDoor(r1, r2);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
aMaze->AddRoom(r1);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
aMaze->AddRoom(r2);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
r1->SetSide(North, factory.MakeWall());
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
r1->SetSide(East, aDoor);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
r1->SetSide(South, factory.MakeWall());
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
r1->SetSide(West, factory.MakeWall());
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
r2->SetSide(North, factory.MakeWall());
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
r2->SetSide(East, factory.MakeWall());
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
r2->SetSide(South, factory.MakeWall());
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
r2->SetSide(West, aDoor);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
return aMaze;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
Maze* MazeGame::CreateMaze3(StandardMazeBuilder& builder)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
builder.BuildMaze();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
builder.BuildRoom(1);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
builder.BuildRoom(2);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
builder.BuildDoor(1, 2);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
return builder.GetMaze();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
Maze* MazeGame::CreateMaze4(MazePrototypeFactory& prototype)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
Maze* aMaze = prototype.MakeMaze();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
Room* r1 = prototype.MakeRoom(1);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
Room* r2 = prototype.MakeRoom(2);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
Door* aDoor = prototype.MakeDoor(r1, r2);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
aMaze->AddRoom(r1);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
aMaze->AddRoom(r2);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
r1->SetSide(North, prototype.MakeWall());
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
r1->SetSide(East, aDoor);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
r1->SetSide(South, prototype.MakeWall());
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
r1->SetSide(West, prototype.MakeWall());
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
r2->SetSide(North, prototype.MakeWall());
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
r2->SetSide(East, prototype.MakeWall());
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
r2->SetSide(South, prototype.MakeWall());
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
r2->SetSide(West, aDoor);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
return aMaze;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
Maze* MazePrototypeFactory::MakeMaze() const
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
return _prototypeMaze->Clone();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
Wall* MazePrototypeFactory::MakeWall() const
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
return _prototypeWall->Clone();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
Room* MazePrototypeFactory::MakeRoom(int n) const
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
Room* room = _prototypeRoom->Clone();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
room->Initialize(n);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
return room;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
Door* MazePrototypeFactory::MakeDoor (Room* r1, Room* r2) const
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
Door* door = _prototypeDoor->Clone();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
door->Initialize(r1, r2);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
return door;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
// testMapSite.cpp : Defines the entry point for the console application.
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
//
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
#include "stdafx.h"
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
#include "MapSite.h"
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
int main(int argc, char* argv[])
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
printf("Hello World! ");
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
MazeGame game;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
MazePrototypeFactory simpleMazeFactory(new Maze, new Wall, new Room, new Door);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
Maze* maze = game.CreateMaze4(simpleMazeFactory);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
return 0;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
相关文章推荐
- 【设计模式】创建型模式之原型Prototype
- 设计模式笔记(6)---原型模式(创建型)
- PHP设计模式:创建型之原型模式(Prototype)
- 设计模式(四)原型模式Prototype(创建型)
- 【设计模式】创建型模式-原型模式
- 【转】设计模式——创建型设计模式总结(简单工厂、普通工厂、抽象工厂、建造者、原型和单例)
- 设计模式(2)-创建型-原型模式(Prototype)
- 设计模式学习之原型模式(Prototype,创建型模式)(5)
- 【设计模式学习笔记五】【创建型模式】【原型模式(Prototype)】
- JAVA设计模式(05):创建型-原型模式(Prototype)
- (4)设计模式之原型模式(创建型模式)
- 设计模式笔记 5.Prototype 原型模式(创建型模式)
- 设计模式--原型模式Prototype(创建型)
- 设计模式-创建型模式-原型
- 设计模式--创建型模式--原型模式(Protype Pattern)
- 创建型设计模式-原型模式
- JAVA设计模式(05):创建型-原型模式(Prototype)
- 设计模式-创建型之原型模式
- 设计模式---原型模式Prototype(创建型)
- 设计模式笔记(6)---原型模式(创建型)