GeekBand C++设计模式(三)

这里,对设计模式的概念就不细说了,凡是用过或者看过这方面书籍的都应该有所了解。

鄙人认为正是设计模式的出现,使得C++的三大性质——封装、继承和多态发挥的淋漓尽致。尤其是多态性质,可以说是面向对的精髓所在。

封装性是为了很好的管理数据和方法;

继承是为了提高代码的重用;

多态的出现,使得执行过程中,出现多样性,调用一个方法时,可以从父类跳到子类执行,又从子类调用父类的方法等等,函数调用过程呈现曲线形式。总之,执行期,表现出多样化。

所以,要想很好的掌握设计模式,必须对多态性有很深的了解,再则,对类与类之间的关系有个清晰的认识,类与类之间的关系,无非6大关系——继承和实现、依赖和关联、组合和聚合

每个模式都是对类之间的这6种关系进行组合,呈现出不同的表现形式,下面,一一介绍23种设计模式,用到的类之间的关系和C++代码(在VS2008调试没问题的)。

先说创建型模式:

1、Factory Pattern工厂模式

使用到的类关系:继承和依赖

图1工厂模式UML图

Head.h

#ifndef_FACTORY_HEAD_

#define_FACTORY_HEAD_

#include

usingstd::cout;

usingstd::endl;

classCProduct;

classCFactory

{

private:

public:

CFactory(){}

~CFactory(){}

public:

virtualCProduct*CreateProduct() = 0;

};

classCConcreteFactory:publicCFactory

{

private:

public:

CConcreteFactory();

~CConcreteFactory();

public:

virtualCProduct*CreateProduct();

};

classCProduct

{

private:

public:

CProduct(){}

~CProduct(){}

public:

virtualvoidOperation()=0;

};

classCConcreteProduct1:publicCProduct

{

private:

public:

CConcreteProduct1();

~CConcreteProduct1();

public:

virtualvoidOperation();

};

classCConcreteProduct2:publicCProduct

{

private:

public:

CConcreteProduct2();

~CConcreteProduct2();

public:

virtualvoidOperation();

};

#endif

Main.cpp

#include"Head.h"

CConcreteFactory::CConcreteFactory()

{

}

CConcreteFactory::~CConcreteFactory()

{

}

CProduct*CConcreteFactory::CreateProduct()

{

returnnewCConcreteProduct2();

}

//////////////////////////////////////////////////////////

CConcreteProduct1::CConcreteProduct1()

{

}

CConcreteProduct1::~CConcreteProduct1()

{

}

voidCConcreteProduct1::Operation()

{

cout<<"CConcreteProduct1 Operation!"<

}

//////////////////////////////////////////////////////

CConcreteProduct2::CConcreteProduct2()

{

}

CConcreteProduct2::~CConcreteProduct2()

{

}

voidCConcreteProduct2::Operation()

{

cout<<"CConcreteProduct2 Operation!"<

}

intmain()

{

CFactory*pFac=newCConcreteFactory();

CProduct*pPro=pFac->CreateProduct();

if(pPro!=NULL)

pPro->Operation();

deletepPro;

deletepFac;

return0;

}

2、AbstractFactory Pattern抽象工厂模式

使用到的类关系:继承和依赖

图2抽象工厂模式

Head.h

#ifndef_ABSTRACT_FACTORY_HEAD_

#define_ABSTRACT_FACTORY_HEAD_

#include

usingstd::cout;

usingstd::endl;

classCProductA;

classCProductB;

classCAbstractFactory

{

private:

public:

CAbstractFactory(){}

~CAbstractFactory(){}

public:

virtualCProductA*CreateProductA() = 0;

virtualCProductB*CreateProductB() = 0;

};

classCConcreteAbstractFactory1:publicCAbstractFactory

{

private:

public:

CConcreteAbstractFactory1();

~CConcreteAbstractFactory1();

public:

virtualCProductA*CreateProductA();

virtualCProductB*CreateProductB();

};

classCConcreteAbstractFactory2:publicCAbstractFactory

{

private:

public:

CConcreteAbstractFactory2();

~CConcreteAbstractFactory2();

public:

virtualCProductA*CreateProductA();

virtualCProductB*CreateProductB();

};

classCProductA

