基于创建型模式的“迷宫”构造
来源:互联网 发布:mac os server工具 编辑:程序博客网 时间:2024/05/21 10:36
本文在GOF《设计模式》一书“创建型模式”一章的例子和模式的基础上,将各个创建型模式有机的结合在一起。实现了“迷宫”构造过程的封装,增强了构造不同迷宫的可扩展性。
创建型模式:
抽象工厂
工厂方法
生成器
原型
单件
其中,抽象工厂是由工厂方法组成的,是一组相互关联的工厂方法的集合。
“迷宫”构造类图:
从类图中可以看出各个模式直接的关系,同时为了显示构造好的迷宫图案,还使用了“策略模式”用于画出迷宫(图中的MazeDrawer类层次)。
以下是各个模块的C++实现:
- #ifndef _PART_H_
- #define _PART_H_
- //Part.h
- enum Direction
- {
- East,
- South,
- West,
- North
- };
- class MapSite
- {
- public:
- virtual void Enter() = 0;
- virtual char* GetInfo() = 0;
- };
- class Room:public MapSite
- {
- public:
- Room(int roomNo)
- {
- _roomNo = roomNo;
- for(int i=0; i<4; ++i)
- _sides[i] = 0;
- }
- MapSite* GetSide(Direction d) const
- {
- return _sides[d];
- }
- void SetSide(Direction d, MapSite* ms)
- {
- if(_sides[d] != 0)
- delete _sides[d];
- _sides[d] = ms;
- }
- virtual void Enter()
- {
- }
- virtual char* GetInfo()
- {
- return "Room";
- }
- int RoomNo() const
- {
- return _roomNo;
- }
- private:
- MapSite* _sides[4];
- int _roomNo;
- };
- class Wall:public MapSite
- {
- public:
- Wall()
- {
- }
- virtual void Enter()
- {
- }
- virtual char* GetInfo()
- {
- return "Wall";
- }
- };
- class Door:public MapSite
- {
- public:
- Door(Room* r1 = 0, Room* r2 = 0)
- {
- _room1 = r1;
- _room2 = r2;
- _isOpen = false;
- }
- Room* OtherSideFrom(Room* r)
- {
- if(r == _room1)
- return _room2;
- else if(r == _room2)
- return _room1;
- return 0;
- }
- virtual void Enter()
- {
- }
- virtual char* GetInfo()
- {
- return "Door";
- }
- private:
- Room* _room1;
- Room* _room2;
- bool _isOpen;
- };
- class Spell
- {
- public:
- Spell()
- {}
- };
- class EnchantedRoom: public Room
- {
- public:
- EnchantedRoom(int roomNo, Spell* spell):Room(roomNo)
- {
- _spell = spell;
- }
- virtual void Enter()
- {
- }
- virtual char* GetInfo()
- {
- return "EnchantedRoom";
- }
- private:
- Spell* _spell;
- };
- class DoorNeedingSpell:public Door
- {
- public:
- DoorNeedingSpell(Room* r1, Room* r2):Door(r1, r2)
- {
- }
- virtual void Enter()
- {
- }
- virtual char* GetInfo()
- {
- return "DoorNeedingSpell";
- }
- };
- class RoomWithABomb: public Room
- {
- public:
- RoomWithABomb(int roomNo):Room(roomNo)
- {
- }
- virtual void Enter()
- {
- }
- virtual char* GetInfo()
- {
- return "RoomWithABomb";
- }
- };
- class BombedWall:public Wall
- {
- public:
- BombedWall()
- {
- }
- virtual void Enter()
- {
- }
- virtual char* GetInfo()
- {
- return "BombedWall";
- }
- };
- 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;
- }
- 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;
- }
- ~Maze()
- {
- }
- private:
- Room** _roomList;
- int _xsize;
- int _ysize;
- };
- #endif
/////////////////////////////////////////////////////////////////////////////////////////////////////////
- //PartFactory.h
- #ifndef _PART_FACTORY_H_
- #define _PART_FACTORY_H_
- #include "Part.h"
- #include <string>
- class BombedPartFactory;
- class EnchantedPartFactory;
- class PartFactory
- {
- public:
- static PartFactory* Instance();
- static void SetMazeStyle(std::string style)
- {
- _mazeStyle = style;
- }
- private:
- static std::string _mazeStyle;
- static PartFactory* _instance;
- protected:
- PartFactory(){}
- public:
- virtual Room* MakeRoom(int rno) const
- {
- return new Room(rno);
- }
- virtual Wall* MakeWall() const
- {
- return new Wall();
- }
- virtual Door* MakeDoor(Room* r1, Room* r2) const
- {
- return new Door(r1, r2);
- }
- };
- class EnchantedPartFactory:public PartFactory
- {
- public:
- EnchantedPartFactory(){}
- virtual Room* MakeRoom(int rno) const
- {
- return new EnchantedRoom(rno, CastSpell());
- }
- virtual Door* MakeDoor(Room* r1, Room* r2) const
- {
- return new DoorNeedingSpell(r1, r2);
- }
- protected:
- Spell* CastSpell() const
- {
- return 0;
- }
- };
- class BombedPartFactory:public PartFactory
- {
- public:
- BombedPartFactory(){}
- virtual Room* MakeRoom(int rno) const
- {
- return new RoomWithABomb(rno);
- }
- virtual Wall* MakeWall() const
- {
- return new BombedWall();
- }
- };
- #endif
//////////////////////////////////////////////////////////////////////////
- //PartFactory.cpp
- #include "PartFactory.h"
- PartFactory* PartFactory::_instance = 0;
- std::string PartFactory::_mazeStyle;
- PartFactory* PartFactory::Instance()
- {
- if(_instance == 0)
- {
- if(_mazeStyle == "bombed")
- _instance = new BombedPartFactory();
- else if(_mazeStyle == "enchanted")
- _instance = new EnchantedPartFactory();
- else
- _instance = new PartFactory();
- }
- return _instance;
- }
////////////////////////////////////////////////////////////////////
- //MazeBuilder.h
- #ifndef _MAZE_BUILDER_H_
- #define _MAZE_BUILDER_H_
- #include "PartFactory.h"
- class MazeBuilder
- {
- public:
- MazeBuilder()
- {
- BuildMaze();
- }
- void BuildMaze()
- {
- _maze = new Maze(8, 8);
- _factory = PartFactory::Instance();
- }
- Maze* GetMaze()
- {
- return _maze;
- }
- virtual void BuildRoom(int rno)
- {
- //emtpy
- }
- virtual void BuildDoor(int roomFrom, int roomTo)
- {
- //emtpy
- }
- protected:
- Direction CommonWall(Room* r1, Room* r2)
- {
- int xs = _maze->XSize();
- Direction dir;
- if(r1->RoomNo()+1 == r2->RoomNo())
- dir = East;
- if(r1->RoomNo()-1 == r2->RoomNo())
- dir = West;
- if(r1->RoomNo()-xs == r2->RoomNo())
- dir = North;
- if(r1->RoomNo()+xs == r2->RoomNo())
- dir = South;
- return dir;
- }
- Maze* _maze;
- PartFactory* _factory;
- };
- class StdMazeBuilder:public MazeBuilder
- {
- public:
- StdMazeBuilder(){}
- virtual void BuildRoom(int rno)
- {
- if(!_maze->RoomNo(rno))
- {
- Room* r = _factory->MakeRoom(rno);
- _maze->AddRoom(r);
- r->SetSide(North, _factory->MakeWall());
- r->SetSide(South, _factory->MakeWall());
- r->SetSide(East, _factory->MakeWall());
- r->SetSide(West, _factory->MakeWall());
- }
- }
- virtual void BuildDoor(int roomFrom, int roomTo)
- {
- Room* r1 = _maze->RoomNo(roomFrom);
- Room* r2 = _maze->RoomNo(roomTo);
- Door* d = _factory->MakeDoor(r1, r2);
- r1->SetSide(CommonWall(r1,r2), d);
- r2->SetSide(CommonWall(r2,r1), d);
- }
- };
- #endif
////////////////////////////////////////////////////////////
- //MazeDrawer.h
- #ifndef _MAZE_DRAW_H_
- #define _MAZE_DRAW_H_
- #include "Part.h"
- #include <iostream>
- class MazeDrawer
- {
- public:
- virtual void DrawMaze(Maze* maze)
- {
- for(int i=0; i<maze->Size(); ++i)
- {
- Room* r = maze->RoomNo(i);
- if(r != 0)
- {
- std::cout << r->GetInfo() << " " << r->RoomNo() << "#:" << std::endl;
- std::string dir;
- for(int j=0; j<4; ++j)
- {
- switch(j)
- {
- case East:
- dir = "East";
- break;
- case South:
- dir = "South";
- break;
- case West:
- dir = "West";
- break;
- case North:
- dir = "North";
- break;
- }
- std::cout << " " << dir << ":" << r->GetSide((Direction)j)->GetInfo() << std::endl;
- }
- }
- }
- }
- };
- #endif
//////////////////////////////////////////////////////////////////
- //MazeGame.h
- #ifndef _MAZE_GAME_H_
- #define _MAZE_GAME_H_
- #include "MazeBuilder.h"
- #include "MazeDrawer.h"
- class MazeGame
- {
- public:
- MazeGame(MazeDrawer* md=0)
- {
- _mazeDrawer = md;
- }
- virtual Maze* CreateMaze(MazeBuilder& mb) = 0;
- void DeleteMaze(Maze* maze)
- {}
- void DrawMaze(Maze* maze)
- {
- if(_mazeDrawer != 0)
- _mazeDrawer->DrawMaze(maze);
- }
- private:
- MazeDrawer* _mazeDrawer;
- };
- class Game1:public MazeGame
- {
- public:
- Game1(MazeDrawer* md):MazeGame(md){}
- virtual Maze* CreateMaze(MazeBuilder& mb)
- {
- for(int i=0; i<24; ++i)
- mb.BuildRoom(i);
- mb.BuildDoor(0,1);
- mb.BuildDoor(1,2);
- mb.BuildDoor(2,10);
- mb.BuildDoor(10,11);
- mb.BuildDoor(11,12);
- mb.BuildDoor(12,20);
- mb.BuildDoor(20,21);
- mb.BuildDoor(21,22);
- mb.BuildDoor(22,14);
- mb.BuildDoor(14,15);
- return mb.GetMaze();
- }
- };
- #endif
/////////////////////////////////////////////////////////////////
- // MazeGame.cpp : 定义控制台应用程序的入口点。
- //
- #include "stdafx.h"
- #include "MazeGame.h"
- int _tmain(int argc, _TCHAR* argv[])
- {
- PartFactory::SetMazeStyle("enchanted");
- MazeGame* game = new Game1(new MazeDrawer());
- Maze* maze = game->CreateMaze(StdMazeBuilder());
- game->DrawMaze(maze);
- return 0;
- }
参考文献:GOF《设计模式》
希望通道中人提出意见建议,共同改善完善,共同交流:hm.y@live.cn
- 基于创建型模式的“迷宫”构造
- 《GOF设计模式》—创建型模式—Delphi源码示例:未基于模式的迷宫
- 《GOF设计模式》—创建型模式—Delphi源码示例:基于创建型模式的迷宫
- 设计模式中迷宫的实现,用于学习创建型模式
- Js的基于构造函数模式的原型链模式
- 《GOF设计模式》—生成器(Builder)—Delphi源码示例:基于生成器的迷宫
- 《GOF设计模式》—原型(Prototype)—Delphi源码示例:基于原型的迷宫1
- 《GOF设计模式》—原型(Prototype)—Delphi源码示例:基于原型的迷宫2
- 《GOF设计模式》—单件(Singleton)—Delphi源码示例:基于单件的迷宫
- 对象创建的方法二之------构造函数模式
- js 创建对象的三种方式——工厂模式、构造函数模式、原型模式
- 对象的创建:工厂模式/构造函数模式/原型模式 (笔记)
- JavaScript中创建对象的方法:工厂模式,构造函数模式, 原型模式
- JavaScript中常见的几种创建对象的方式:基于对象扩充属性和方法、基于工厂方式创建对象、基于构造函数和基于原型
- 基于栈操作的迷宫问题求解
- 6.基于STL STACK的 迷宫算法
- JS面向对象的几种创建方式:工厂模式、构造函数模式、原型模式、混合模式、动态原型模式
- 《GOF设计模式》—工厂方法(Factory Method)—Delphi源码示例:基于工厂方法的迷宫
- 网络编程错过很多次,而且不容易发现的原因
- 面试问题1:谈谈你对加班的看法
- socket编程
- linux开发 -- 命令行参数解析 getopt
- Java有能力抵挡LAMP的进攻吗?
- 基于创建型模式的“迷宫”构造
- CBR会议论文集和书籍(Proceedings and Books)
- swing 嘴尖介绍 ~~转帖
- MS SQL查询时自动给数据排名:RANK(),DENSE_RANK()
- VB常用代码总结(二)【转】
- VB常用代码总结(一)【转】
- 获取设备句柄的两种方法
- delhpi 数据库中的图像存储和读取
- 一个简单的加密解密算法