您的位置:首页 > 编程语言 > Go语言

Gof 设计模式 创建型

2016-05-25 00:32 417 查看
#include<iostream>
using namespace std;

#if 0
class A{
public:
virtual void test()
{
cout << "test A" << endl;
}
};

class B{
public:
virtual void test()
{
cout << "test B" << endl;
}
};

class C :public A, public B
{

};
int main()
{
cout << sizeof(C) << endl;
int ar[100];
delete ar;
return 0;
}
#endif
//单例模式
#if 0
class singleclass
{
public:
static singleclass *getobject()
{
//lock();
if (ms == NULL)
{
ms = new singleclass();
}
//unlock();
return ms;
}
void test()
{
cout << "singleclass" << endl;
}
private:
singleclass(){}
static singleclass *ms;
};
singleclass* singleclass::ms = NULL;

int main()
{
singleclass *p = singleclass::getobject();
p->test();
return 0;
}
#endif

//          原型模式
//原型模式是创建型模式的一种,
//其特点在于通过“复制”一个已经存在的实例来返回新的实例,而不是新建实例。
//被复制的实例就是我们所称的“原型”,这个原型是可定制的。
//原型模式多用于创建复杂的或者耗时的实例,
//因为这种情况下,复制一个已经存在的实例使程序运行更高效;
//或者创建值相等,只是命名不一样的同类数据
#if 0
class Resume
{
protected:
char *name;
public:
Resume() {}
virtual ~Resume() {}
virtual Resume* Clone() { return NULL; }
virtual void Set(char *n) {}
virtual void Show() {}
};
class ResumeA : public Resume
{
public:
ResumeA(const char *str);  //构造函数
ResumeA(const ResumeA &r); //拷贝构造函数
~ResumeA();                //析构函数
ResumeA* Clone();          //克隆,关键所在
void Show();               //显示内容
};
ResumeA::ResumeA(const char *str)
{
if (str == NULL) {
name = new char[1];
name[0] = '\0';
}
else {
name = new char[strlen(str) + 1];
strcpy(name, str);
}
}
ResumeA::~ResumeA() { delete[] name; }
ResumeA::ResumeA(const ResumeA &r) {
name = new char[strlen(r.name) + 1];
strcpy(name, r.name);
}
ResumeA* ResumeA::Clone() {
return new ResumeA(*this);
}
void ResumeA::Show() {
cout << "ResumeA name : " << name << endl;
}

class ResumeB : public Resume
{
public:
ResumeB(const char *str);  //构造函数
ResumeB(const ResumeB &r); //拷贝构造函数
~ResumeB();                //析构函数
ResumeB* Clone();          //克隆,关键所在
void Show();               //显示内容
};
ResumeB::ResumeB(const char *str)
{
if (str == NULL) {
name = new char[1];
name[0] = '\0';
}
else {
name = new char[strlen(str) + 1];
strcpy(name, str);
}
}
ResumeB::~ResumeB() { delete[] name; }
ResumeB::ResumeB(const ResumeB &r) {
name = new char[strlen(r.name) + 1];
strcpy(name, r.name);
}
ResumeB* ResumeB::Clone() {
return new ResumeB(*this);
}
void ResumeB::Show() {
cout << "ResumeB name : " << name << endl;
}

int main(int argc, char* argv[])
{
Resume *r1 = new ResumeA("A");
Resume *r2 = new ResumeB("B");
Resume *r3 = r1->Clone();
Resume *r4 = r2->Clone();
r1->Show(); r2->Show();
//删除r1,r2
delete r1; delete r2;
r1 = r2 = NULL;
//深拷贝所以对r3,r4无影响
r3->Show(); r4->Show();
delete r3; delete r4;
r3 = r4 = NULL;
return 0;
}
#endif

//        建造模式
//生成器模式(英:Builder Pattern)是一种设计模式,
//又名:建造模式,是一种对象构建模式。
//它可以将复杂对象的建造过程抽象出来(抽象类别),
//使这个抽象过程的不同实现方法可以构造出不同表现(属性)的对象。
#if 0
class Product{
public:
string ProductName;
};
class ProductA : public Product{
public:
ProductA() : ProductName("ProductA"){}
};
class ProductB :public Product{
public:
ProductB() : ProductName("ProductB"){}
};

