设计模式- 之我见---创造性(虚拟工厂)

 

/*

leftright by Tony

 

*/

//AbstractFactory.h

#ifndef AF_HH
#define AF_HH
#include "stdafx.h"
//enum Good{Noodle,Banana,CD,Hotdog,Food};
/////////////////////////////////////////////////

             /*<-虚拟工厂->*/

////////////////////////////////////////////////
class Noodle{};
class Banana{};
class CD{};
class Hotdog{};
class Food{};
class AFactory
{
public:
 virtual Noodle *MakeNoodle();
 virtual Banana *MakeBanana();
 virtual CD * MakeCD();
 virtual Hotdog *MakeHotdog();
 virtual Food * MakeFood();

};
class Shop
{
public :
 Shop();
 virtual ~Shop();
 virtual void Create(AFactory &);
private:
 Noodle *N;
 Banana *B;
 CD * C;
 Hotdog *H;
 Food * F;
};
#endif

 

 

////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

 

 

 

// AbstractFactory.cpp : 定义控制台应用程序的入口点。
//抽象工厂

#include "stdafx.h"
#include "AbstractFactory.h"
Noodle *AFactory::MakeNoodle()
{
 return new Noodle();
}
Banana *AFactory::MakeBanana()
{
 return new Banana();
}
CD * AFactory::MakeCD()
{
 return new CD();
}
Hotdog *AFactory::MakeHotdog()
{
 return new Hotdog();
}
Food * AFactory::MakeFood()
{
 return new Food();
}
void Shop::Create(AFactory &a)
{
 this->B = a.MakeBanana();
 this->C = a.MakeCD();
 this->F = a.MakeFood();
 this->H = a.MakeHotdog();
 this->N = a.MakeNoodle();
}
Shop::~Shop()
{
 if (B!=NULL)
  delete B;
 
 if (C!=NULL)
  delete C;
 
 if (F!=NULL)
  delete F;
 
 if (H!=NULL)
  delete H;
 
 if (N!=NULL)
  delete N;

}
Shop::Shop()
{}
int _tmain(int argc, _TCHAR* argv[])
{
 AFactory f;
 Shop s;
 s.Create(f);

 return 0;
}

 

 

/*

leftright by Tony

 

*/

//////////////////有问题地方 ,还请指教...

 

你可能感兴趣的:(设计模式,delete,class,null,c,畅叙言)