#define win 0
#define mac 1
#include <iostream>
using namespace std;

class button
{
public:
    button(){}
    virtual void createbutton()=0;
};

class macbutton : public button
{
public:
    macbutton(){}
    virtual void createbutton(){cout<<"macbutton"<<endl;}
};

class winbutton : public button
{
public:
    winbutton(){}
    virtual void createbutton(){cout<<"winbutton"<<endl;}
};

class simplefactory
{
public:
    simplefactory(){}
    button *createproduct(int ID);
    button *createmacproduct();
    button *createwinproduct();
};

button *simplefactory::createproduct(int ID)
{
    if (ID==win)
        return new winbutton;
    if (ID==mac)
        return new macbutton;
}

button *simplefactory::createwinproduct()
{
    return new winbutton;
}

button *simplefactory::createmacproduct()
{
    return new macbutton;
}

int main()
{
    simplefactory fc;
    button *bt=fc.createproduct(win);

    bt->createbutton();

    delete bt;
    system("pause");
    return 0;
}

相关文章:

  • 2022-02-12
  • 2022-12-23
  • 2021-12-09
  • 2021-11-17
  • 2021-04-11
  • 2021-09-27
  • 2021-12-08
猜你喜欢
  • 2022-12-23
  • 2021-06-24
  • 2021-12-24
  • 2022-12-23
  • 2021-09-13
  • 2021-11-01
  • 2021-07-13
相关资源
相似解决方案