设计模式迷你手册 chm文件
设计模式速查收藏 有代码 还不错 // example /* */ class Product {}; #ifdef Implementation1 class MyProduct : public Product {}; class YourProduct : public Product {}; class TheirProduct : public Product {}; typedef int ProductId; const int MINE = 1; const int YOURS = 2; const int THEIRS = 2; /* */ class Creator { public: virtual Product* Create(ProductId); }; /* */ Product* Creator::Create (ProductId id) { if (id == MINE) return new MyProduct; if (id == YOURS) return new YourProduct; // repeat for remaining products... return 0; } /* */ class MyCreator : public Creator { public: virtual Product* Create(ProductId); }; /* */ Product* MyCreator::Create (ProductId id) { if (id == YOURS) return new MyProduct; if (id == MINE) return new YourProduct; // N.B.: switched YOURS and MINE if (id == THEIRS) return new TheirProduct; return Creator::Create(id); // called if all others fail } /* */ #endif #ifdef Implementation2 /* */ class Creator { public: Product* GetProduct(); protected: virtual Product* CreateProduct(); private: Product* _product; }; /* */ Product* Creator::GetProduct () { if (_product == 0) { _product = CreateProduct(); } return _product; } /* */ #endif #ifdef Implementation3 /* */ class Creator { public: virtual Product* CreateProduct() = 0; }; /* */ template <class TheProduct> class StandardCreator: public Creator { public: virtual Product* CreateProduct(); }; /* */ template <class TheProduct> Product* StandardCreator<TheProduct>::CreateProduct () { return new TheProduct; } /* */ class MyProduct : public Product { public: MyProduct(); // ... }; StandardCreator<MyProduct> myCreator; /* */ #endif /* */ #include "C++/MazeParts.H" /* */ class MazeGame { public: Maze* CreateMaze(); /* */ // factory methods: /* */ virtual Maze* MakeMaze() const { return new Maze; } virtual Room* MakeRoom(int n) const { return new Room(n); } virtual Wall* MakeWall() const { return new Wall; } virtual Door* MakeDoor(Room* r1, Room* r2) const { return new Door(r1, r2); } }; /* */ Maze* MazeGame::CreateMaze () { Maze* aMaze = MakeMaze(); /* */ Room* r1 = MakeRoom(1); Room* r2 = MakeRoom(2); Door* theDoor = MakeDoor(r1, r2); /* */ aMaze->AddRoom(r1); aMaze->AddRoom(r2); /* */ r1->SetSide(North, MakeWall()); r1->SetSide(East, theDoor); r1->SetSide(South, MakeWall()); r1->SetSide(West, MakeWall()); /* */ r2->SetSide(North, MakeWall()); r2->SetSide(East, MakeWall()); r2->SetSide(South, MakeWall()); r2->SetSide(West, theDoor); /* */ return aMaze; } /* */ class BombedMazeGame : public MazeGame { public: BombedMazeGame(); /* */ virtual Wall* MakeWall() const { return new BombedWall; } /* */ virtual Room* MakeRoom(int n) const { return new RoomWithABomb(n); } }; /* */ class EnchantedMazeGame : public MazeGame { public: EnchantedMazeGame(); /* */ 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; }; /* */
- 粉丝: 0
- 资源: 8
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
最新资源
- 基于pygame实现的烟花代码
- mcu-printf关于51单片机使用printf函数进行串口调试的方法
- MySQL和数据表操作
- 微信小程序面试题.pdf
- 基于matlab实现电力系统仿真计算软件包,包括潮流计算,最优潮流计算等.rar
- 基于matlab实现电力系统各种故障波形仿真,单相接地故障,两相间短路,两相接地短路,三相短路等.rar
- 基于matlab实现电动汽车动力性,爬坡性,续驶里程等性能仿真.rar
- Python动态烟花代码.pdf
- 基于matlab实现串口发送接收数据 可配置端口,波特率等 发送可选择ASCII方式或HEX方式
- matlab基于BP神经网络手写字母识别(单一).zip代码9