设计模式之创建型模式

1. 多线程安全的单例模式(double check)

 1 class Locker
 2 {
 3 public:
 4     Locker()
 5     {
 6         pthread_mutex_init(&m_mutex, NULL);
 7     }
 8 
 9     ~Locker()
10     {
11         pthread_mutex_destroy(&m_mutex);
12     }
13 
14     void lock()
15     {
16         pthread_mutex_lock(&m_mutex);
17     }
18 
19     void unlock()
20     {
21         pthread_mutex_unlock(&m_mutex);
22     }
23 
24 private:
25     pthread_mutex_t m_mutex;
26 };
27 
28 template <typename T>
29 class Singleton
30 {
31 private:
32     Singleton()
33     {
34     }
35 
36 public:
37     ~Singleton()
38     {
39     }
40 
41     /**
42      * the only way to get the instance.
43      * double check.
44      */
45     static T* getInstance()
46     {
47         if(NULL == m_instance)//once check
48         {
49             Locker m_lock;
50             m_lock.lock();
51             if(NULL == m_instance)//second check
52             {
53                 m_instance = new T();
54             }
55             m_lock.unlock();
56         }
57         return m_instance;
58     }
59 
60     static void destory()
61     {
62         Locker m_lock;
63         m_lock.lock();
64         if(NULL != m_instance)
65         {
66             delete m_instance;
67             m_instance = NULL;
68         }
69         m_lock.unlock();
70     }
71 
72 private:
73     static T* m_instance;
74 };
75 
76 template<class T>
77 T* Singleton<T>::m_instance = NULL;
View Code

 

一些使用到的类

 1 enum Direction {North, South, East, West};
 2 
 3 class MapSite
 4 {
 5 public:
 6     virtual void Enter() = 0;
 7 };
 8 
 9 class Room: public MapSite
10 {
11 public:
12     Room(int roomNo);
13     MapSite *getSide(Direction) const;
14     void setSide(Direction, MapSite*);
15     virtual void Enter();
16 
17 private:
18     MapSite *_sides[4];
19     int _roomNumber;
20 };
21 
22 class Wall: public MapSite
23 {
24 public:
25     Wall();
26     virtual void Enter();
27 };
28 
29 class Door: public MapSite
30 {
31 public:
32     Door(Room *r1 = 0, Room *r2 = 0);
33 
34     virtual void Enter();
35     Room* otherSideFrom(Room *);
36 private:
37     Room *_room1;
38     Room *_room2;
39     bool _isOpen;
40 };
41 
42 class Maze
43 {
44 public:
45     Maze();
46     void addRoom(Room *);
47     Room* roomNo(int) const;
48 private:
49     //...
50     //
51 };
52 
53 
54 class MazeGame
55 {
56 public:
57     MazeGame();
58     Maze *createMaze();
59 private:
60     //...
61     //
62 };
View Code

 

2. 抽象工厂模式

 

 1 class AbstractProductA
 2 {
 3 
 4 };
 5 
 6 class AbstractProductB
 7 {
 8 
 9 };
10 
11 class ProductA1: public AbstractProductA
12 {
13 
14 };
15 
16 class ProductA2: public AbstractProductA
17 {
18 
19 };
20 
21 class ProductB1: public AbstractProductB
22 {
23 
24 };
25 
26 class ProductB2: public AbstractProductB
27 {
28 
29 };
30 
31 class AbstractFactory
32 {
33 public:
34     AbstractProductA* createProductA();
35     AbstractProductB* createProductB();
36 };
37 
38 class ConcreteFactory1
39 {
40 public:
41     AbstractProductA* createProductA()
42     {
43         return new ProductA1();
44     }
45 
46     AbstractProductB* createProductB()
47     {
48         return ProductB1();
49     }
50     
51 };
52 
53 class ConcreteFactory2
54 {
55 public:
56     AbstractProductA* createProductA()
57     {
58         return ProductA2();
59     }
60 
61     AbstractProductB* createProductB()
62     {
63         return ProductB2();
64     }
65 };
View Code

 

