C++设计模式之工厂模式

#define APPLE  1
#define ORANGE 2
class XFruit
{
   public :
     virtual void ShowName() = 0;
} ;
//派生出子类商品
class Apple : public XFruit
{
   public :
      void ShowName (){ShowMessage("My name is apple"); }

};
class Orange : public XFruit
{
  public :
      void ShowName(){ShowMessage("My name is orange");}
} ;
//工厂类基类
class FruitShop
{
    public:

    XFruit* SaleFruit(int type)
   {
     if(type == APPLE)
     {
       return new Apple;
     }
     else if(type == ORANGE)
     {
       return new Orange;
     }
     else
     {
       return NULL;
     }

   }
} ;

void __fastcall TForm1::RzButton1Click(TObject *Sender)
{
   FruitShop * fruitshop = new FruitShop();
   fruitshop->SaleFruit(APPLE)->ShowName();
   delete  fruitshop;

}
//---------------------------------------------------------------------------
void __fastcall TForm1::RzButton2Click(TObject *Sender)
{
   FruitShop * fruitshop = new FruitShop();
   fruitshop->SaleFruit(ORANGE)->ShowName();
   delete  fruitshop;
}

 

posted @ 2016-02-25 15:54  風行  阅读(197)  评论(0编辑  收藏  举报