随笔分类 -  Design Pattern

摘要:面试中的Singleton引子 “请写一个Singleton。”面试官微笑着和我说。 “这可真简单。”我心里想着,并在白板上写下了下面的Singleton实现: 1 class Singleton 2 { 3 public: 4 static Singleton& Instance() 5 { 6 static Singleton singleton; 7 return singleton; 8 } 9 10 private:11 Singleton() { };12 }; “那请你讲解一下该实现的各组成。”面试官的脸上仍然... 阅读全文
posted @ 2012-08-19 13:48 freewater 阅读(264) 评论(0) 推荐(0)
摘要:1 #ifndef FACTORYMETHOD_H 2 #define FACTORYMETHOD_H 3 4 #include<iostream> 5 using namespace std; 6 7 class Product//should be a pure virtual class. 8 { 9 public:10 Product(){}11 ~Product(){}12 };13 14 class ConcreteProductA:public Product15 {16 public:17 ConcreteProductA()18 {19 ... 阅读全文
posted @ 2012-06-25 15:45 freewater 阅读(328) 评论(0) 推荐(0)
摘要:装饰者模式(Decorator Pattern):动态地给一个对象添加一些额外的职责,就增加功能来说,装饰者模式比生成子类更加灵活。 1 #ifndef DECORATOR_H 2 #define DECORATOR_H 3 4 #include<iostream> 5 using namespace std; 6 7 class Component 8 { 9 public:10 virtual void operation()=0;11 };12 13 class ConcreteComponent:public Component14 {15 public:16 vo... 阅读全文
posted @ 2012-06-25 13:53 freewater 阅读(178) 评论(0) 推荐(0)
摘要:1 #ifndef OBSERVER_H 2 #define OBSERVER_H 3 #include<list> 4 #include<iostream> 5 using namespace std; 6 7 class Observer 8 { 9 public: 10 virtual void update()=0; 11 }; 12 13 class Subject 14 { 15 public: 16 virtual void attach(Observer* observer)=0; 17 virtual void detach(O... 阅读全文
posted @ 2012-06-23 11:09 freewater 阅读(182) 评论(0) 推荐(0)
摘要:策略模式(Strategy):定义了算法家族,分别封装起来,让它们之间可以互相替换,此模式让算法的变化,不会影响到使用算法的客户。[DP]以下为策略模式的实现代码。 1 #include<iostream> 2 using namespace std; 3 4 class Strategy{ 5 public: 6 Strategy(void){} 7 virtual ~Strategy(void){} 8 9 virtual void AlgorithmInterface()=0;10 };11 12 class ConcreteStrategyA:public ... 阅读全文
posted @ 2012-06-22 14:38 freewater 阅读(207) 评论(0) 推荐(0)