// Factory.h: interface for the CFactory class.
//
//////////////////////////////////////////////////////////////////////
#if !defined(AFX_FACTORY_H__EA4BC919_0C3C_445D_95D0_0B3BC1A79A46__INCLUDED_)
#define AFX_FACTORY_H__EA4BC919_0C3C_445D_95D0_0B3BC1A79A46__INCLUDED_
#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000
class CProduct;
class CFactory
{
public:
CFactory();
virtual ~CFactory();
public:
virtual CProduct *CreateA(void) = 0;
virtual CProduct *CreateB(void) = 0;
};
class CFactory1 : public CFactory
{
public:
CFactory1();
virtual ~CFactory1();
public:
CProduct *CreateA(void);
CProduct *CreateB(void);
};
class CFactory2 : public CFactory
{
public:
CFactory2();
virtual ~CFactory2();
public:
CProduct *CreateA(void);
CProduct *CreateB(void);
};
#endif // !defined(AFX_FACTORY_H__EA4BC919_0C3C_445D_95D0_0B3BC1A79A46__INCLUDED_)
// Factory.cpp: implementation of the CFactory class.
//
//////////////////////////////////////////////////////////////////////
#include "Factory.h"
#include "Product.h"
//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////
CFactory::CFactory()
{
}
CFactory::~CFactory()
{
}
/////////////////////////////////////////////
CFactory1::CFactory1()
{
}
CFactory1::~CFactory1()
{
}
CProduct *CFactory1::CreateA()
{
return new CProductA1();
}
CProduct *CFactory1::CreateB()
{
return new CProductB1();
}
/////////////////////////////////////////////
/////////////////////////////////////////////
CFactory2::CFactory2()
{
}
CFactory2::~CFactory2()
{
}
CProduct *CFactory2::CreateA()
{
return new CProductA2();
}
CProduct *CFactory2::CreateB()
{
return new CProductB2();
}
/////////////////////////////////////////////
// Product.h: interface for the CProduct class.
//
//////////////////////////////////////////////////////////////////////
#if !defined(AFX_PRODUCT_H__2A9147E1_2856_4F8A_9AD3_3BC7D3FD97E3__INCLUDED_)
#define AFX_PRODUCT_H__2A9147E1_2856_4F8A_9AD3_3BC7D3FD97E3__INCLUDED_
#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000
class CProduct
{
public:
CProduct();
virtual ~CProduct();
public:
virtual void Show(void) = 0;
};
class CProductA1 : public CProduct
{
public:
CProductA1();
virtual ~CProductA1();
public:
void Show(void);
};
class CProductB1 : public CProduct
{
public:
CProductB1();
virtual ~CProductB1();
public:
void Show(void);
};
class CProductA2 : public CProduct
{
public:
CProductA2();
virtual ~CProductA2();
public:
void Show(void);
};
class CProductB2 : public CProduct
{
public:
CProductB2();
virtual ~CProductB2();
public:
void Show(void);
};
#endif // !defined(AFX_PRODUCT_H__2A9147E1_2856_4F8A_9AD3_3BC7D3FD97E3__INCLUDED_)
// Product.cpp: implementation of the CProduct class.
//
//////////////////////////////////////////////////////////////////////
#include "Product.h"
#include <iostream>
using namespace std;
//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////
CProduct::CProduct()
{
}
CProduct::~CProduct()
{
}
/////////////////////////////////////////////////////////
CProductA1::CProductA1()
{
}
CProductA1::~CProductA1()
{
}
void CProductA1::Show()
{
cout<<"CProductA1::Show"<<endl;
}
/////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////
CProductB1::CProductB1()
{
}
CProductB1::~CProductB1()
{
}
void CProductB1::Show()
{
cout<<"CProductB1::Show"<<endl;
}
/////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////
CProductA2::CProductA2()
{
}
CProductA2::~CProductA2()
{
}
void CProductA2::Show()
{
cout<<"CProductA2::Show"<<endl;
}
/////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////
CProductB2::CProductB2()
{
}
CProductB2::~CProductB2()
{
}
void CProductB2::Show()
{
cout<<"CProductB2::Show"<<endl;
}
/////////////////////////////////////////////////////////
//main
#include "Factory.h"
#include "Product.h"
#define NULL 0
void main()
{
CFactory *pFactory = NULL;
CProduct *pProductA = NULL;
CProduct *pProductB = NULL;
for(int i = 0; i < 2; i++)
{
if(i == 0)
{
pFactory = new CFactory1();
}
else
{
pFactory = new CFactory2();
}
pProductA = pFactory->CreateA();
pProductB = pFactory->CreateB();
pProductA->Show();
pProductB->Show();
delete pFactory;
delete pProductA;
delete pProductB;
}
}