- Published on
设计模式(1)——抽象工厂 Abstract Factory
- Authors
- Name
- Leon
一、Abstract Factory (抽象工厂,对象创建型模式)
1. 问题:
AbstractFactory(抽象工厂)用来创建一类(或称一种)产品(具体对象),当对 Facotry 类进行抽象,然后不同种类的工厂做为子类时,就成为 AbastractFactory 模式。
2. 功能:
创建不同种类(一组)具体对象。一个具体工厂(一个AbstractFacotry的具体子类)生产一组具体产品对象。所以多个具体工厂就会生产多组具体产品对象。
3. 意图:
封装一组对象的创建过程,让系统与具体对象解耦。
4. 类图:
5. 抽象工厂与工厂方法的区别:
工厂方法生产一种单一产品,抽象工厂生产一组(有多种)产品。
6. C++实现
AbstractFactory.h
// AbstractFactory.h
#pragma once
class AbstractProductA;
class AbstractProductB;
class AbstracFactory
{
public:
virtual ~AbstracFactory();
virtual AbstractProductA* CreateProductA() = 0;
virtual AbstractProductB* CreateProductB() = 0;
protected:
AbstracFactory();
private:
};
class ConcreteFactory1 : public AbstracFactory {
public:
ConcreteFactory1();
~ConcreteFactory1();
AbstractProductA* CreateProductA();
AbstractProductB* CreateProductB();
protected:
private:
};
class ConcreteFactory2 : public AbstracFactory {
public:
ConcreteFactory2();
~ConcreteFactory2();
AbstractProductA* CreateProductA();
AbstractProductB* CreateProductB();
protected:
private:
};
AbstractFactory.cpp
// AbstractFactory.cpp
#include "AbstractFactory.h"
#include "Product.h"
#include <iostream>
using namespace std;
AbstracFactory::AbstracFactory() {}
AbstracFactory::~AbstracFactory() {}
ConcreteFactory1::ConcreteFactory1() {}
ConcreteFactory1::~ConcreteFactory1() {}
ConcreteFactory2::ConcreteFactory2() {}
ConcreteFactory2::~ConcreteFactory2() {}
AbstractProductA* ConcreteFactory1::CreateProductA() {
return new ProductA1();
}
AbstractProductB* ConcreteFactory1::CreateProductB() {
return new ProductB1();
}
AbstractProductA* ConcreteFactory2::CreateProductA(){
return new ProductA2();
}
AbstractProductB* ConcreteFactory2::CreateProductB() {
return new ProductB2();
}
Product.h
// Product.h
#pragma once
class AbstractProductA {
public:
virtual ~AbstractProductA();
protected:
AbstractProductA();
private:
};
class AbstractProductB {
public:
virtual ~AbstractProductB();
protected:
AbstractProductB();
private:
};
class ProductA1 : public AbstractProductA {
public:
ProductA1();
~ProductA1();
protected:
private:
};
class ProductA2 : public AbstractProductA {
public:
ProductA2();
~ProductA2();
protected:
private:
};
class ProductB1 : public AbstractProductB {
public:
ProductB1();
~ProductB1();
protected:
private:
};
class ProductB2 : public AbstractProductB {
public:
ProductB2();
~ProductB2();
protected:
private:
};
Product.cpp
//Product.cpp
#include "Product.h"
#include <iostream>
using namespace std;
AbstractProductA::AbstractProductA() {}
AbstractProductA::~AbstractProductA(){}
AbstractProductB::AbstractProductB() {}
AbstractProductB::~AbstractProductB(){}
ProductA1::ProductA1() {
cout << "ProductA1" << endl;
}
ProductA1::~ProductA1() { }
ProductA2::ProductA2() {
cout << "ProductA2" << endl;
}
ProductA2::~ProductA2() { }
ProductB1::ProductB1() {
cout << "ProductB1" << endl;
}
ProductB1::~ProductB1() {}
ProductB2::ProductB2() {
cout << "ProductB2" << endl;
}
ProductB2::~ProductB2() {}
main.cpp
// main.cpp
#include "AbstractFactory.h"
#include <iostream>
using namespace std;
int main(int argc, char* argv[]) {
AbstracFactory* f1 = new ConcreteFactory1();
f1->CreateProductA();
f1->CreateProductB();
AbstracFactory* f2 = new ConcreteFactory2();
f2->CreateProductA();
f2->CreateProductB();
return 0;
}