Welcome to tiger's blog!

What lead to success, what we are seeking...
posts - 47, comments - 23, trackbacks - 0, articles - 8
   :: 首页 :: 新随笔 :: 联系 :: 聚合  :: 管理

设计模式(Abstract Factory)

Posted on 2007-04-13 09:52 tiger 阅读(407) 评论(0)  编辑 收藏 引用

// 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;
 }
}


只有注册用户登录后才能发表评论。
网站导航: 博客园   IT新闻   BlogJava   知识库   博问   管理