#include <iostream> #include <string> using namespace std; class text { public: text(){} virtual ~text(){} virtual void operation(){cout<<"a text"<<endl;} }; class Scoll :public text { public: Scoll(){} virtual ~Scoll(){} void decorator(text* comp){this->tx=comp;} void operation(){tx->operation();} protected: text *tx; }; class vScoll :public Scoll { public: vScoll(){} virtual ~vScoll(){} void operation(){cout<<"vscoll+";Scoll::operation();} }; class hScoll : public Scoll { public: hScoll(){} virtual ~hScoll(){} void operation(){cout<<"hscoll+";Scoll::operation();} }; int main() { text *ptx=new text; Scoll *pvs=new vScoll; Scoll *phs=new hScoll; ptx->operation(); pvs->decorator(ptx); pvs->operation(); phs->decorator(pvs); phs->operation(); delete phs; delete pvs; delete ptx; system("pause"); return 0; }
装饰模式(C++)
最新推荐文章于 2023-02-11 16:58:05 发布