1/33这里,对设计模式的概念就不细说了,凡是用过或者看过这方面书籍的都应该有所了解。鄙人认为正是设计模式的出现,使得C++的三大性质——封装、继承和多态发挥的淋漓尽致。尤其是多态性质,可以说是面向对的精髓所在。封装性是为了很好的管理数据和方法;继承是为了提高代码的重用;而多态的出现,使得执行过程中,出现多样性,调用一个方法时,可以从父类跳到子类执行,又从子类调用父类的方法等等,函数调用过程呈现曲线形式。总之,执行期,表现出多样化。所以,要想很好的掌握设计模式,必须对多态性有很深的了解,再则,对类与类之间的关系有个清晰的认识,类与类之间的关系,无非6大关系——继承和实现、依赖和关联、组合和聚合。每个模式都是对类之间的这6种关系进行组合,呈现出不同的表现形式,下面,一一介绍23种设计模式,用到的类之间的关系和C++代码(在VS2008调试没问题的)。先说创建型模式:1、FactoryPattern工厂模式使用到的类关系:继承和依赖2/33图1工厂模式UML图Head.h#ifndef_FACTORY_HEAD_#define_FACTORY_HEAD_#includeiostreamusingstd::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();};#endif3/33Main.cpp#includeHead.hCConcreteFactory::CConcreteFactory(){}CConcreteFactory::~CConcreteFactory(){}CProduct*CConcreteFactory::CreateProduct(){returnnewCConcreteProduct2();}//////////////////////////////////////////////////////////CConcreteProduct1::CConcreteProduct1(){}CConcreteProduct1::~CConcreteProduct1(){}voidCConcreteProduct1::Operation(){coutCConcreteProduct1Operation!endl;}//////////////////////////////////////////////////////CConcreteProduct2::CConcreteProduct2(){}CConcreteProduct2::~CConcreteProduct2(){}voidCConcreteProduct2::Operation(){coutCConcreteProduct2Operation!endl;}intmain(){CFactory*pFac=newCConcreteFactory();CProduct*pPro=pFac-CreateProduct();if(pPro!=NULL)pPro-Operation();deletepPro;deletepFac;return0;}4/332、AbstractFactoryPattern抽象工厂模式使用到的类关系:继承和依赖图2抽象工厂模式Head.h#ifndef_ABSTRACT_FACTORY_HEAD_#define_ABSTRACT_FACTORY_HEAD_#includeiostreamusingstd::cout;usingstd::endl;classCProductA;classCProductB;classCAbstractFactory{private:public:CAbstractFactory(){}~CAbstractFactory(){}public:virtualCProductA*CreateProductA()=0;5/33virtualCProductB*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();6/33};classCProductB{private:public:CProductB(){}~CProductB(){}public:virtualvoidOperation()=0;};classCConcreteProductB1:publicCProductB{private:public:CConcreteProductB1();~CConcreteProductB1();public:virtualvoidOperation();};classCConcreteProductB2:publicCProductB{private:public:CConcreteProductB2();~CConcreteProductB2();public:virtualvoidOperation();};#endifMain.cpp#includeHead.hCConcreteAbstractFactory1::CConcreteAbstractFactory1(){}CConcreteAbstractFactory1::~CConcreteAbstractFactory1(){}CProductA*CConcreteAbstractFactory1::CreateProductA(){returnnewCConcreteProductA1();}CProductB*CConcreteAbstractFactory1::CreateProductB(){returnnewCConcreteProductB1();}7/33/////////////////////////CConcreteAbstractFactory2//////////////////////CConcreteAbstractFactory2::CConcreteAbstractFactory2(){}CConcreteAbstractFactory2::~CConcreteAbstractFactory2(){}CProductA*CConcreteAbstractFactory2::CreateProductA(){returnnewCConcreteProductA2();}CProductB*CConcreteAbstractFactory2::CreateProductB(){returnnewCConcreteProductB2();}//////////////////////CConcreteProductA1///////////////////////////////////CConcreteProductA1::CConcreteProductA1(){}CConcreteProductA1::~CConcreteProductA1(){}voidCConcreteProductA1::Operation(){coutCConcreteProductA1Operation!endl;}//////////////////////CConcreteProductA2///////////////////////////////////CConcreteProductA2::CConcreteProductA2(){}CConcreteProductA2::~CConcreteProductA2(){}voidCConcreteProductA2::Operation(){coutCConcreteProductA2Operation!endl;}//////////////////////CConcreteProductB1///////////////////////////////////CConcreteProductB1::CConcreteProductB1(){}CConcreteProductB1::~CConcreteProductB1(){}voidCConcreteProductB1::Operation()8/33{coutCConcreteProductB1Operation!endl;}//////////////////////CConcreteProductB2///////////////////////////////////CConcreteProductB2::CConcreteProductB2(){}CConcreteProductB2::~CConcreteProductB2(){}voidCConcreteProductB2::Operation(){coutCConcreteProductB2Operation!endl;}intmain(){CAbstractFactory*pFac=ne