• [Design Pattern] 抽象工厂模式


    抽象工厂模式

    抽象工厂针对产品族,不针对产品等级结构

    产品族:同一产地同一个厂商,功能不同。

    产品等级结构:功能相同,但是产地和厂商不同。

    class AbstractApple
    {
    public:
        virtual void showName() = 0;
    };
    class ChApple : public AbstractApple
    {
    public:
        virtual void showName()
        {
            cout << "Ch Apple." << endl;
        }
    };
    class USAApple : public AbstractApple
    {
    public:
        virtual void showName()
        {
            cout << "USA Apple." << endl;
        }
    };
    class JpApple : public AbstractApple
    {
    public:
        virtual void showName()
        {
            cout << "Jp Apple." << endl;
        }
    };
    
    class AbstractBanana
    {
    public:
        virtual void showName() = 0;
    };
    class ChBanana : public AbstractBanana
    {
    public:
        virtual void showName()
        {
            cout << "Ch Banana." << endl;
        }
    };
    class USABanana : public AbstractBanana
    {
    public:
        virtual void showName()
        {
            cout << "USA Banana." << endl;
        }
    };
    class JpBanana : public AbstractBanana
    {
    public:
        virtual void showName()
        {
            cout << "Jp Banana." << endl;
        }
    }; 
    class AbstractPear
    {
    public:
        virtual void showName() = 0;
    };
    class ChPear : public AbstractPear
    {
    public:
        virtual void showName()
        {
            cout << "Ch Pear." << endl;
        }
    };
    class USAPear : public AbstractPear
    {
    public:
        virtual void showName()
        {
            cout << "USA Banana." << endl;
        }
    };
    
    class JpPear : public AbstractPear
    {
    public:
        virtual void showName()
        {
            cout << "Jp Pear." << endl;
        }
    };
    
    class AbstractFactory
    {
    public:
        virtual AbstractApple* createApple() = 0;
        virtual AbstractBanana* createBanana() = 0;
        virtual AbstractPear* createPear() = 0;
    };
    
    class ChFactory : AbstractFactory
    {
    public:
        virtual AbstractApple* createApple()
        {
            return new ChApple();
        }
        virtual AbstractBanana* createBanana()
        {
            return new ChBanana();
        }
        virtual AbstractPear* createPear()
        {
            return new ChPear();
        }
    };
    class USAFactory : AbstractFactory
    {
    public:
        virtual AbstractApple* createApple()
        {
            return new USAApple();
        }
        virtual AbstractBanana* createBanana()
        {
            return new USABanana();
        }
        virtual AbstractPear* createPear()
        {
            return new USAPear();
        }
    };
    class JpFactory : AbstractFactory
    {
    public:
        virtual AbstractApple* createApple()
        {
            return new JpApple();
        }
        virtual AbstractBanana* createBanana()
        {
            return new JpBanana();
        }
        virtual AbstractPear* createPear()
        {
            return new JpPear();
        }
    };
  • 相关阅读:
    Linux下vi的用法
    C++程序设计语言编程风格演变史(转载)
    软件项目经理必备素质(转)
    DES加密算法的实现
    世界编程大赛一等奖作品
    AStar算法的原理及应用
    一个DirectX使用的例子
    ConsolePlayer
    自制贪吃蛇游戏
    关于安装 DirectX SDk Dec 2005 后无法编译DirectShow应用程序的问题
  • 原文地址:https://www.cnblogs.com/immjc/p/9120830.html
Copyright © 2020-2023  润新知