daily pastebin goal
86%
SHARE
TWEET

Untitled

a guest Jul 12th, 2018 57 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. class CUIFrameWork
  2. {
  3. public:
  4.     // Instead of hard coding we write factory methods which
  5.     // perform the task of object creation.
  6.     virtual CDataComponent* MakeDataComp()
  7.     {
  8.         return new CDataComponent();
  9.     }
  10.  
  11.     virtual CUIComponent* MakeUIComp()
  12.     {
  13.         return new CUIComponent();
  14.     }
  15.  
  16.     virtual CToolBarComponent* MakeToolBarComp( UINT nID )
  17.     {
  18.         return new CToolBarComponent( nID );
  19.     }
  20.  
  21.     CUITemplate* CreateUI()
  22.     {
  23.         CDataComponent* pData = MakeDataComp();
  24.         CUIComponent* pUI = MakeUIComp();
  25.         CToolBarComponent* pTooBar1 = MakeToolBarComp( ID_STANDARD );
  26.         CToolBarComponent* pTooBar2 = MakeToolBarComp( ID_CUSTOM );
  27.         pTooBar2->AddDropDownButton();
  28.         pTooBar2->AddComboBox();
  29.  
  30.         pUI->AddToolBar(pTooBar1);
  31.         pUI->AddToolBar(pTooBar2);
  32.  
  33.         return new CUITemplate( pData, pUI );
  34.     }
  35. };
  36.    
  37. struct pa_diddle { virtual ~pa_diddle(); virtual void diddle() = 0; };
  38.  
  39. struct bo_diddle : pa_diddle { void diddle() { bo(); }};
  40. struct lee_diddle : pa_diddle { void diddle() { lee(); }};
  41.  
  42. struct diddle_builder
  43. {
  44.   enum name { BO, LEE };
  45.  
  46.   pa_diddle * build_diddle(name n)
  47.   {
  48.     switch(n)
  49.     {
  50.     case BO:  return new bo_diddle();
  51.     case LEE: return new lee_diddle();
  52.     }
  53.   }
  54. };
  55.    
  56. struct some_abstraction { ... };
  57.  
  58. struct some_class
  59. {
  60.   ... functions and stuff ...
  61.  
  62.   struct something_only_some_class_knows_about : some_abstraction {};
  63.   some_abstraction* create_whatnot() const { return new something....about; }
  64. };
  65.    
  66. /*
  67. */
  68. class Product {};
  69.  
  70. #ifdef Implementation1
  71. class MyProduct : public Product {};
  72. class YourProduct : public Product {};
  73. class TheirProduct : public Product {};
  74. typedef int ProductId;
  75. const int MINE = 1;
  76. const int YOURS = 2;
  77. const int THEIRS = 2;
  78. /*
  79. */
  80. class Creator {
  81. public:
  82.     virtual Product* Create(ProductId);
  83. };
  84. /*
  85. */
  86. Product* Creator::Create (ProductId id) {
  87.     if (id == MINE)  return new MyProduct;
  88.     if (id == YOURS) return new YourProduct;
  89.     // repeat for remaining products...
  90.  
  91.     return 0;
  92. }
  93. /*
  94. */
  95. class MyCreator : public Creator {
  96. public:
  97.     virtual Product* Create(ProductId);
  98. };
  99. /*
  100. */
  101. Product* MyCreator::Create (ProductId id) {
  102.     if (id == YOURS)  return new MyProduct;
  103.     if (id == MINE)   return new YourProduct;
  104.         // N.B.: switched YOURS and MINE
  105.  
  106.     if (id == THEIRS) return new TheirProduct;
  107.  
  108.     return Creator::Create(id); // called if all others fail
  109. }
  110. /*
  111. */
  112. #endif
  113. #ifdef Implementation2
  114. /*
  115. */
  116. class Creator {
  117. public:
  118.     Product* GetProduct();
  119. protected:
  120.     virtual Product* CreateProduct();
  121. private:
  122.     Product* _product;
  123. };
  124. /*
  125. */
  126. Product* Creator::GetProduct () {
  127.     if (_product == 0) {
  128.         _product = CreateProduct();
  129.     }
  130.     return _product;
  131. }
  132. /*
  133. */
  134. #endif
  135. #ifdef Implementation3
  136. /*
  137. */
  138. class Creator {
  139. public:
  140.     virtual Product* CreateProduct() = 0;
  141. };
  142. /*
  143. */
  144. template <class TheProduct>
  145. class StandardCreator: public Creator {
  146. public:
  147.     virtual Product* CreateProduct();
  148. };
  149. /*
  150. */
  151. template <class TheProduct>
  152. Product* StandardCreator<TheProduct>::CreateProduct () {
  153.     return new TheProduct;
  154. }
  155. /*
  156. */
  157. class MyProduct : public Product {
  158. public:
  159.     MyProduct();
  160.     // ...
  161. };
  162.  
  163. StandardCreator<MyProduct> myCreator;
  164. /*
  165. */
  166. #endif
  167. /*
  168. */
  169. #include "C++/MazeParts.H"
  170. /*
  171. */
  172. class MazeGame {
  173. public:
  174.     Maze* CreateMaze();
  175. /*
  176. */
  177. // factory methods:
  178. /*
  179. */
  180.     virtual Maze* MakeMaze() const
  181.         { return new Maze; }
  182.     virtual Room* MakeRoom(int n) const
  183.         { return new Room(n); }
  184.     virtual Wall* MakeWall() const
  185.         { return new Wall; }
  186.     virtual Door* MakeDoor(Room* r1, Room* r2) const
  187.         { return new Door(r1, r2); }
  188. };
  189. /*
  190. */
  191. Maze* MazeGame::CreateMaze () {
  192.     Maze* aMaze = MakeMaze();
  193. /*
  194. */
  195.     Room* r1 = MakeRoom(1);
  196.     Room* r2 = MakeRoom(2);
  197.     Door* theDoor = MakeDoor(r1, r2);
  198. /*
  199. */
  200.     aMaze->AddRoom(r1);
  201.     aMaze->AddRoom(r2);
  202. /*
  203. */
  204.     r1->SetSide(North, MakeWall());
  205.     r1->SetSide(East, theDoor);
  206.     r1->SetSide(South, MakeWall());
  207.     r1->SetSide(West, MakeWall());
  208. /*
  209. */
  210.     r2->SetSide(North, MakeWall());
  211.     r2->SetSide(East, MakeWall());
  212.     r2->SetSide(South, MakeWall());
  213.     r2->SetSide(West, theDoor);
  214. /*
  215. */
  216.     return aMaze;
  217. }
  218. /*
  219. */
  220. class BombedMazeGame : public MazeGame {
  221. public:
  222.     BombedMazeGame();
  223. /*
  224. */
  225.     virtual Wall* MakeWall() const
  226.         { return new BombedWall; }
  227. /*
  228. */
  229.     virtual Room* MakeRoom(int n) const
  230.         { return new RoomWithABomb(n); }
  231. };
  232. /*
  233. */
  234. class EnchantedMazeGame : public MazeGame {
  235. public:
  236.     EnchantedMazeGame();
  237. /*
  238. */
  239.     virtual Room* MakeRoom(int n) const
  240.         { return new EnchantedRoom(n, CastSpell()); }
  241. /*
  242. */
  243.     virtual Door* MakeDoor(Room* r1, Room* r2) const
  244.         { return new DoorNeedingSpell(r1, r2); }
  245. protected:
  246.     Spell* CastSpell() const;
  247. };
  248. /*
  249. */
  250.    
  251. //Mazeparts.h #included file
  252.  
  253. #ifndef MazeParts_H
  254. #define MazeParts_H
  255.  
  256. #include "defs.H"
  257.  
  258. enum Direction { North, East, South, West };
  259. #ifndef MapSite_H
  260. #define MapSite_H
  261.  
  262. class MapSite {
  263. public:
  264.     virtual void Enter() = 0;
  265. };
  266.  
  267. #endif
  268. #ifndef _H
  269. #define _H
  270.  
  271. class Room : public MapSite {
  272. public:
  273.     Room(int = 0);
  274.     Room(const Room&);
  275.  
  276.     virtual Room* Clone() const;
  277.     void InitializeRoomNo(int);
  278.  
  279.     MapSite* GetSide(Direction);
  280.     void SetSide(Direction, MapSite*);
  281.  
  282.     virtual void Enter();
  283. private:
  284.     MapSite* _sides[4];
  285.     int _roomNumber;
  286. };
  287. #endif
  288. #ifndef Wall_H
  289. #define Wall_H
  290.  
  291. class Wall : public MapSite {
  292. public:
  293.  
  294.     Wall();
  295.     Wall(const Wall&);
  296.     virtual Wall* Clone() const;
  297.     virtual void Enter();
  298. };
  299. #endif
  300. #ifndef Door_H
  301. #define Door_H
  302.  
  303. class Door : public MapSite {
  304. public:
  305.     Door(Room* = 0, Room* = 0);
  306.     Door(const Room&);
  307.  
  308.     virtual Door* Clone() const;
  309.     void Initialize(Room*, Room*);
  310.  
  311.     virtual void Enter();
  312.     Room* OtherSideFrom(Room*);
  313. private:
  314.     Room* _room1;
  315.     Room* _room2;
  316.     bool _isOpen;
  317. };
  318. #endif
  319. #ifndef Maze_H
  320. #define Maze_H
  321.  
  322. class Maze {
  323. public:
  324.     Maze();
  325.     Maze(const Maze&);
  326.     Room* RoomNo(int);
  327.     void AddRoom(Room*);
  328.  
  329.     virtual Maze* Clone() const;
  330. private:
  331.     // ...
  332. };
  333. #endif
  334. #ifndef BombedWall_H
  335. #define BombedWall_H
  336.  
  337. class BombedWall : public Wall {
  338. public:
  339.     BombedWall(bool bombed = false);
  340.     BombedWall(const BombedWall&);
  341.  
  342.     virtual Wall* Clone() const;
  343.     void Intialize(bool);
  344.  
  345.     virtual void Enter();
  346. private:
  347.     bool _bomb;
  348. };
  349. #endif
  350. #ifndef RoomWithABomb_H
  351. #define RoomWithABomb_H
  352.  
  353. class RoomWithABomb: public Room {
  354. public:
  355.     RoomWithABomb(int = 0, bool bombed = false);
  356.     RoomWithABomb(const RoomWithABomb&);
  357.     bool HasBomb();
  358. private:
  359.     bool _bomb;
  360. };
  361.  
  362. #endif
  363. #ifndef EnchantedRoom_H
  364. #define EnchantedRoom_H
  365.  
  366. class Spell;
  367.  
  368. class EnchantedRoom : public Room {
  369. public:
  370.     EnchantedRoom(int, Spell* = 0);
  371.     EnchantedRoom(const EnchantedRoom&);
  372.     bool HasSpell();
  373.     Spell PickUpSpell();
  374. private:
  375.     Spell* _spell;
  376. };
  377.  
  378. #endif
  379. #ifndef DoorNeedingSpell_H
  380. #define DoorNeedingSpell_H
  381.  
  382. class  DoorNeedingSpell : public Door {
  383. public:
  384.     DoorNeedingSpell(Room*, Room*);
  385.     DoorNeedingSpell(const DoorNeedingSpell&);
  386.     bool TrySpell(Spell);
  387. };
  388. #endif
  389.  
  390.  
  391. #endif
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top