design pattern -- decorator pattern 发表于 2010-05-03 | 更新于: 2017-10-12 | | 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110// 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;} 本文作者: 帐前卒 本文链接: http://chillyc.info/2010/2010-05-03-design-pattern-decorator-pattern/ 版权声明: 本博客所有文章除特别声明外,只能复制超链接地址,且必须注明出处!