设计模式之抽象工厂模式(AbstactFactory ) 创建型模式

AbstractFactory模式典型的结构图为:

设计模式之抽象工厂模式(AbstactFactory ) 创建型模式

AbstractFactory模式是为创建一组(有多类)相关或依赖的对象提供创建接口,而 Factory模式正如我在相应的文档中分析的是为一类对象提供创建接口或延迟对象的创建到子类中实现。并且可以看到,abstractFactory 模式通常都是使用 Factory 模式实现(ConcreteFactory1)。

AbstractFactory模式关键就是将这一组对象的创建封装到一个用于创建对象的类(ConcreteFactory)中,维护这样一个创建类总比维护n多相关对象的创建过程要简单的多。

-------------------------Product.h---------------------------------
#pragma once

class AbstractProductA
{
public:
virtual ~AbstractProductA() = 0;
protected:
AbstractProductA();
};

class AbstractProductB
{
public:
virtual ~AbstractProductB() = 0;
protected:
AbstractProductB();
};

class ProductA0 : public AbstractProductA
{
public:
ProductA0();
~ProductA0();
};

class ProductA1 : public AbstractProductA
{
public:
ProductA1();
~ProductA1();
};


class ProductB0 : public AbstractProductB
{
public:
ProductB0();
~ProductB0();
};

class ProductB1 : public AbstractProductB
{
public:
ProductB1();
~ProductB1();
};

-------------------------Product.cpp-------------------------------
#include "stdafx.h"
#include "Product.h"
#include <iostream>
using namespace std;

AbstractProductA::AbstractProductA() { }

AbstractProductA::~AbstractProductA() { }

AbstractProductB::AbstractProductB() { }

AbstractProductB::~AbstractProductB() { }

ProductA0::ProductA0()
{
cout << "ProductA0 constructor" << endl;
}

ProductA0::~ProductA0() { }

ProductB0::ProductB0()
{
cout << "ProductB0 constructor" << endl;
}

ProductB0::~ProductB0() { }

ProductA1::ProductA1()
{
cout << "ProductA1 constructor" << endl;
}

ProductA1::~ProductA1() { }

ProductB1::ProductB1()
{
cout << "ProductB1 constructor" << endl;
}

ProductB1::~ProductB1() { }
-------------------------Factory.h---------------------------------
#pragma once

class AbstractProductA;
class AbstractProductB;

class AbstructFactory
{
public:
virtual ~AbstructFactory() = 0;
AbstructFactory();

virtual AbstractProductA* CreateProductA() = 0;
virtual AbstractProductB* CreateProductB() = 0;
};

class

ConcreteFactory0 : public AbstructFactory
{
public:
ConcreteFactory0();
~ConcreteFactory0();

AbstractProductA* CreateProductA();
AbstractProductB* CreateProductB();
};

class ConcreteFactory1 : public AbstructFactory
{
public:
ConcreteFactory1();
~ConcreteFactory1();

AbstractProductA* CreateProductA();
AbstractProductB* CreateProductB();
};

-------------------------Factory.cpp-------------------------------
#include "stdafx.h"
#include "Product.h"
#include "Factory.h"

AbstructFactory::AbstructFactory() { }

AbstructFactory::~AbstructFactory() { }

ConcreteFactory0::ConcreteFactory0() { }
ConcreteFactory0::~ConcreteFactory0() { }

AbstractProductA* ConcreteFactory0::CreateProductA()
{
return new ProductA0;
}

AbstractProductB* ConcreteFactory0::CreateProductB()
{
return new ProductB0;
}

ConcreteFactory1::ConcreteFactory1() { }
ConcreteFactory1::~ConcreteFactory1() { }

AbstractProductA* ConcreteFactory1::CreateProductA()
{
return new ProductA1;
}

AbstractProductB* ConcreteFactory1::CreateProductB()
{
return new ProductB1;
}
-------------------------main.cpp----------------------------------
#include "stdafx.h"
#include "Product.h"
#include "Factory.h"
#include <iostream>

using namespace std;

int main()
{
ConcreteFactory0* fac0 = new ConcreteFactory0;
ConcreteFactory1* fac1 = new ConcreteFactory1;

AbstractProductA* proA0 = fac0->CreateProductA();
AbstractProductB* proB0 = fac0->CreateProductB();
AbstractProductA* proA1 = fac1->CreateProductA();
AbstractProductB* proB1 = fac1->CreateProductB();

delete proB1;
delete proA1;
delete proB0;
delete proA0;
delete fac1;
delete fac0;

return 0;
}
-------------------------------------------------------------------

你可能感兴趣的:(设计模式)