根据系列博客的第一篇设计模式-01-设计模式简介及分类对24设计模式的分类,本文对第一类—–创建型模式,进行举例分析
一.工厂模式
修正了简单工厂模式中不遵守开放-封闭原则。工厂方法模式把选择判断移到了客户端去实现,如果想添加新功能就不用修改原来的类,直接修改客户端即可
代码举例:
#include <string>
#include <iostream>
using namespace std;
//实例基类,相当于Product(为了方便,没用抽象)
class LeiFeng
{
public:
virtual void Sweep()
{
cout<<"雷锋扫地"<<endl;
}
};
//学雷锋的大学生,相当于ConcreteProduct
class Student: public LeiFeng
{
public:
virtual void Sweep()
{
cout<<"大学生扫地"<<endl;
}
};
//学雷锋的志愿者,相当于ConcreteProduct
class Volenter: public LeiFeng
{
public :
virtual void Sweep()
{
cout<<"志愿者"<<endl;
}
};
//工场基类Creator
class LeiFengFactory
{
public:
virtual LeiFeng* CreateLeiFeng()
{
return new LeiFeng();
}
};
//工场具体类
class StudentFactory : public LeiFengFactory
{
public :
virtual LeiFeng* CreateLeiFeng()
{
return new Student();
}
};
class VolenterFactory : public LeiFengFactory
{
public:
virtual LeiFeng* CreateLeiFeng()
{
return new Volenter();
}
};
//客户端
int main()
{
LeiFengFactory *sf=new LeiFengFactory();
LeiFeng *s=sf->CreateLeiFeng();
s->Sweep();
delete s;
delete sf;
return 0;
}
二.抽象工厂模式
定义了一个创建一系列相关或相互依赖的接口,而无需指定它们的具体类。
用于交换产品系列,如ACCESS->SQL SERVER;产品的具体类名被具体工厂的实现分离
代码举例:
#include <string>
#include <iostream>
#include <vector>
using namespace std;
//用户抽象接口
class IUser
{
public :
virtual void GetUser()=0;
virtual void InsertUser()=0;
};
//部门抽象接口
class IDepartment
{
public:
virtual void GetDepartment()=0;
virtual void InsertDepartment()=0;
};
//ACCESS用户
class CAccessUser : public IUser
{
public:
virtual void GetUser()
{
cout<<"Access GetUser"<<endl;
}
virtual void InsertUser()
{
cout<<"Access InsertUser"<<endl;
}
};
//ACCESS部门
class CAccessDepartment : public IDepartment
{
public:
virtual void GetDepartment()
{
cout<<"Access GetDepartment"<<endl;
}
virtual void InsertDepartment()
{
cout<<"Access InsertDepartment"<<endl;
}
};
//SQL用户
class CSqlUser : public IUser
{
public:
virtual void GetUser()
{
cout<<"Sql User"<<endl;
}
virtual void InsertUser()
{
cout<<"Sql User"<<endl;
}
};
//SQL部门类
class CSqlDepartment: public IDepartment
{
public:
virtual void GetDepartment()
{
cout<<"sql getDepartment"<<endl;
}
virtual void InsertDepartment()
{
cout<<"sql insertdepartment"<<endl;
}
};
//抽象工厂
class IFactory
{
public:
virtual IUser* CreateUser()=0;
virtual IDepartment* CreateDepartment()=0;
};
//ACCESS工厂
class AccessFactory : public IFactory
{
public:
virtual IUser* CreateUser()
{
return new CAccessUser();
}
virtual IDepartment* CreateDepartment()
{
return new CAccessDepartment();
}
};
//SQL工厂
class SqlFactory : public IFactory
{
public:
virtual IUser* CreateUser()
{
return new CSqlUser();
}
virtual IDepartment* CreateDepartment()
{
return new CSqlDepartment();
}
};
客户端:
int main()
{
IFactory* factory= new SqlFactory();
IUser* user=factory->CreateUser();
IDepartment* depart = factory->CreateDepartment();
user->GetUser();
depart->GetDepartment();
return 0;
}
三.单例模式
保证一个类仅有一个实例,并提供一个访问它的全局访问点
代码举例:
#include <iostream>
#include <string>
#include <vector>
using namespace std;
class Singelton
{
private:
Singelton(){}
static Singelton* singel;
public:
static Singelton* GetInstance()
{
if(singel == NULL)
{
singel = new Singelton();
}
return singel;
}
};
Singelton* Singelton::singel = NULL;//注意静态变量类外初始化
客户端:
int main()
{
Singelton* s1=Singelton::GetInstance();
Singelton* s2=Singelton::GetInstance();
if(s1 == s2)
cout<<"ok"<<endl;
else
cout<<"no"<<endl;
return 0;
}
四.建造者模式
在当创建复杂对象的算法应该独立于该对象的组成部分以及它们的装配方式时适用。
代码举例:
//例一
#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();
return 0;
}
//例二
#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->CreateBody();
p->CreateHand();
p->CreateFoot();
}
};
//客户端代码:
int main()
{
Person *p=new ThickPerson();
Direct *d= new Direct(p);
d->Create();
delete d;
delete p;
return 0;
}
五.原型模式
从一个对象再创建另外一个可定制的对象,而无需知道任何创建的细节。并能提高创建的性能。 说白了就COPY技术,把一个对象完整的COPY出一份。
代码举例:
#include<iostream>
#include <vector>
#include <string>
using namespace std;
class Prototype //抽象基类
{
private:
string m_strName;
public:
Prototype(string strName){ m_strName = strName; }
Prototype() { m_strName = " "; }
void Show()
{
cout<<m_strName<<endl;
}
virtual Prototype* Clone() = 0 ;
} ;
// class ConcretePrototype1
class ConcretePrototype1 : public Prototype
{
public:
ConcretePrototype1(string strName) : Prototype(strName){}
ConcretePrototype1(){}
virtual Prototype* Clone()
{
ConcretePrototype1 *p = new ConcretePrototype1() ;
*p = *this ; //复制对象
return p ;
}
} ;
// class ConcretePrototype2
class ConcretePrototype2 : public Prototype
{
public:
ConcretePrototype2(string strName) : Prototype(strName){}
ConcretePrototype2(){}
virtual Prototype* Clone()
{
ConcretePrototype2 *p = new ConcretePrototype2() ;
*p = *this ; //复制对象
return p ;
}
} ;
//客户端
int main()
{
ConcretePrototype1* test = new ConcretePrototype1("小王");
ConcretePrototype2* test2 = (ConcretePrototype2*)test->Clone();
test->Show();
test2->Show();
return 0;
}