1.Decorator 模式通过组合的方式提供了一种给类增加职责(操作)的方法。

2.Decorator模式结构图

Decorator Pattern

3.实现

 1 #ifndef _DECORATOR_H_ 
 2 #define _DECORATOR_H_
 3 
 4 class Component 
 5 { 
 6 public: 
 7     virtual ~Component();
 8     virtual void Operation();
 9 protected: 
10     Component();
11 private:
12 };
13 
14 class ConcreteComponent:public Component 
15 { 
16 public: 
17     ConcreteComponent();
18     ~ConcreteComponent();
19     void Operation();
20 protected:
21 private:
22 };
23 
24 class Decorator:public Component 
25 { 
26 public: 
27     Decorator(Component* com);
28     virtual ~Decorator();
29     void Operation();
30 protected: 
31     Component* _com;
32 private: 
33 };
34 
35 class ConcreteDecorator:public Decorator 
36 { 
37 public: 
38     ConcreteDecorator(Component* com);
39     ~ConcreteDecorator();
40     void Operation();
41     void AddedBehavior();
42 protected:
43 private:
44 };
45 
46 #endif
Decorator.h

相关文章:

  • 2021-12-06
  • 2021-05-31
  • 2021-09-07
  • 2021-10-08
猜你喜欢
  • 2022-01-01
  • 2021-07-15
  • 2021-12-13
  • 2021-12-06
  • 2021-12-21
  • 2021-06-07
相关资源
相似解决方案