本文按照GOF书中的例子做实验。
创建型模式包含了5种设计模式,书中已创建迷宫的例子讲解了这5个例子,将这5种设计模式都运用到了创建迷宫当中。
这5种创建型模式包含:
1. Factory Method(工厂方法)
2. Abstract Factory(抽象工厂)
3. Builder(建造者)
4. Prototype(原型)
5. Singleton(单例)
以下是代码部分
//maze.h 这是组件类 包含了门,墙和房间,炸弹,施魔法组件
#ifndef PART_H
#define PART_H
#include <iostream>
using namespace std;
enum Direction {North, South , East ,West};
//char c_Direction[4][10] = {"North", "South" , "East" ,"West"};
class MapSite{
public:
virtual void Enter() = 0;
};
class Room :public MapSite{
public:
Room(int roomNo){
_roomNumber = roomNo;
for(int i=0; i<4; ++i){
_sides[i] = 0;
}
cout << "Room : This Room NO is : " << _roomNumber<< endl;
}
~Room(){
cout << "Room : ~Room() " << endl;
}
MapSite* GetSide (Direction d) const{
return _sides[d];
//cout << "Room : This Room Direction is : " << c_Direction[d] << endl;
}
void SetSide(Direction d, MapSite* ms){
if(_sides[d] != 0){
delete _sides[d];
}
_sides[d] = ms;
//cout << "Room : This Room Direction is : " << c_Direction[d] << endl;
}
virtual void Enter(){
cout << "Room : Enter Room " << endl;
}
int RoomNo() const
{
return _roomNumber;
}
private:
MapSite* _sides[4];
int _roomNumber;
};
class Wall : public MapSite{
public:
Wall(){
cout << "Wall : This is a Wall " << endl;
}
~Wall(){
cout << "Wall : ~Wall() " << endl;
}
virtual void Enter(){
cout << "Wall : Enter Room " << endl;
}
};
class Door :public MapSite{
public:
Door(Room* r1= 0, Room* r2=0){
_room1 = r1;
_room2 = r2;
_isopen = false;
cout << "Door :This Door is connect r1 and r2 " << endl;
}
~Door(){
cout << "Door : ~Door() " << endl;
}
Room* OtherSideFrom(Room* r){
if(r == _room1)
return _room2;
else if(r = _room2)
return _room1;
}
virtual void Enter(){
cout << "Door : Enter Room " << endl;
}
private:
Room* _room1;
Room* _room2;
bool _isopen;
};
class Maze{
public:
Maze(int xsize=1, int ysize=1){
if(xsize<=0 || ysize<=0){
xsize = ysize = 1;
}
_xsize = xsize;
_ysize = ysize;
_roomList = new Room*[_xsize*_ysize];
for(int i=0; i<_xsize*_ysize; ++i){
_roomList[i] = 0;
}
cout << "Maze : This is a Maze " << endl;
}
~Maze(){
cout << "Maze : ~Maze() " << endl;
}
void AddRoom(Room* r){
int rno = r->RoomNo();
_roomList[rno] = r;
}
Room* RoomNo(int rno) const{
for(int i=0; i<_xsize*_ysize; ++i)
{
if(_roomList[rno] == 0)
continue;
if(_roomList[rno]->RoomNo() == rno)
return _roomList[rno];
}
return 0;
}
int XSize() const
{
return _xsize;
}
int YSzie() const
{
return _ysize;
}
int Size() const
{
return _xsize*_ysize;
}
private:
int _xsize;
int _ysize;
Room** _roomList;
//...
};
//原始方法创建迷宫
//class MazeGame {
//
//public:
// Maze* CreateMaze(){
// Maze* aMaze = new Maze;
// Room* r1 = new Room(1);
// Room* r2 = new Room(2);
// Door* theDoor = new Door(r1,r2);
//
// aMaze->AddRoom(r1);
// aMaze->AddRoom(r2);
//
// r1->SetSide(North, new Wall);
// r1->SetSide(East, theDoor);
// r1->SetSide(South, new Wall);
// r1->SetSide(West, new Wall);
//
//
// r2->SetSide(North, new Wall);
// r2->SetSide(East, new Wall);
// r2->SetSide(South, new Wall);
// r2->SetSide(West, theDoor );
//
// return aMaze;
// }
//
//};
//施魔法实例类
class Spell{
public:
Spell(){
cout << "Spell : I am Spell ! " << endl;
}
~Spell(){
cout << "Spell : ~Spell() ! " << endl;
}
};
class EnchantedRoom : public Room
{
public:
EnchantedRoom(int roomNo,Spell* spell)
:Room(roomNo){
cout << "EnchantedRoom : EnchantedRoom() " << endl;
_spell = spell;
}
~EnchantedRoom(){
cout << "EnchantedRoom : ~EnchantedRoom() " << endl;
}
virtual void Enter(){
}
private:
Spell* _spell;
};
class DoorNeedingSpell:public Door
{
public:
DoorNeedingSpell(Room* r1, Room* r2):Door(r1, r2){
cout << "DoorNeedingSpell : DoorNeedingSpell()" <<endl;
}
~DoorNeedingSpell(){
cout << "DoorNeedingSpell : ~DoorNeedingSpell()" <<endl;
}
virtual void Enter(){
}
};
class RoomWithABomb: public Room
{
public:
RoomWithABomb(int roomNo):Room(roomNo){
cout << "RoomWithABomb : RoomWithABomb()" << endl;
}
~RoomWithABomb(){
cout << "RoomWithABomb : ~RoomWithABomb()" << endl;
}
virtual void Enter(){
}
};
class BombedWall:public Wall
{
public:
BombedWall(){
cout << "BombedWall : BombedWall()" << endl;
}
~BombedWall(){
cout << "BombedWall : ~BombedWall()" << endl;
}
virtual void Enter(){
}
};
#endif
//mazefactory.h 抽象工厂类,其中工厂实现为单例模式
#ifndef MAZEFACTORY_H
#define MAZEFACTORY_H
#include "maze.h"
//抽象工厂方法创建普通迷宫,只有房子和门,墙
class MazeFactory{
public:
static MazeFactory* Instance();
public:
virtual Maze* MakeMaze() const{
return new Maze;
}
virtual Wall* MakeWall() const{
return new Wall;
}
virtual Room* MakeRoom(int n) const{
return new Room(n);
}
virtual Door* MakeDoor(Room* r1, Room* r2) const{
return new Door(r1,r2);
}
protected: //单例模式
MazeFactory(){
cout << "MazeFactory : MazeFactory()" << endl;
}
~MazeFactory(){
cout << "MazeFactory : ~MazeFactory()" << endl;
}
private:
static MazeFactory* _instance;
};
//创建魔法迷宫
class EnchantedMazeFactory : public MazeFactory
{
public:
EnchantedMazeFactory(){
cout << "EnchantedMazeFactory : EnchantedMazeFactory()" << endl;
}
~EnchantedMazeFactory(){
cout << "EnchantedMazeFactory : ~EnchantedMazeFactory()" << endl;
}
virtual Room* MakeRoom(int n ) const{
return new EnchantedRoom(n, CastSpell());
}
virtual Door* MakeDoor(Room* r1, Room* r2) const{
return new DoorNeedingSpell(r1, r2);
}
protected:
Spell* CastSpell() const{
return 0;
}
};
//创建含有炸弹的迷宫
class BombedMazeFactory : public MazeFactory{
public:
BombedMazeFactory(){
cout << "BombedMazeFactory : BombedMazeFactory()" << endl;
}
~BombedMazeFactory(){
cout << "BombedMazeFactory : ~BombedMazeFactory()" << endl;
}
virtual Room* MakeRoom(int rno ) const{
return new RoomWithABomb(rno);
}
virtual Wall* MakeWall() const {
return new BombedWall();
}
};
class MazeGame {
public:
MazeGame(){
cout << "MazeGame : MazeGame()" << endl;
}
~MazeGame(){
cout << "MazeGame : ~MazeGame()" << endl;
}
Maze* CreateMaze(MazeFactory& factory){
Maze* aMaze = factory.MakeMaze();
Room* r1 = factory.MakeRoom(1);
Room* r2 = factory.MakeRoom(2);
Door* aDoor = factory.MakeDoor(r1,r2);
aMaze->AddRoom(r1);
aMaze->AddRoom(r2);
r1->SetSide(North, factory.MakeWall());
r1->SetSide(East, aDoor);
r1->SetSide(South, factory.MakeWall());
r1->SetSide(West, factory.MakeWall());
r2->SetSide(North, factory.MakeWall());
r2->SetSide(East, factory.MakeWall());
r2->SetSide(South, factory.MakeWall());
r2->SetSide(West, aDoor );
return aMaze;
}
};
#endif
/*mazefactory.cpp 抽象工厂的单例实现,由于工厂可以生产3种不同的迷宫,因此根据用户需求,可以分别创建3种不同的迷宫,包括普通迷宫,施加了魔法的迷宫,和含有炸弹的迷宫*/
#include "mazefactory.h"
//MazeFactory Singleton mode
MazeFactory* MazeFactory::_instance = 0;//定义
MazeFactory* MazeFactory::Instance(){
if(NULL == _instance){
//const char* mazeStyle = getenv("MAZESTYLE");
const char* mazeStyle = "bombed";//此处可以修改以使用不同的子类
if(strcmp(mazeStyle,"bombed") == 0){
_instance = new BombedMazeFactory;
}
else if(strcmp(mazeStyle,"enchanted") == 0){
_instance = new EnchantedMazeFactory;
}
// other possible subclass
else{
_instance = new MazeFactory;
}
}
return _instance;
}
//main.cpp
#include <iostream>
#include "mazefactory.h"
int main()
{
//原始方法创建迷宫
//MazeGame* Maze = new MazeGame();
//Maze->CreateMaze();
//抽象工厂方法创建迷宫
//MazeFactory factory;
//MazeGame* Maze = new MazeGame();
//Maze->CreateMaze(factory);
//delete Maze;
//抽象工厂方法创建含有炸弹的迷宫
//BombedMazeFactory bombfactory;
//MazeGame bobgame;
//bobgame.CreateMaze(bombfactory);
//抽象工厂方法创建含有魔法的迷宫
//EnchantedMazeFactory* enchantedfactory = new EnchantedMazeFactory;
//MazeGame* enchantedgame = new MazeGame;
//enchantedgame->CreateMaze(*enchantedfactory);
//delete enchantedfactory;
//delete enchantedgame;
//单例创建迷宫
MazeFactory* m = MazeFactory::Instance();
MazeGame SingletonMazeGame;
SingletonMazeGame.CreateMaze(*m);
getchar();
return 0;
}
版权声明:本文为博主原创文章,未经博主允许不得转载。
原文地址:http://blog.csdn.net/xiabodan/article/details/47832623