类间通信-门面模式
来源:互联网 发布:狼图腾 知乎 编辑:程序博客网 时间:2024/05/17 22:38
例:如图的类图和内存图
#include<iostream>using namespace std;class Base{protected:Base *next;public: Base() { next=NULL; cout<<"Base create"<<endl; } Base(Base *t) { next=t; cout<<"Base create"<<endl; } virtual void fun()=0; virtual ~Base() { cout<<"Base des"<<endl; }};class A:public Base{public: A() { cout<<"A Create"<<endl; next=NULL; } A(Base *t) { cout<<"A Create"<<endl; next=t; } void fun() { cout<<"A fun()"<<endl; if(next) next->fun(); } ~A() { cout<<"A Des"<<endl; }};class B:public Base{public: B() { cout<<"B Create"<<endl; next=NULL; } B(Base *t) { cout<<"B Create"<<endl; next=t; } void fun() { cout<<"B fun()"<<endl; if(next) next->fun(); } ~B() { cout<<"B Des"<<endl; }};class Decorator:public Base{public: Decorator() { cout<<"Decorator Create"<<endl; next=NULL; } Decorator(Base *t) { cout<<"Decorator Create"<<endl; next=t; } virtual void fun()=0; ~Decorator() { cout<<"Decorator Des"<<endl; }};class D1:public Decorator{public: D1() { cout<<"D1 Create"<<endl; } D1(Base *t) { next=t; } void fun() { cout<<"D1 fun()"<<endl; if(next) next->fun(); } ~D1() { cout<<"D1 des"<<endl; }};class D2:public Decorator{public: D2() { cout<<"D2 Create"<<endl; } D2(Base *t) { next=t; } void fun() { cout<<"D2 fun()"<<endl; if(next) next->fun(); } ~D2() { cout<<"D2 des"<<endl; }};class D3:public Decorator{public: D3() { cout<<"D3 Create"<<endl; } D3(Base *t) { next=t; } void fun() { cout<<"D3 fun()"<<endl; if(next) next->fun(); } ~D3() { cout<<"D3 des"<<endl; }};class My{private: Base * pBase;public: My() { cout<<"My Create"<<endl; } My(Base *t) { pBase=t; } void dofun() { pBase->fun(); } ~My() { cout<<"My des"<<endl; }};int main(void){ D3 *pD3=new D3(); D1 *pD1=new D1(pD3); A *pA=new A(pD1); My *pMy=new My(pA); pMy->dofun(); delete pMy; delete pA; delete pD1; delete pD3; return 0;}
例:将next,由protected改为private
#include<iostream>using namespace std;class Base{private:Base *next;public: Base() { next=NULL; cout<<"Base create"<<endl; } Base(Base *t) { next=t; cout<<"Base create"<<endl; } virtual void fun()=0; void WinNext() { if(next) next->fun(); } virtual ~Base() { cout<<"Base des"<<endl; }};class A:public Base{public: A() { cout<<"A Create"<<endl; } A(Base *t):Base(t) { cout<<"A Create"<<endl; } void fun() { cout<<"A fun()"<<endl; WinNext(); } ~A() { cout<<"A Des"<<endl; }};class B:public Base{public: B() { cout<<"B Create"<<endl; } B(Base *t):Base(t) { cout<<"B Create"<<endl; } void fun() { cout<<"B fun()"<<endl; WinNext(); } ~B() { cout<<"B Des"<<endl; }};class Decorator:public Base{public: Decorator() { cout<<"Decorator Create"<<endl; } Decorator(Base *t):Base(t) { cout<<"Decorator Create"<<endl; } virtual void fun()=0; ~Decorator() { cout<<"Decorator Des"<<endl; }};class D1:public Decorator{public: D1() { cout<<"D1 Create"<<endl; } D1(Base *t):Decorator(t) { } void fun() { cout<<"D1 fun()"<<endl; WinNext(); } ~D1() { cout<<"D1 des"<<endl; }};class D2:public Decorator{public: D2() { cout<<"D2 Create"<<endl; } D2(Base *t):Decorator(t) { } void fun() { cout<<"D2 fun()"<<endl; WinNext(); } ~D2() { cout<<"D2 des"<<endl; }};class D3:public Decorator{public: D3() { cout<<"D3 Create"<<endl; } D3(Base *t):Decorator(t) { } void fun() { cout<<"D3 fun()"<<endl; WinNext(); } ~D3() { cout<<"D3 des"<<endl; }};class My{private: Base * pBase;public: My() { cout<<"My Create"<<endl; } My(Base *t) { pBase=t; } void dofun() { pBase->fun(); } ~My() { cout<<"My des"<<endl; }};int main(void){ D3 *pD3=new D3(); D1 *pD1=new D1(pD3); A *pA=new A(pD1); My *pMy=new My(pA); pMy->dofun(); delete pMy; delete pA; delete pD1; delete pD3; return 0;}
阅读全文
1 0
- 类间通信-门面模式
- 门面模式(结构类)
- 门面模式
- 门面模式
- 门面模式
- 门面模式
- 门面模式
- 门面模式
- 门面模式
- 门面模式
- 门面模式
- 门面模式
- 门面模式
- 门面模式
- 门面模式
- 门面模式
- 门面模式
- 门面模式
- React Native 指定文字行数,超出行数显示更多
- 菜鸟springboot学习之旅四
- 恶意代码分析实战 Lab 3-4 习题笔记
- codeforces 851 #432 div2 C Five Dimensional Points
- Form表单中get与post的本质区别
- 类间通信-门面模式
- gridview里模板列使用textbox、button赋值取值,以及全选按钮的使用
- ArcGIS Maritime 发布海图切片服务详解
- 一个textView显示多个字体大小
- AChartEngine 常用属性设置
- MacOS Shell
- QJson解析数组里面的不同对象的值(怎么将数组转为对象)
- vue饿了么学习-第四篇(web字体)
- python再复习(1)数据类型以及字符串