class Creator{
public:
inline Product *CreateProduct(){
return DoCreateProduct();
}
protected:
virtual Product *DoCreateProduct() = 0;
};
class CreatorA : public Creator{
public:
Product *DoCreateProduct(){
return new ProductA;
}
};
class CreatorB : public Creator{
public:
Product *DoCreateProduct(){
return new ProductB;
}
};

int main(int argc, char **argv){
Creator *creator = new CreatorA;
Product *product = creator->CreateProduct();
cout << product->ProductName << endl;//"ProductA"
delete creator;
delete product;
creator = new CreatorB;
product = creator->CreateProduct();
cout << product->ProductName << endl;//"ProductB"
delete creator;
delete product;
return 0;
}
#endif

//          工厂方法模式
//工厂方法模式(英语:Factory method pattern)是一种实现了“工厂”概念的面向对象设计模式。
//就像其他创建型模式一样,它也是处理在不指定对象具体类型的情况下创建对象的问题。
//工厂方法模式的实质是“定义一个创建对象的接口,但让实现这个接口的类来决定实例化哪个类。
//工厂方法让类的实例化推迟到子类中进行。
#if 0
class IProduct
{
public:
IProduct(){}
~IProduct(){}
};
class Android : public IProduct{};
class Iphone : public IProduct{};
class IFactory
{
public:
IFactory(){}
virtual ~IFactory(){}
virtual IProduct *getproduct() = 0;
};

class AndroidFactory : public IFactory
{
public:
AndroidFactory(){}
~AndroidFactory(){}
IProduct *getproduct()
{
cout << "a Android phone be create" << endl;
return new Android();
}
};
class AppleFactory : public IFactory
{
public:
AppleFactory(){}
~AppleFactory(){}
IProduct *getproduct()
{
cout << "a Apple phone be create" << endl;
return new Iphone();
}
};

int main()
{
IFactory *fac = new AndroidFactory();
IProduct *pro =  fac->getproduct();
fac = new AppleFactory();
pro = fac->getproduct();
return 0;
}
#endif

//抽象工厂模式(英语:Abstract factory pattern)是一种软件开发设计模式。
//抽象工厂模式提供了一种方式,可以将一组具有同一主题的单独的工厂封装起来。
//在正常使用中,客户端程序需要创建抽象工厂的具体实现,然后使用抽象工厂作为接口来创建这一主题的具体对象。
//客户端程序不需要知道(或关心)它从这些内部的工厂方法中获得对象的具体类型,
//因为客户端程序仅使用这些对象的通用接口。抽象工厂模式将一组对象的实现细节与他们的一般使用分离开来。

//AbstractFactory 模式和 Factory 模式的区别是初学(使用)设计模式时候的一个容易引
//起困惑的地方。 实际上, AbstractFactory 模式是为创建一组( 有多类) 相关或依赖的对象提
//供创建接口, 而 Factory 模式正如我在相应的文档中分析的是为一类对象提供创建接口或延
//迟对象的创建到子类中实现。并且可以看到, AbstractFactory 模式通常都是使用 Factory 模
//式实现( ConcreteFactory1)。

class AbstractIProduct
{
public:
AbstractIProduct(){}
~AbstractIProduct(){}
};
class Androidphone : public AbstractIProduct{};
class Androidpad : public AbstractIProduct{};
class Iphone : public AbstractIProduct{};
class Ipad : public AbstractIProduct{};
class IFactory
{
public:
IFactory(){}
virtual ~IFactory(){}
virtual AbstractIProduct *getpad() = 0;
virtual AbstractIProduct *getphone() = 0;
};

class AndroidFactory : public IFactory
{
public:
AndroidFactory(){}
~AndroidFactory(){}
AbstractIProduct *getphone()
{
cout << "a Android phone be create" << endl;
return new Androidphone();
}
AbstractIProduct *getpad()
{
cout << "a Android pad be create" << endl;
return new Androidpad();
}
};
class AppleFactory : public IFactory
{
public:
AppleFactory(){}
~AppleFactory(){}
AbstractIProduct *getphone()
{
cout << "a Apple phone be create" << endl;
return new Iphone();
}
AbstractIProduct *getpad()
{
cout << "a Apple pad be create" << endl;
return new Ipad();
}
};

int main()
{
IFactory *fac = new AndroidFactory();
AbstractIProduct *pro = fac->getpad();
pro = fac->getphone();
fac = new AppleFactory();
pro = fac->getpad();
pro = fac->getphone();
return 0;
}


                                            
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: