设计模式-创建型-抽象工厂
原创
©著作权归作者所有:来自51CTO博客作者shixin_0125的原创作品,请联系作者获取转载授权,否则将追究法律责任
#pragma once
#ifndef ABSTRACTFACTORY_H
#define ABSTRACTFACTORY_H
class AbstractProductA
{
public:
AbstractProductA() {}
virtual ~AbstractProductA(){};
};
class ConcreateProductA1 : public AbstractProductA
{
public:
ConcreateProductA1();
virtual ~ConcreateProductA1();
};
class ConcreateProductA2 : public AbstractProductA
{
public:
ConcreateProductA2();
virtual ~ConcreateProductA2();
};
class AbstractProductB
{
public:
AbstractProductB() {}
virtual ~AbstractProductB(){};
};
class ConcreateProductB1 : public AbstractProductB
{
public:
ConcreateProductB1();
virtual ~ConcreateProductB1();
};
class ConcreateProductB2 : public AbstractProductB
{
public:
ConcreateProductB2();
virtual ~ConcreateProductB2();
};
class AbstractFactory
{
public:
AbstractFactory(){}
virtual ~AbstractFactory(){}
virtual AbstractProductA* CreateProductA() = 0;
virtual AbstractProductB* CreateProductB() = 0;
};
class ConcreateFactory1 : public AbstractFactory
{
public:
ConcreateFactory1();
virtual ~ConcreateFactory1();
virtual AbstractProductA* CreateProductA();
virtual AbstractProductB* CreateProductB();
};
class ConcreateFactory2 : public AbstractFactory
{
public:
ConcreateFactory2();
virtual ~ConcreateFactory2();
virtual AbstractProductA* CreateProductA();
virtual AbstractProductB* CreateProductB();
};
#endif
#include "StdAfx.h"
#include "abstractfactory_impl.h"
#include <iostream>
ConcreateProductA1::ConcreateProductA1()
{
std::cout << "construction of ConcreateProductA1\n";
}
ConcreateProductA1::~ConcreateProductA1()
{
std::cout << "destruction of ConcreateProductA1\n";
}
ConcreateProductA2::ConcreateProductA2()
{
std::cout << "construction of ConcreateProductA2\n";
}
ConcreateProductA2::~ConcreateProductA2()
{
std::cout << "destruction of ConcreateProductA2\n";
}
ConcreateProductB1::ConcreateProductB1()
{
std::cout << "construction of ConcreateProductB1\n";
}
ConcreateProductB1::~ConcreateProductB1()
{
std::cout << "destruction of ConcreateProductB1\n";
}
ConcreateProductB2::ConcreateProductB2()
{
std::cout << "construction of ConcreateProductB2\n";
}
ConcreateProductB2::~ConcreateProductB2()
{
std::cout << "destruction of ConcreateProductB2\n";
}
ConcreateFactory1::ConcreateFactory1()
{
std::cout << "construction of ConcreateFactory1\n";
}
ConcreateFactory1::~ConcreateFactory1()
{
std::cout << "destruction of ConcreateFactory1\n";
}
AbstractProductA* ConcreateFactory1::CreateProductA()
{
return new ConcreateProductA1();
}
AbstractProductB* ConcreateFactory1::CreateProductB()
{
return new ConcreateProductB1();
}
ConcreateFactory2::ConcreateFactory2()
{
std::cout << "construction of ConcreateFactory2\n";
}
ConcreateFactory2::~ConcreateFactory2()
{
std::cout << "destruction of ConcreateFactory2\n";
}
AbstractProductA* ConcreateFactory2::CreateProductA()
{
return new ConcreateProductA2();
}
AbstractProductB* ConcreateFactory2::CreateProductB()
{
return new ConcreateProductB2();
}
#include "stdafx.h"
#include "abstractfactory_impl.h"
#include <stdlib.h>
int _tmain(int argc, _TCHAR* argv[])
{
AbstractFactory *pFactory1 = new ConcreateFactory1;
AbstractProductA *pProductA = pFactory1->CreateProductA();
AbstractFactory *pFactory2 = new ConcreateFactory2;
AbstractProductB *pProductB = pFactory2->CreateProductB();
delete pFactory1;
delete pProductA;
delete pFactory2;
delete pProductB;
system("pause");
return 0;
}