{

private:

public:

CProductA(){}

~CProductA(){}

public:

virtualvoidOperation()=0;

};

classCConcreteProductA1:publicCProductA

{

private:

public:

CConcreteProductA1();

~CConcreteProductA1();

public:

virtualvoidOperation();

};

classCConcreteProductA2:publicCProductA

{

private:

public:

CConcreteProductA2();

~CConcreteProductA2();

public:

virtualvoidOperation();

};

classCProductB

{

private:

public:

CProductB(){}

~CProductB(){}

public:

virtualvoidOperation()=0;

};

classCConcreteProductB1:publicCProductB

{

private:

public:

CConcreteProductB1();

~CConcreteProductB1();

public:

virtualvoidOperation();

};

classCConcreteProductB2:publicCProductB

{

private:

public:

CConcreteProductB2();

~CConcreteProductB2();

public:

virtualvoidOperation();

};

#endif

Main.cpp

#include"Head.h"

CConcreteAbstractFactory1::CConcreteAbstractFactory1()

{

}

CConcreteAbstractFactory1::~CConcreteAbstractFactory1()

{

}

CProductA*CConcreteAbstractFactory1::CreateProductA()

{

returnnewCConcreteProductA1();

}

CProductB*CConcreteAbstractFactory1::CreateProductB()

{

returnnewCConcreteProductB1();

}

/////////////////////////CConcreteAbstractFactory2//////////////////////

CConcreteAbstractFactory2::CConcreteAbstractFactory2()

{

}

CConcreteAbstractFactory2::~CConcreteAbstractFactory2()

{

}

CProductA*CConcreteAbstractFactory2::CreateProductA()

{

returnnewCConcreteProductA2();

}

CProductB*CConcreteAbstractFactory2::CreateProductB()

{

returnnewCConcreteProductB2();

}

//////////////////////CConcreteProductA1///////////////////////////////////

CConcreteProductA1::CConcreteProductA1()

{

}

CConcreteProductA1::~CConcreteProductA1()

{

}

voidCConcreteProductA1::Operation()

{

cout<<"CConcreteProductA1 Operation!"<

}

//////////////////////CConcreteProductA2///////////////////////////////////

CConcreteProductA2::CConcreteProductA2()

{

}

CConcreteProductA2::~CConcreteProductA2()

{

}

voidCConcreteProductA2::Operation()

{

cout<<"CConcreteProductA2 Operation!"<

}

//////////////////////CConcreteProductB1///////////////////////////////////

CConcreteProductB1::CConcreteProductB1()

{

}

CConcreteProductB1::~CConcreteProductB1()

{

}

voidCConcreteProductB1::Operation()

{

cout<<"CConcreteProductB1 Operation!"<

}

//////////////////////CConcreteProductB2///////////////////////////////////

CConcreteProductB2::CConcreteProductB2()

{

}

CConcreteProductB2::~CConcreteProductB2()

{

}

voidCConcreteProductB2::Operation()

{

cout<<"CConcreteProductB2 Operation!"<

}

intmain()

{

CAbstractFactory*pFac=newCConcreteAbstractFactory1();

CProductA*pProA1=pFac->CreateProductA();

if(pProA1!=NULL)

pProA1->Operation();

CProductB*pProB1=pFac->CreateProductB();

if(pProB1!=NULL)

pProB1->Operation();

deletepProA1;

deletepProB1;

deletepFac;

return0;

}

3、单例模式

使用到的类关系:

单例模式很简单,目的就是只创建一个实例,关键是他的构造函数私有的,所以在外部是不能被调用的,只能使用自己的实例化函数进行构造,而实例化函数中,保证了只创建一个实例,下次在调用,如果判断已经创建了一个实例,则只返回已创建的实例,详看程序。

图3单例模式

Head.h

#ifndef_SINGLETON_HEAD_

#define_SINGLETON_HEAD_

#include

usingstd::cout;

usingstd::endl;

classCSingleton

{

private:

staticCSingleton*m_pSelf;

private:

CSingleton(){}// 把构造函数访问权限设置为private, 使得外部不能访问

public:

~CSingleton();

public:

staticCSingleton*Instance();// 实例化函数

voidOperation();

};

#endif

Main.cpp

#include"Head.h"

