帐前卒专栏

Without software, we are nothing.

Design Pattern -- Decorator Pattern

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
// decorator.cpp : Defines the entry point for the console application.
// author: chillyc
// email: chillycreator @t gmail.com

#include<iostream>
namespace pattern{
  /// this pattern is decorator pattern.
  /// In this pattern the super class should have default process functions.
  /// for example: "Read" has default read word function, and it is useful in the future.
  /// Read_Char, Read_Chinese, Decorator inherit Read class.
  /// But those three subclass is not powerful to deal with sentences.
  /// Then the subclass of Decorator will deal with sentences, but it dose not know every word.
  /// So Sentence(Encode) pattern has become.
  /// But decorator pattern is not only dealing with this situation but also enhance some classes
  /// capacity without adding new classes.
  /// is it something like Bridge?
#ifndef READ_H
#define READ_H
  class Read{
  public:
      Read(){}
      virtual ~Read(){}

      /// this is default process function.
      /// and decorator class will use this function to read
      virtual void ReadWord(){std::cout<<"default read!"<<std::endl;}
      /// if the function is a pure virtual function,
      /// decorator class need not inherit this class and
      /// it just has an instance of Read as a member varible.
      // virtual void ReadWord()=0;

  };
#endif

#ifndef READCHAR_H
#define READCHAR_H
  class Read_Char:public Read{
  public:
      Read_Char():Read(){}
      ~Read_Char(){}
      virtual void ReadWord();
  };
#endif
  void Read_Char::ReadWord(){
      std::cout<< "read a char"<<std::endl;

  }

#ifndef READCHINESE_H
#define READCHINESE_H
  class Read_Chinese:public Read{
  public:
      Read_Chinese():Read(){}
      ~Read_Chinese(){}
      virtual void ReadWord();
  };
#endif
  void Read_Chinese::ReadWord(){
      std::cout<<"read a chinese word"<<std::endl;
  }

#ifndef DECORATOR_H
#define DECORATOR_H
  class Decorator:public Read{
  protected:
      Read* r;
  public:
      Decorator():Read(){r = NULL;}
      Decorator(Read* r):Read(),r(r){}
      virtual ~Decorator(){if(r!=NULL) delete r;}
      virtual void ReadWord();
  };
#endif
  void Decorator::ReadWord(){
      if(NULL == r)
          Read::ReadWord();
      else
          r->ReadWord();
  }

#ifndef READSENTENCE_H
#define READSENTENCE_H
  // this class is real work decorator class
  class Read_Sentence:public Decorator{

  public:
      Read_Sentence():Decorator(){}
      Read_Sentence(Read* r):Decorator(r){}
      ~Read_Sentence(){}
      virtual void ReadWord();
  };
#endif
  void Read_Sentence::ReadWord(){
      /// parent class method is called first!
      Decorator::ReadWord();
      /// then your method be called
      std::cout<<"read a sentence"<<std::endl;
  }
}

int main()
{
  pattern::Read_Sentence rs1(new pattern::Read_Char());// read char
  pattern::Read_Sentence rs2(new pattern::Read_Chinese());// read chinese
  pattern::Read_Sentence rs3; // default read
  rs1.ReadWord();
  rs2.ReadWord();
  rs3.ReadWord();
  return 0;
}

Comments