1.简单工厂模式:
- 简单工厂模式(Simple factory pattern):包含三个角色。
- 工厂角色:包含创建所有实例的内部逻辑,创建所需要的产品对象。
- 抽象产品角色:简单工厂模式中所有具体产品角色的父类,它负责所有具体产品实例中的共有接口。
- 具体产品角色:简单工厂模式中所创建的实例对象。
- 简单工厂模式的缺点:只有在工厂类中修改代码达到新增类的功能会违背开放封闭原则,不易于代码的扩充。因为简单工厂模式违背了开闭原则,所以gof不将其归属于23种设计模式之一。
- 简单工厂模式案例如下:需要修改工厂角色中的CreateObject方法才能新增生产其他产品功能。
#include<iostream>
using std::cout;
using std::endl;
//声明一个抽象基类Product
class Product
{
public:
virtual void Show() = 0;
//为了在通过基类指针释放在堆上分配内存的派生类对象时
//不发生内存泄漏,需要在基类中将析构函数声明为虚析构函数。如果不这
//样做,将导致派生类对象中的基类成分资源被释放,而派生类中的资源没
//被释放,导致资源泄漏。
virtual ~Product()
{
cout << "The product's destructor are called" << endl;
}
};
class ProductA :public Product
{
public:
virtual void Show()
{
cout << "I am product A" << endl;
}
~ProductA()
{
cout << "The productA's destructor are called" << endl;
}
};
class ProductB :public Product
{
public:
virtual void Show()
{
cout << "I am product B" << endl;
}
~ProductB()
{
cout << "The productB's destructor are called" << endl;
}
};
//声明一个产品类型的枚举类型
typedef enum ProductType
{
TypeA, TypeB
} pt;
//一个工厂
class Factory
{
public:
Product* CreateObject(pt type);
~Factory()
{
cout << "The factory's destructor are called" << endl;
}
};
Product* Factory::CreateObject(pt type)
{
switch (type)
{
case TypeA:
return new ProductA();
case TypeB:
return new ProductB();
default:
return NULL;
}
}
//在继承,虚函数重写,有父类指针或者引用都满足的前提下发生多态
void ObjPlay(Product* product)
{
if (product)
{
product->Show();
}
if (product)
{
delete product;
}
return;
}
int main()
{
Factory* factory = NULL;
factory = new Factory();
if (factory != NULL)
{
ObjPlay(factory->CreateObject(TypeA));
ObjPlay(factory->CreateObject(TypeB));
delete factory;
}
return 0;
}
2.工厂模式:
- 工厂模式(Factory pattern)(工厂方法模式又称多态模式):定义一个创建产品对象的接口,将实际创建工作推迟到子类中。工厂模式包括抽象工厂角色,具体工厂角色,抽象产品角色,具体产品角色。
- 优点是可以在不违背开放封闭原则的前提下对抽象工厂类进行功能的扩展。缺点是每次新增一个产品,就需要增加一个具体工厂类。这相对于简单工厂模式来说需要定义更多的类。
- 工厂方法模式的简单案例
#include<iostream>
using std::endl;
using std::cout;
class Fruit
{
public:
virtual void Process() = 0;
virtual ~Fruit()
{
}
};
class Banana: public Fruit
{
public:
virtual void Process()
{
cout<<"Banana are processed"<<endl;
}
};
class Apple: public Fruit
{
public:
virtual void Process()
{
cout<<"Apple are processed"<<endl;
}
};
class AbFactory
{
public:
virtual Fruit * CreateObj() = 0;
virtual ~AbFactory()
{
}
};
class BananaFactory: public AbFactory
{
public:
virtual Fruit * CreateObj()
{
return new Banana();
}
};
class AppleFactory: public AbFactory
{
public:
virtual Fruit * CreateObj()
{
return new Apple();
}
};
int main()
{
Fruit * fruit = NULL;
AbFactory * factory = NULL;
factory = new AppleFactory();
if(factory != NULL)
{
fruit = factory->CreateObj();
}
if(fruit != NULL)
{
fruit->Process();
}
delete factory;
delete fruit;
factory = new BananaFactory();
if(factory != NULL)
{
fruit = factory->CreateObj();
}
if(fruit != NULL)
{
fruit->Process();
}
delete factory;
delete fruit;
return 0;
}
3.抽象工厂模式
- 抽象工厂模式:抽象工厂模式可以向客户端提供多个接口,使得客户端在不必指定产品的具体类型下,能够创建多个产品族的产品对象。
- 抽象工厂模式与工厂模式的区别:工厂模式要么只能生产产品A,要么只能生产产品B,一次性只能生产一件产品。抽象工厂模式可以在抽象工厂类中定义一套生产一族
产品的接口,将具体实现推迟到子类中。所以抽象工厂模式可以生产一族产品。
- 抽象工厂模式示例:
#include<iostream>
using std::endl;
using std::cout;
class Fruit
{
public:
virtual void Process() = 0;
virtual ~Fruit()
{
}
};
class Banana : public Fruit
{
public:
virtual void Process()
{
cout << "Banana are processed" << endl;
}
};
class Apple : public Fruit
{
public:
virtual void Process()
{
cout << "Apple are processed" << endl;
}
};
class AbFactory
{
public:
virtual Fruit* BananaInterface() = 0;
virtual Fruit* AppleInterface() = 0;
virtual ~AbFactory()
{
}
};
class NorthFactory : public AbFactory
{
public:
virtual Fruit* BananaInterface()
{
return new Banana();
}
virtual Fruit* AppleInterface()
{
return new Apple();
}
};
int main()
{
Fruit* fruit = NULL;
AbFactory* factory = NULL;
factory = new NorthFactory();
if (factory != NULL)
{
fruit = factory->AppleInterface();
fruit->Process();
}
if (fruit != NULL)
{
delete fruit;
}
fruit = factory->BananaInterface();
fruit->Process();
if (fruit != NULL)
{
delete fruit;
}
delete factory;
return 0;
}