CSingleton*CSingleton::m_pSelf=NULL;

CSingleton::~CSingleton()

{

if(m_pSelf!=NULL)

deletem_pSelf;

m_pSelf=NULL;

}

CSingleton*CSingleton::Instance()

{

if(NULL==m_pSelf)

m_pSelf=newCSingleton();// 只有在类内部才能调用构造函数

returnm_pSelf;

}

voidCSingleton::Operation()

{

cout<<"the Address is "<

}

intmain()

{

CSingleton*pS=CSingleton::Instance();

if(NULL!=pS)

pS->Operation();

CSingleton*pS1=CSingleton::Instance();

if(NULL!=pS1)

pS1->Operation();

return0;

}

4、Builder Pattern建造者模式

使用到的类关系:组合、继承和依赖

图4建造者模式

Head.h

#ifndef_BUILDER_HEAD_

#define_BUILDER_HEAD_

#include

#include

usingstd::string;

usingstd::cout;

usingstd::endl;

classCProduct;

classCBuilder;

classCDirector

{

private:

CBuilder*m_pBuilder;

public:

CDirector(CBuilder*pBuilder);

~CDirector();

voidConstructProduct();

};

classCBuilder

{

private:

public:

CBuilder(){}

~CBuilder(){}

public:

virtualvoidBuildPartA(conststring&strA)=0;

virtualvoidBuildPartB(conststring&strB)=0;

virtualvoidBuildPartC(conststring&strC)=0;

virtualCProduct*GetProduct() = 0;

};

classCConcreteBuilder:publicCBuilder

{

private:

public:

CConcreteBuilder();

~CConcreteBuilder();

public:

virtualvoidBuildPartA(conststring&strA);

virtualvoidBuildPartB(conststring&strB);

virtualvoidBuildPartC(conststring&strC);

virtualCProduct*GetProduct();

};

classCProduct

{

private:

public:

CProduct(){}

~CProduct(){}

public:

voidOperation();

};

#endif

Main.cpp

#include"Head.h"

CDirector::CDirector(CBuilder*pBuilder)

{

m_pBuilder=pBuilder;

}

CDirector::~CDirector()

{

if(m_pBuilder!=NULL)

deletem_pBuilder;

m_pBuilder=NULL;

}

voidCDirector::ConstructProduct()

{

CProduct*pPro=NULL;

if(m_pBuilder!=NULL)

pPro=m_pBuilder->GetProduct();

if(pPro!=NULL)

pPro->Operation();

deletepPro;

pPro=NULL;

}

CConcreteBuilder::CConcreteBuilder()

{

}

CConcreteBuilder::~CConcreteBuilder()

{

}

voidCConcreteBuilder::BuildPartA(conststring&strA)

{

cout<<"Build PartA->"<

}

voidCConcreteBuilder::BuildPartB(conststring&strB)

{

cout<<"Build PartB->"<

}

voidCConcreteBuilder::BuildPartC(conststring&strC)

{

cout<<"Build PartC->"<

}

CProduct*CConcreteBuilder::GetProduct()

{

BuildPartA("hong");

BuildPartB("ji");

BuildPartC("hai");

returnnewCProduct();

}

voidCProduct::Operation()

{

cout<<"CProduct Operation"<

}

intmain()

{

CDirector*pDir=newCDirector(newCConcreteBuilder());

pDir->ConstructProduct();

deletepDir;

pDir=NULL;

return0;

}

5、prototype 原型模式

创建型模式的最后一个,其实原型模式的目的很简单,就是克隆一个已有的对象,所以其作用和拷贝构造函数相似.

使用到的类关系:继承

图5原型模式

Head.h

#ifndef_PROTOTYPE_HEAD_

#define_PROTOTYPE_HEAD_

#include

usingstd::cout;

usingstd::endl;

classCPrototype

{

private:

public:

CPrototype(){}

~CPrototype(){}

public:

virtualCPrototype*Clone()const= 0;

virtualvoidOperation()const=0;

};

classCConcretePrototype:publicCPrototype

{

private:

public:

CConcretePrototype(){}

CConcretePrototype(constCConcretePrototype&self);

~CConcretePrototype(){}

public:

virtualCPrototype*Clone()const;

public:

voidOperation()const;

};

