三种工厂模式:简单工厂模式、工厂模式、抽象工厂模式。
1.简单工厂模式
简单工厂模式是工厂模式中最简单的模式,他可以隐藏创建具体对象的细节,也可以隐藏类的实现细节。在需要进行增加产品时,不需要破坏原有的产品类以及实现,只要增加新的实现类,以及修改工厂类即可。
优点:比较简单,容易实现。缺点:当有新产品增加时,需要破坏工厂类,违反了开放-封闭原则。
实现UML类图如下所示:
实现代码如下:
BaseProduct类实现:
#ifndef BASEPRODUCT_H
#define BASEPRODUCT_H
class BaseProduct
{
public:
BaseProduct();
public:
virtual void production() = 0;
};
#endif // BASEPRODUCT_H
#ifndef PRODUCTA_H
#define PRODUCTA_H
#include "baseproduct.h"
class ProductA : public BaseProduct
{
public:
ProductA();
public:
void production();
};
#endif // PRODUCTA_H
#include "producta.h"
#include <iostream>
using namespace std;
ProductA::ProductA()
{
}
void ProductA::production()
{
std::cout << "product A" << std::endl;
}
#ifndef PRODUCTB_H
#define PRODUCTB_H
#include "baseproduct.h"
class ProductB : public BaseProduct
{
public:
ProductB();
public:
void production();
};
#endif // PRODUCTB_H
#include "productb.h"
#include <iostream>
using namespace std;
ProductB::ProductB()
{
}
void ProductB::production()
{
cout << "Product B" << endl;
}
#ifndef PRODUCTFACTORY_H
#define PRODUCTFACTORY_H
#include "baseproduct.h"
class ProductFactory
{
public:
ProductFactory();
public:
virtual BaseProduct * CreateProduct(int pType) = 0;
};
#endif // PRODUCTFACTORY_H
#include "productfactory.h"
ProductFactory::ProductFactory()
{
}
#ifndef SIMPLEFACTORY_H
#define SIMPLEFACTORY_H
#include "baseproduct.h"
#include "productfactory.h"
#include "producta.h"
#include "productb.h"
class SimpleFactory : public ProductFactory
{
public:
SimpleFactory();
public:
BaseProduct * CreateProduct(int pType);
};
#endif // SIMPLEFACTORY_H
#include "simplefactory.h"
#include <iostream>
using namespace std;
SimpleFactory::SimpleFactory()
{
}
BaseProduct * SimpleFactory::CreateProduct(int pType)
{
BaseProduct * pobj = NULL;
switch (pType) {
case 1:
pobj = new ProductA();
break;
case 2:
pobj = new ProductB();
break;
default:
break;
}
return pobj;
}
使用简单工厂调用对象方法:SimpleFactory * factory = new SimpleFactory();
BaseProduct * product = factory->CreateProduct(1);
product->production();
product = factory->CreateProduct(2);
product->production();
输出:product A;
product B;
2.工厂模式
简单工厂模式中的缺点是每当需要增加产品类时,就需要破坏工厂类,这就违背了开放-封闭原则。工厂方法解决了这个问题,但是实现的功能不变。但是也存在一个缺点:每当增加一个新的产品类时,就需要增加一个工厂类。工厂模式类图如下:C++代码实现如下:
#ifndef BASEPRODUCT_H
#define BASEPRODUCT_H
class BaseProduct
{
public:
BaseProduct();
public:
virtual void production() = 0;
};
#endif // BASEPRODUCT_H
#ifndef PRODUCTA_H
#define PRODUCTA_H
#include "baseproduct.h"
class ProductA : public BaseProduct
{
public:
ProductA();
public:
void production();
};
#endif // PRODUCTA_H
#include "producta.h"
#include <iostream>
using namespace std;
ProductA::ProductA()
{
}
void ProductA::production()
{
std::cout << "product A" << std::endl;
}
#ifndef PRODUCTB_H
#define PRODUCTB_H
#include "baseproduct.h"
class ProductB : public BaseProduct
{
public:
ProductB();
public:
void production();
};
#endif // PRODUCTB_H
#include "productb.h"
#include <iostream>
using namespace std;
ProductB::ProductB()
{
}
void ProductB::production()
{
cout << "Product B" << endl;
}
#ifndef FACTORYA_H
#define FACTORYA_H
#include "productfactory.h"
#include "baseproduct.h"
#include "producta.h"
class FactoryA : public ProductFactory
{
public:
FactoryA();
public:
BaseProduct * CreateProduct(int pType);
};
#endif // FACTORYA_H
#include "factorya.h"
FactoryA::FactoryA()
{
}
BaseProduct * FactoryA::CreateProduct(int pType)
{
BaseProduct * pProductA = new ProductA();
return pProductA;
}
#ifndef FACTORYB_H
#define FACTORYB_H
#include "productfactory.h"
#include "productb.h"
class FactoryB : public ProductFactory
{
public:
FactoryB();
public:
BaseProduct * CreateProduct(int pType);
};
#endif // FACTORYB_H
#include "factoryb.h"
FactoryB::FactoryB()
{
}
BaseProduct * FactoryB::CreateProduct(int pType)
{
BaseProduct *pProductB = new ProductB();
return pProductB;
}
客户端使用工厂方法:
FactoryA * fa = new FactoryA();
FactoryB * fb = new FactoryB();
BaseProduct * productA = fa->CreateProduct(1);//沿用简单工厂模式中的基类,因此这个函数没有改动,参数可以任意填写
BaseProduct * productB = fb->CreateProduct(2);//沿用简单工厂模式中的基类,因此这个函数没有改动,参数可以任意填写
productA->production();
productB->production();
输出:
product A
product B
抽象工厂方法
抽象工厂模式解决了在简单工厂和工厂模式中存在的问题,但是还是存在一个问题,如果增加产品或者增加产品类型,就会破坏工厂类的方法。因此,抽象工厂方法也是存在缺陷的,这就需要我们在实际应用中根据需求,来选择使用何种设计模式。抽象工厂方法UML图如下:
C++代码实现:
#ifndef BASEPRODUCTA_H
#define BASEPRODUCTA_H
class BaseProductA
{
public:
BaseProductA();
public:
virtual void productA()=0;
};
#endif // BASEPRODUCTA_H
#ifndef PRODUCTA1_H
#define PRODUCTA1_H
#include "baseproducta.h"
class ProductA1 : public BaseProductA
{
public:
ProductA1();
public:
void productA();
};
#endif // PRODUCTA1_H
#include "producta1.h"
#include <iostream>
using namespace std;
ProductA1::ProductA1()
{
}
void ProductA1::productA()
{
cout << "product A1" << endl;
}
#ifndef PRODUCTA2_H
#define PRODUCTA2_H
#include "baseproducta.h"
class ProductA2 : public BaseProductA
{
public:
ProductA2();
public:
void productA();
};
#endif // PRODUCTA2_H
#include "producta2.h"
#include <iostream>
using namespace std;
ProductA2::ProductA2()
{
}
void ProductA2::productA()
{
cout << "product A2" << endl;
}
#ifndef BASEPRODUCTB_H
#define BASEPRODUCTB_H
class BaseProductB
{
public:
BaseProductB();
public:
virtual void productB() = 0;
};
#endif // BASEPRODUCTB_H
#ifndef PRODUCTB1_H
#define PRODUCTB1_H
#include "baseproductb.h"
class ProductB1 : public BaseProductB
{
public:
ProductB1();
public:
void productB();
};
#endif // PRODUCTB1_H
#include "productb1.h"
#include <iostream>
using namespace std;
ProductB1::ProductB1()
{
}
void ProductB1::productB()
{
cout << "product B1" << endl;
}
#ifndef PRODUCTB2_H
#define PRODUCTB2_H
#include "baseproductb.h"
class ProductB2 : public BaseProductB
{
public:
ProductB2();
public:
void productB();
};
#endif // PRODUCTB2_H
#include "productb2.h"
#include <iostream>
using namespace std;
ProductB2::ProductB2()
{
}
void ProductB2::productB()
{
cout << "Product B2" << endl;
}
#ifndef PRODUCTFACTORY_H
#define PRODUCTFACTORY_H
#include "baseproduct.h"
#include "baseproducta.h"
#include "baseproductb.h"
class ProductFactory
{
public:
ProductFactory();
public:
virtual BaseProduct * CreateProduct(int pType) = 0;
virtual BaseProductA * CreateProductA() = 0;
virtual BaseProductB * CreateProductB() = 0;
};
#endif // PRODUCTFACTORY_H
#ifndef FACTORYA_H
#define FACTORYA_H
#include "productfactory.h"
#include "baseproduct.h"
#include "producta.h"
#include "baseproducta.h"
#include "baseproductb.h"
class FactoryA : public ProductFactory
{
public:
FactoryA();
public:
BaseProduct * CreateProduct(int pType);
BaseProductA * CreateProductA();
BaseProductB * CreateProductB();
};
#endif // FACTORYA_H
#include "factorya.h"
#include "producta1.h"
#include "productb1.h"
FactoryA::FactoryA()
{
}
BaseProduct * FactoryA::CreateProduct(int pType)
{
BaseProduct * pProductA = new ProductA();
return pProductA;
}
BaseProductA * FactoryA::CreateProductA()
{
return (new ProductA1());
}
BaseProductB * FactoryA::CreateProductB()
{
return (new ProductB1);
}
#ifndef FACTORYB_H
#define FACTORYB_H
#include "productfactory.h"
#include "productb.h"
#include "baseproducta.h"
#include "baseproductb.h"
class FactoryB : public ProductFactory
{
public:
FactoryB();
public:
BaseProduct * CreateProduct(int pType);
BaseProductA * CreateProductA();
BaseProductB * CreateProductB();
};
#endif // FACTORYB_H
#include "factoryb.h"
#include "producta2.h"
#include "productb2.h"
FactoryB::FactoryB()
{
}
BaseProduct * FactoryB::CreateProduct(int pType)
{
BaseProduct *pProductB = new ProductB();
return pProductB;
}
BaseProductA * FactoryB::CreateProductA()
{
return (new ProductA2);
}
BaseProductB * FactoryB::CreateProductB()
{
return (new ProductB2);
}
客户端调用抽象工厂方法:
<pre name="code" class="cpp"> FactoryA *fa = new FactoryA();
FactoryB *fb = new FactoryB();
BaseProductA *pa1 = fa->CreateProductA();
BaseProductB *pb1 = fa->CreateProductB();
BaseProductA *pa2 = fb->CreateProductA();
BaseProductB *pb2 = fb->CreateProductB();
pa1->productA();
pa2->productA();
pb1->productB();
pb2->productB();
输出:
product A1
product A2
product B1
Product B2