C++抽象工厂

#include 
#include 
using namespace std;

// 抽象工厂模式
// 衣服的基类
class Clothes
{
public:
    virtual void show() = 0;
};

class AD_T_shirt:public Clothes
{
public:
    void show()
    {
        cout << "阿迪 T_shirt" << endl;
    }
};
class AD_Jacket:public Clothes
{
public:
    void show()
    {
        cout << "阿迪 Jacket" << endl;
    }
};

class LN_T_shirt:public Clothes
{
public:
    void show()
    {
        cout << "李宁 T_shirt" << endl;
    }
};
class LN_Jacket:public Clothes
{
public:
    void show()
    {
        cout << "李宁 Jacket" << endl;
    }
};

// 抽象工厂
class AbsFactory
{
public:
    virtual Clothes *Create_T_shirt() = 0;
    virtual Clothes *Create_Jacket() = 0;
};

class Factory_AD :public AbsFactory
{
public:
    virtual Clothes *Create_T_shirt()
    {
        return new AD_T_shirt;
    }
    virtual Clothes *Create_Jacket()
    {
        return new AD_Jacket;
    }
};

class Factory_LN :public AbsFactory
{
public:
    virtual Clothes *Create_T_shirt()
    {
        return new LN_T_shirt;
    }
    virtual Clothes *Create_Jacket()
    {
        return new LN_Jacket;
    }
};

int main()
{
    AbsFactory *pf1 = new Factory_AD;
    Clothes *pc1 = pf1->Create_Jacket();


    AbsFactory *pf2 = new Factory_LN;
    Clothes *pc2 = pf2->Create_T_shirt();

    Clothes *pc3 = pf1->Create_T_shirt();

    return 0;
}


你可能感兴趣的:(学习C)