#endif

Main.cpp

#include"Head.h"

CConcretePrototype::CConcretePrototype(constCConcretePrototype&self)

{

// 如果有成员变量,进行复制操作,注意深度拷贝

}

CPrototype*CConcretePrototype::Clone()const

{

returnnewCConcretePrototype(*this);

}

voidCConcretePrototype::Operation()const

{

cout<<"CConcretePrototype Operation"<

}

intmain()

{

CPrototype*pInstance=newCConcretePrototype();

CPrototype*pClone=pInstance->Clone();

if(pClone!=NULL)

pClone->Operation();

deletepInstance;

pInstance=NULL;

deletepClone;// 注意,pClone也需要释放,否则有内存泄露

pClone=NULL;

return0;

}

创建型模式,一共5个,到此完毕

下面进行结构行模式的C++模仿

6、Bridge Pattern 桥接模式

桥接模式是实际编程中应用的比较多的一个模式,其目的是将抽象与实现进行分离。

使用到的类关系:继承与组合

图6桥接模式

Head.h

#ifndef_BRIDGE_HEAD_

#define_BRIDGE_HEAD_

#include

usingstd::cout;

usingstd::endl;

classCProductImp;

// 注意,这里有一个纯虚函数,但是该类还可以包含成员变量,这是正确的,一个类有纯虚函数,

// 只是说明该类不能被实例化,但是它还是可以包含成员变量,然后对其子类进行实例化,父类的

//  这些成员变量是可以使用的

classCProduct

{

protected:

CProductImp*m_pImpProduct;

public:

CProduct(CProductImp*pImpPrd);

~CProduct();

public:

virtualvoidOperation() = 0;

};

classCConcreteProduct:publicCProduct

{

private:

public:

CConcreteProduct(CProductImp*pImpPrd);

~CConcreteProduct();

public:

virtualvoidOperation();

};

classCProductImp

{

private:

public:

CProductImp(){}

~CProductImp(){}

public:

virtualvoidOperation()=0;

};

classCConcreteProductImp1:publicCProductImp

{

private:

public:

CConcreteProductImp1(){}

~CConcreteProductImp1(){}

public:

virtualvoidOperation();

};

classCConcreteProductImp2:publicCProductImp

{

private:

public:

CConcreteProductImp2(){}

~CConcreteProductImp2(){}

public:

virtualvoidOperation();

};

#endif

Main.cpp

#include"Head.h"

CProduct::CProduct(CProductImp*pImpPrd)

{

m_pImpProduct=pImpPrd;

}

CProduct::~CProduct()

{

if(NULL!=m_pImpProduct)

deletem_pImpProduct;

m_pImpProduct=NULL;

}

CConcreteProduct::CConcreteProduct(CProductImp*pImpPrd):CProduct(pImpPrd)

{

}

CConcreteProduct::~CConcreteProduct()

{

}

voidCConcreteProduct::Operation()

{

if(m_pImpProduct!=NULL)

m_pImpProduct->Operation();

}

voidCConcreteProductImp1::Operation()

{

cout<<"CConcreteProductImp1 Operation"<

}

voidCConcreteProductImp2::Operation()

{

cout<<"CConcreteProductImp2 Operation"<

}

intmain()

{

CProduct*pProduct=newCConcreteProduct(newCConcreteProductImp1());

if(pProduct!=NULL)

pProduct->Operation();

deletepProduct;

pProduct=NULL;

return0;

}

7、Adapter Pattern 适配器模式

目的:使用第三方库时,不用修改自己的调用接口

使用到的类关系:继承与关联

图7适配器模式

Head.h

#ifndef_APAPTER_HEAD_

#define_APAPTER_HEAD_

#include

usingstd::cout;

usingstd::endl;

classCAdaptee;// 第三方库

classCTarget

{

private:

public:

CTarget(){}

~CTarget(){}

public:

virtualvoidRequest() = 0;

};

classCAdapter:publicCTarget

{

private:

CAdaptee*m_pAdaptee;

public:

CAdapter(CAdaptee*pAdaptee);

~CAdapter();

public:

virtualvoidRequest();

};

classCAdaptee

{

private:

public:

CAdaptee(){}

~CAdaptee(){}

public:

voidSpecificRequest();

};

