美文网首页
GeekBand C++设计模式(三)

GeekBand C++设计模式(三)

作者: bilinbilin | 来源:发表于2016-07-26 00:24 被阅读0次

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

鄙人认为正是设计模式的出现,使得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;

}

相关文章

  • C++设计模式(三) GeekBand

    “对象性能”模式 面向对象很好地解决了“抽象”的问题,但是不可避免地要付出一定的代价。对于通常情况来讲,面向对象的...

  • GeekBand C++设计模式(三)

    这里,对设计模式的概念就不细说了,凡是用过或者看过这方面书籍的都应该有所了解。 鄙人认为正是设计模式的出现,使得C...

  • GeekBand C++设计模式

    5.对象创建 通过“对象创建”模式绕开new,来避免对象创建(new)过程中所导致的紧耦合(依赖具体类),从而支持...

  • c++设计模式(一)--GeekBand

    一 策略模式策略模式和 Template 模式要解决的问题是相同(类似)的,都是为了给业务逻辑(算法)具体实现和抽...

  • 【geekband】C++设计模式1

    具体详见我的博客:(作业的码在博客最后)design patterns责任是思考面向对象设计的一个观点从概念层面,...

  • C++设计模式(一) GeekBand

    好的软件设计是多用代码复用,但实际设计中却要遇到许多变化,而变化是复用的天敌。为了能够尽量减少变化所带来的设计复杂...

  • C++设计模式(二) GeekBand

    “对象创建”模式通过“对象创建”模式绕开new,来避免对象创建(new)过程中所导致的紧耦合(依赖具体类),从而支...

  • [GeekBand] C++设计模式-2

    Factory Method 工厂方法模式:定义一个用于创建对象的接口,让子类决定实例化哪一个类。工厂方法是一个类...

  • [GeekBand] C++设计模式-3

    Singleton Flyweight State Memento Composite Iterator Chai...

  • [GeekBand] C++设计模式-1

    面向对象设计原则 单一职责原则(SRP) 一个类应该只负责一项职责,即只承担某一项功能。一个类应该仅有一个引起它变...

网友评论

      本文标题:GeekBand C++设计模式(三)

      本文链接:https://www.haomeiwen.com/subject/kplnjttx.html