3. 生成器模式

 1 class MazeBuilder
 2 {
 3 public:
 4     virtual void BuildMaze() { }
 5     virtual void BuildRoom(int roomNo) { }
 6     virtual void BuildDoor(int roomFrom, int roomTo) { }
 7 
 8     virtual Maze* getMaze() {
 9         return 0;
10     }
11 protected:
12     MazeBuilder();
13 };
14 
15 class StandardMazeBuilder: public MazeBuilder
16 {
17 public:
18     StandardMazeBuilder()
19     {
20         _currentMaze = 0;
21     }
22     virtual void BuildMaze(){
23         _currentMaze = new Maze();
24     }
25     virtual void BuildRoom(int n)
26     {
27         if(!_currentMaze.roomNo(n))
28         {
29             Room *room = new Room(n);
30             _currentMaze.addRoom(room);
31             room.setSide(North, new Wall());
32             room.setSide(South, new Wall());
33             room.setSide(East, new Wall());
34             room.setSide(West, new Wall());
35         }
36     }
37     virtual void BuildDoor(int n1, int n2)
38     {
39         Room *r1 = _currentMaze.roomNo(n1);
40         Room *r2 = _currentMaze.roomNo(n2);
41         Door *d = new Door(r1, r2);
42         r1.setSide(commonWall(r1, r2), d);
43         r2.setSide(commonWall(r1, r2), d);
44     }
45 
46     virtual Maze* getMaze()
47     {
48         return _currentMaze;
49     }
50 
51 private:
52     Direction commonWall(Room*, Room*);
53     Maze* _currentMaze;
54 };
55 
56 class MazeGame
57 {
58 public:
59     Maze* createMaze(MazeBuilder &builder)
60     {
61         //hider the detail of how to create them.
62         builder.BuildMaze();
63         builder.BuildRoom(1);
64         builder.BuildRoom(2);
65         builder.BuildDoor(1, 2);
66         return builder.getMaze();
67     }
68 
69     Maze* createComplexMaze(MazeBuilder &builder)
70     {
71         builder.BuildMaze();
72         builder.BuildRoom(1);
73         builder.BuildRoom(2);
74         //...
75         builder.BuildRoom(1001);
76         builder.BuildDoor(1, 2);
77         return builder.getMaze();
78     }
79 };
View Code

 

4. 工厂模式

 1 //the factory
 2 class MazeGame
 3 {
 4 public:
 5     MazeGame();
 6     Maze *createMaze();
 7 
 8     //factory methods
 9     virtual Maze* makeMaze() const
10     {
11         return new Maze();
12     }
13     virtual Room* makeRoom(int n) const{
14         return new Room(n);
15     }
16     virtual Wall* makeWall() const {
17         return new Wall();
18     }
19     virtual Door* makeDoor(Room* r1, Room* r2) const
20     {
21         return new Door(r1, r2);
22     }
23 private:
24     //...
25     //
26 };
27 
28 //the factory
29 class BombedMazeGame: public MazeGame
30 {
31 public:
32     BombedMazeGame();
33     virtual Wall* makeWall() const
34     {
35         return new BombedWall();
36     }
37 
38     virtual Room* makeRoom(int n) const
39     {
40         return RoomWithBomb(n);
41     }
42 };
43 
44 //the factory
45 class EnchantedMazeGame: public MazeGame
46 {
47 public:
48     EnchantedMazeGame();
49 
50     virtual Room* makeRoom(int n) const
51     {
52         return new EnchantedRoom(n, CastSpell());
53     }
54 
55     virtual Door* makeDoor(Room *r1, Room *r2) const
56     {
57         return new DoorNeedingSpell(r1, r2);
58     }
59 
60 protected:
61     Spell *CastSpell() const;
62 };
63 
64 
65 Maze* MazeGame::createMaze()
66 {
67     Maze* aMaze = makeMaze();
68     Room *r1 = makeRoom(1);//the room maybe child Room
69     Room *r2 = makeRoom(2);
70     Door *theDoor = makeDoor(r1, r1);//the door maybe child door
71     //..
72     return aMaze;
73 }
View Code

 

5. 原型模式

找工作的时候,我们需要准备简历。假设没有打印设备,因此需手写简历,这些简历的内容都是一样的。这样有个缺陷,如果要修改简历中的某项,那么所有已写好 的简历都要修改,工作量很大。随着科技的进步,出现了打印设备。我们只需手写一份,然后利用打印设备复印多份即可。如果要修改简历中的某项,那么修改原始 的版本就可以了,然后再复印。原始的那份手写稿相当于是一个原型,有了它,就可以通过复印(拷贝)创造出更多的新简历。

 1 class MazePrototypeFactory: public MazeFactory
 2 {
 3 public:
 4     MazePrototypeFactory(Maze* m, Wall* w, Room* r, Door* d)
 5     {
 6         _prototypeMaze = m;
 7         _prototypeWall = w;
 8         _prototypeRoom = r;
 9         _prototypeDoor = d;
10     }
11 
12     virtual Maze* makeMaze() const;
13     virtual Room* makeRoom(int) const;
14     virtual Wall* makeWall() const
15     {
16         return _prototypeWall->Clone();
17     }
18     virtual Door* makeDoor(Room* r1, Room* r2) const
19     {
20         Door *d = _prototypeDoor->Clone();
21         d->Initialize(r1, r2);
22         return d;
23     }
24 
25 private:
26     Maze *_prototypeMaze;
27     Room *_prototypeRoom;
28     Wall *_prototypeWall;
29     Door *_prototypeDoor;
30 };
31 
32 int main()
33 {
34     MazePrototypeFactory simpleMazeFactory(new Maze(), new Wall(), new Room(), new Door());
35     MazePrototypeFactory bombedMazeFactory(new Maze(), new BombedWall(), new RoomWithBomb(), new Door());
36 }
View Code

 

posted @ 2015-04-01 19:16  wiessharling  阅读(187)  评论(0编辑  收藏  举报