#endif

Main.cpp

#include"Head.h"

CAdapter::CAdapter(CAdaptee*pAdaptee)

{

m_pAdaptee=pAdaptee;

}

CAdapter::~CAdapter()

{

if(m_pAdaptee!=NULL)

deletem_pAdaptee;

m_pAdaptee=NULL;

}

voidCAdapter::Request()

{

if(m_pAdaptee!=NULL)

m_pAdaptee->SpecificRequest();

}

voidCAdaptee::SpecificRequest()

{

cout<<"CAdaptee SpecificRequest"<

}

intmain()

{

CTarget*pTarget=newCAdapter(newCAdaptee());

if(pTarget)

pTarget->Request();

deletepTarget;

pTarget=NULL;

return0;

}

8、Decorator Pattern 装饰者模式

使用到的类关系:继承与组合

图8装饰者模式

Head.h

#ifndef_DECORATOR_HEAD_

#define_DECORATOR_HEAD_

#include

usingstd::cout;

usingstd::endl;

classCComponent

{

private:

public:

CComponent(){}

~CComponent(){}

public:

virtualvoidOperation();

};

classCConcreteComponent:publicCComponent

{

private:

public:

CConcreteComponent(){}

~CConcreteComponent(){}

public:

virtualvoidOperation();

};

classCDecorator:publicCComponent

{

protected:

CComponent*m_pCom;

public:

CDecorator(CComponent*pCom);

~CDecorator();

public:

virtualvoidOperation()=0;

};

classCConcreteDecorator:publicCDecorator

{

private:

public:

CConcreteDecorator(CComponent*pCom);

~CConcreteDecorator();

public:

virtualvoidOperation();

voidAddedBehavior();

};

#endif

Main.cpp

#include"Head.h"

voidCComponent::Operation()

{

cout<<"CComponent Operation"<

}

voidCConcreteComponent::Operation()

{

cout<<"CConcreteComponent Operation"<

}

CDecorator::CDecorator(CComponent*pCom)

{

m_pCom=pCom;

}

CDecorator::~CDecorator()

{

if(m_pCom!=NULL)

deletem_pCom;

m_pCom=NULL;

}

CConcreteDecorator::CConcreteDecorator(CComponent*pCom):CDecorator(pCom)

{

}

CConcreteDecorator::~CConcreteDecorator()

{

}

voidCConcreteDecorator::Operation()

{

if(m_pCom!=NULL)

m_pCom->Operation();

cout<<"CConcreteDecorator Operation"<

AddedBehavior();

}

voidCConcreteDecorator::AddedBehavior()

{

cout<<"CConcreteDecorator AddedBehavior"<

}

intmain()

{

CDecorator*pDec=newCConcreteDecorator(newCConcreteComponent());

pDec->Operation();

deletepDec;

pDec=NULL;

return0;

}

9、Composite Pattern组合模式

图9组合模式

目的:当结构中用到递归的树形结构时使用

使用到的类关系:继承与组合(与装饰有相似之处)

组合模式也是一个比较复杂的模式,要细细研究代码

Head.h

#ifndef_COMPOSITE_HEAD_

#define_COMPOSITE_HEAD_

#include

#include

#include

usingstd::cout;

usingstd::endl;

usingstd::vector;

usingstd::string;

classCComponent

{

private:

public:

CComponent(){}

~CComponent(){}

public:

virtualvoidOperation()=0;

virtualvoidAdd(CComponent*pCom){}

virtualvoidRemove(constint&nIndex){}

virtualCComponent*GetChild(constint&nIndex){returnNULL;}

};

classCLeaf:publicCComponent

{

private:

public:

CLeaf(){}

~CLeaf(){}

public:

virtualvoidOperation();

};

classCComposite:publicCComponent

{

protected:

vectorm_arrCom;

public:

CComposite();

~CComposite();

public:

virtualvoidOperation();

virtualvoidAdd(CComponent*pCom);

virtualvoidRemove(constint&nIndex);

virtualCComponent*GetChild(constint&nIndex);

};

#endif

Main.cpp

#include"Head.h"

voidCLeaf::Operation()

{

cout<<"CLeaf::Operation"<

}

CComposite::CComposite()

{

m_arrCom.clear();

}

