开发工具:
文件大小: 187kb
下载次数: 0
上传时间: 2009-12-14
详细说明: 设计模式速查收藏 有代码 还不错 // 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... r eturn 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 StandardCreator: public Creator { public: virtual Product* CreateProduct(); }; /* */ template Product* StandardCreator::CreateProduct () { return new TheProduct; } /* */ class MyProduct : public Product { public: MyProduct(); // ... }; StandardCreator 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; }; /* */ ...展开收缩
(系统自动生成,下载前可以参看下载内容)
下载文件列表
相关说明
- 本站资源为会员上传分享交流与学习,如有侵犯您的权益,请联系我们删除.
- 本站是交换下载平台,提供交流渠道,下载内容来自于网络,除下载问题外,其它问题请自行百度。
- 本站已设置防盗链,请勿用迅雷、QQ旋风等多线程下载软件下载资源,下载后用WinRAR最新版进行解压.
- 如果您发现内容无法下载,请稍后再次尝试;或者到消费记录里找到下载记录反馈给我们.
- 下载后发现下载的内容跟说明不相乎,请到消费记录里找到下载记录反馈给我们,经确认后退回积分.
- 如下载前有疑问,可以通过点击"提供者"的名字,查看对方的联系方式,联系对方咨询.