Singleton:
(1)、意图:保证一个类仅有一个实例,并提供一个访问它的全局访问点;
(2)、适用性:当类只能有一个实例而且客户可以从一个众所周知的访问点访问它时;当这个唯一实例应该是通过子类化可扩展的,并且客户应该无需更改代码就能使用一个扩展的实例时;
(3)、优点:
A、对唯一实例的受控访问:因为Singleton类封装它的唯一实例,所以它可以严格的控制客户怎样以及何时访问它;
B、缩小名空间:Singleton模式是对全局变量的一种改进,它避免了那些存储唯一实例的全局变量污染名空间;
C、允许对操作和表示的精化:Singleton类可以有子类,而且用这个扩展类的实例来配置一个应用是很容易的,你可以用你所需要的类的实例在运行时刻配置应用;
D、允许可变数目的实例:这个模式使得你易于改变你的想法,并允许Singleton类的多个实例。此外,你可以用相同的方法来控制应用所使用的实例的数目。只有允许访问Singleton实例的操作需要改变;
E、比类操作更灵活:另一种封装Singleton功能的方式是使用类操作(即C++中的静态成员函数或者是Smalltalk中的类方法)。但这两种语言技术都难以改变设计以允许一个类有多个实例。此外,C++中的静态成员函数不是虚函数,因此子类不能多态的重定义它们。
示例代码1:
#include <iostream> #include <string> #include <vector> using namespace std; class Singleton { private: Singleton() {} static Singleton* single; public: static Singleton* GetInstance() { if (single == NULL) { single = new Singleton(); } return single; } }; Singleton* Singleton::single = NULL;//静态变量类外初始化 //客户端 int main() { Singleton* s1 = Singleton::GetInstance(); Singleton* s2 = Singleton::GetInstance(); if (s1 == s2) cout<<"ok"<<endl; else cout<<"no"<<endl; /*result: ok */ return 0; } |
示例代码2:
Singleton.h:
#ifndef _SINGLETON_H_ #define _SINGLETON_H_ #include <iostream> using namespace std; class Singleton { public: static Singleton* Instance(); protected: Singleton();//Singleton不可以被实例化,因此将其构造函数声明为protected或者直接声明为private private: static Singleton* _instance; }; #endif//~_SINGLETON_H_ |
Singleton.cpp:
#include "Singleton.h" #include <iostream> using namespace std; Singleton* Singleton::_instance = 0; Singleton::Singleton() { cout<<"Singleton ..."<<endl; } Singleton* Singleton::Instance() { if (_instance == 0) _instance = new Singleton(); return _instance; } |
main.cpp:
#include "Singleton.h" #include <iostream> using namespace std; int main() { Singleton* sgn = Singleton::Instance(); /*result: Singleton ... */ return 0; } |
单例模式结构图:
Builder: (1)、意图:将一个复杂对象的构建与它的表示分离,使得同样的构建过程可以创建不同的表示;(2)、适用性:当创建复杂对象的算法应该独立于该对象的组成部分以及它们的装配方式时;当构造过程必须允许被构造的对象有不同的表示时;
Abstract Factory与Builder相似,因为它也可以创建复杂对象,主要的区别是Builder模式着重于一步步构造一个复杂对象。而AbstractFactory着重于多个系列的产品对象(简单的或是复杂的).Builder在最后的一步返回产品,而对于AbstractFactory来说,产品是立即返回的。Composite通常是用Builder生成的。
示例代码1:
#include <string> #include <iostream> #include <vector> using namespace std; //最终的产品类 class Product { private: vector<string> m_product; public: void Add(string strtemp) { m_product.push_back(strtemp); } void Show() { vector<string>::iterator p = m_product.begin(); while (p != m_product.end()) { cout<<*p<<endl; p ++; } } }; //建造者基类 class Builder { public: virtual void BuilderA() = 0; virtual void BuilderB() = 0; virtual Product* GetResult() = 0; }; //第一种建造方式 class ConCreteBuilder1 : public Builder { private: Product* m_product; public: ConCreteBuilder1() { m_product = new Product(); } virtual void BuilderA() { m_product->Add("one"); } virtual void BuilderB() { m_product->Add("two"); } virtual Product* GetResult() { return m_product; } }; //第二种建造方式 class ConcreteBuilder2 : public Builder { private: Product* m_product; public: ConcreteBuilder2() { m_product = new Product(); } virtual void BuilderA() { m_product->Add("A"); } virtual void BuilderB() { m_product->Add("B"); } virtual Product* GetResult() { return m_product; } }; //指挥者类 class Direct { public: void Construct(Builder* temp) { temp->BuilderA(); temp->BuilderB(); } }; //客户端 int main() { Direct* p = new Direct(); Builder* b1 = new ConCreteBuilder1(); Builder* b2 = new ConcreteBuilder2(); p->Construct(b1);//调用第一种方式 Product* pb1 = b1->GetResult(); pb1->Show(); p->Construct(b2);//调用第二种方式 Product* pb2 = b2->GetResult(); pb2->Show(); /* result: one two A B */ return 0; } |
示例代码2:
#include <string> #include <iostream> #include <vector> using namespace std; class Person { public: virtual void CreateHead() = 0; virtual void CreateHand() = 0; virtual void CreateBody() = 0; virtual void CreateFoot() = 0; }; class ThinPerson : public Person { public: virtual void CreateHead() { cout<<"thin head"<<endl; } virtual void CreateHand() { cout<<"thin hand"<<endl; } virtual void CreateBody() { cout<<"thin body"<<endl; } virtual void CreateFoot() { cout<<"thin foot"<<endl; } }; class ThickPerson : public Person { public: virtual void CreateHead() { cout<<"ThickPerson head"<<endl; } virtual void CreateHand() { cout<<"ThickPerson hand"<<endl; } virtual void CreateBody() { cout<<"ThickPerson body"<<endl; } virtual void CreateFoot() { cout<<"ThickPerson foot"<<endl; } }; //指挥者类 class Direct { private: Person* p; public: Direct(Person* temp) {p = temp;} void Create() { p->CreateHead(); p->CreateHand(); p->CreateBody(); p->CreateFoot(); } }; //客户端代码 int main() { Person* p = new ThickPerson(); Direct* d = new Direct(p); d->Create(); delete d; delete p; /*result: ThickPerson head ThickPerson hand ThickPerson body ThickPerson foot */ return 0; } |
示例代码3:
product.h:
#ifndef _PRODUCT_H_ #define _PRODUCT_H_ class Product { public: Product(); ~Product(); void ProducePart(); protected: private: }; class ProductPart { public: ProductPart(); ~ProductPart(); ProductPart* BuildPart(); protected: private: }; #endif//~_PRODUCT_H_ |
product.cpp:
#include "product.h" #include <iostream> using namespace std; Product::Product() { ProducePart(); cout<<"return a product"<<endl; } Product::~Product() { } void Product::ProducePart() { cout<<"build part of product ..."<<endl; } ProductPart::ProductPart() { cout<<"build productpart ..."<<endl; } ProductPart::~ProductPart() { } ProductPart* ProductPart::BuildPart() { return new ProductPart; }
|
builder.h:
#ifndef _BUILDER_H #define _BUILDER_H #include <string> using namespace std; class Product; class Builder { public: virtual ~Builder(); virtual void BuildPartA(const string& buildPara) = 0; virtual void BuildPartB(const string& buildPara) = 0; virtual void BuildPartC(const string& buildPara) = 0; virtual Product* GetProduct() = 0; protected: Builder(); private: }; class ConcreteBuilder : public Builder { public: ConcreteBuilder(); ~ConcreteBuilder(); void BuildPartA(const string& buildPara); void BuildPartB(const string& buildPara); void BuildPartC(const string& buildPara); Product* GetProduct(); protected: private: }; #endif//~_BUILDER_H |
builder.cpp:
#include "builder.h" #include "product.h" #include <iostream> using namespace std; Builder::Builder() { } Builder::~Builder() { } ConcreteBuilder::ConcreteBuilder() { } ConcreteBuilder::~ConcreteBuilder() { } void ConcreteBuilder::BuildPartA(const string& buildPara) { cout<<"Step1:Build PartA ..."<<buildPara<<endl; } void ConcreteBuilder::BuildPartB(const string& buildPara) { cout<<"Step1:Build PartB ..."<<buildPara<<endl; } void ConcreteBuilder::BuildPartC(const string& buildPara) { cout<<"Step1:Build PartC ..."<<buildPara<<endl; } Product* ConcreteBuilder::GetProduct() { BuildPartA("pre-defined"); BuildPartB("pre-defined"); BuildPartC("pre-defined"); return new Product(); } |
director.h:
#ifndef _DIRECTOR_H_ #define _DIRECTOR_H_ class Builder; class Director { public: Director(Builder* bld); ~Director(); void Construct(); protected: private: Builder* _bld; }; #endif//~_DIRECTOR_H_ |
director.cpp:
#include "director.h" #include "builder.h" Director::Director(Builder* bld) { _bld = bld; } Director::~Director() { } void Director::Construct() { _bld->BuildPartA("user-defined"); _bld->BuildPartB("user-defined"); _bld->BuildPartC("user-defined"); } |
main.cpp:
#include "builder.h" #include "product.h" #include "director.h" #include <iostream> using namespace std; int main() { Director* d = new Director(new ConcreteBuilder()); d->Construct(); /*result: Step1:Build PartA ...user-defined Step1:Build PartB ...user-defined Step1:Build PartC ...user-defined */ return 0; } |
建造者模式结构图:
|