CComposite::~CComposite()

{

for(inti= 0;i

{

CComponent*pCur=m_arrCom[i];

if(pCur!=NULL)

{

deletepCur;

pCur=NULL;

}

}

m_arrCom.clear();

}

voidCComposite::Operation()

{

cout<<"CComposite::Operation"<

vector::iteratorit=m_arrCom.begin();

for(;it!=m_arrCom.end();it++)

{

CComponent*pCur= *it;

if(pCur==NULL)

continue;

pCur->Operation();

}

}

voidCComposite::Add(CComponent*pCom)

{

m_arrCom.push_back(pCom);

}

voidCComposite::Remove(constint&nIndex)

{

if(nIndex< 0)

return;

vector::iteratorit=m_arrCom.begin();

inti= 0;

for(;it!=m_arrCom.end();it++)

{

CComponent*pCur= *it;

if(pCur==NULL)

{

i++;

continue;

}

if(i==nIndex)

break;

i++;

}

m_arrCom.erase(it);

}

CComponent*CComposite::GetChild(constint&nIndex)

{

if(nIndex< 0)

returnNULL;

returnm_arrCom[nIndex];

}

intmain()

{

CComponent*pLeaf1=newCLeaf();

CComponent*pLeaf2=newCLeaf();

CComponent*pLeaf3=newCLeaf();

CComponent*pComposite2=newCComposite();

CComponent*pComposite3=newCComposite();

CComponent*pComposite1=newCComposite();

pComposite1->Add(pLeaf1);

pComposite1->Add(pLeaf2);

pComposite2->Add(pLeaf3);

pComposite1->Add(pComposite2);

pComposite1->Add(pComposite3);

pComposite1->Operation();

// 只释放pComposite1即可,其他的是pComposite1的成员,会在pComposite1的析构函数中

// 释放

deletepComposite1;

pComposite1=NULL;

return0;

}

10、Flyweight Pattern享元模式

目的:避免同样的内容重复创建,节省空间,提交利用率

使用到的类关系:继承与组合

图10享元模式

Head.h

#ifndef_COMPOSITE_HEAD_

#define_COMPOSITE_HEAD_

#include

#include

#include

usingstd::cout;

usingstd::endl;

usingstd::vector;

usingstd::string;

classCFlyweight

{

protected:

stringm_strState;

public:

~CFlyweight(){}

protected:

CFlyweight(conststring&strState)

{

m_strState=strState;

}

public:

virtualvoidOperation()=0;

conststringGetState()const{returnm_strState;}

};

classCConcreteCFlyweight:publicCFlyweight

{

private:

public:

CConcreteCFlyweight(conststring&strState);

~CConcreteCFlyweight();

public:

virtualvoidOperation();

};

classCUnShareConcreteCFlyweight:publicCFlyweight

{

public:

CUnShareConcreteCFlyweight(conststring&strState):CFlyweight(strState)

{}

~CUnShareConcreteCFlyweight(){}

public:

virtualvoidOperation(){}

};

classCFlyweightFactory

{

private:

vectorm_arrFlyweight;

public:

CFlyweightFactory();

~CFlyweightFactory();

public:

CFlyweight*GetFlyweight(conststring&strState);

};

#endif

Main.cpp

#include"Head.h"

CConcreteCFlyweight::CConcreteCFlyweight(conststring&strState):CFlyweight(strState)

{

}

CConcreteCFlyweight::~CConcreteCFlyweight()

{

}

voidCConcreteCFlyweight::Operation()

{

cout<<"CConcreteCFlyweight "<

}

CFlyweightFactory::CFlyweightFactory()

{

m_arrFlyweight.clear();

}

CFlyweightFactory::~CFlyweightFactory()

{

for(inti= 0;i

{

CFlyweight*pCur=m_arrFlyweight[i];

if(pCur!=NULL)

{

deletepCur;

pCur=NULL;

}

}

m_arrFlyweight.clear();

}

CFlyweight*CFlyweightFactory::GetFlyweight(conststring&strState)

{

vector::iteratorit=m_arrFlyweight.begin();

for(;it!=m_arrFlyweight.end();it++)

{

if(*it==NULL)

continue;

if((*it)->GetState() ==strState)

{

cout<<"already created"<

return*it;

}

}

CFlyweight*pNew=newCConcreteCFlyweight(strState);

m_arrFlyweight.push_back(pNew);

returnpNew;

}

intmain()

{

CFlyweightFactory*pFac=newCFlyweightFactory();

CFlyweight*pFly1=pFac->GetFlyweight("hong");

if(pFly1)

pFly1->Operation();

CFlyweight*pFly2=pFac->GetFlyweight("jihai");

if(pFly2)

pFly2->Operation();

CFlyweight*pFly3=pFac->GetFlyweight("hong");

if(pFly3)

pFly3->Operation();

deletepFac;

pFac=NULL;

return0;

}

11、FacadePattern 外观模式

目的:将众多接口进行统一化,提供一个简单的接口

使用到的类关系:依赖

图11外观模式

Head.h

#ifndef_FACADE_HEAD_

#define_FACADE_HEAD_

#include

usingstd::cout;

usingstd::endl;

classCSubSystem1;

classCSubSystem2;

classCFacade

{

private:

CSubSystem1*m_pSubSys1;

CSubSystem2*m_pSubSys2;

public:

CFacade();

~CFacade();

public:

voidOperation();

};

classCSubSystem1

{

private:

public:

CSubSystem1(){}

~CSubSystem1(){}

public:

voidOperation();

};

classCSubSystem2

{

private:

public:

CSubSystem2(){}

~CSubSystem2(){}

public:

voidOperation();

};

#endif

Main.cpp

#include"Head.h"

CFacade::CFacade()

{

m_pSubSys1=newCSubSystem1();

m_pSubSys2=newCSubSystem2();

}

CFacade::~CFacade()

{

if(m_pSubSys1!=NULL)

deletem_pSubSys1;

m_pSubSys1=NULL;

if(m_pSubSys2!=NULL)

deletem_pSubSys2;

m_pSubSys2=NULL;

}

voidCFacade::Operation()

{

if(m_pSubSys1)

m_pSubSys1->Operation();

if(m_pSubSys2)

m_pSubSys2->Operation();

}

voidCSubSystem1::Operation()

{

cout<<"CSubSystem1::Operation"<

}

voidCSubSystem2::Operation()

{

cout<<"CSubSystem2::Operation"<

}

intmain()

{

CFacade*pFac=newCFacade();

pFac->Operation();

deletepFac;

pFac=NULL;

return0;

}

12、代理模式

使用到的类关系:依赖

图12代理模式

Head.h

#ifndef_PROXY_HEAD_

#define_PROXY_HEAD_

#include

usingstd::cout;

usingstd::endl;

classCProxy;

classCSubject

{

private:

public:

CSubject(){}

virtual~CSubject(){}

public:

virtualvoidRequest()=0;

};

classCConcreteSubject:publicCSubject

{

private:

public:

CConcreteSubject(){}

~CConcreteSubject(){}

public:

virtualvoidRequest();

};

classCProxy:publicCSubject

{

private:

CSubject*m_pSubject;

public:

CProxy(CSubject*pSubject);

~CProxy();

public:

virtualvoidRequest();

};

#endif

Main.cpp

#include"Head.h"

voidCConcreteSubject::Request()

{

cout<<"CConcreteSubject::Request"<

}

CProxy::CProxy(CSubject*pSubject)

{

m_pSubject=pSubject;

}

CProxy::~CProxy()

{

if(m_pSubject)

deletem_pSubject;

m_pSubject=NULL;

}

voidCProxy::Request()

{

if(m_pSubject)

m_pSubject->Request();

}

intmain()

{

CSubject*pSub=newCProxy(newCConcreteSubject());

pSub->Request();

deletepSub;

pSub=NULL;

return0;

}

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 202,802评论 5 476
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 85,109评论 2 379
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 149,683评论 0 335
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 54,458评论 1 273
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 63,452评论 5 364
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 48,505评论 1 281
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 37,901评论 3 395
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 36,550评论 0 256
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 40,763评论 1 296
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 35,556评论 2 319
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 37,629评论 1 329
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 33,330评论 4 318
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 38,898评论 3 307
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 29,897评论 0 19
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 31,140评论 1 259
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 42,807评论 2 349
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 42,339评论 2 342

推荐阅读更多精彩内容