zoukankan      html  css  js  c++  java
  • C++工厂模式

    1. 简单工厂模式

    #include <iostream>
    using namespace std;
    
    class Shoes
    {
    public:
        virtual ~Shoes() {}
        virtual void Show() = 0;
    };
    
    class LiNingShoes : public Shoes
    {
    public:
        void Show() { cout << "LiNing" << endl; }
    };
    
    class AdidasShoes : public Shoes
    {
    public:
        void Show() { cout << "Adidas" << endl; }
    };
    
    enum SHOES_TYPE
    {
        LINING,
        ADIDAS
    };
    
    class ShoesFactory
    {
    public:
        Shoes* CreateShoes(SHOES_TYPE type)
        {
            switch (type)
            {
            case LINING:
                return new LiNingShoes();
                break;
            case ADIDAS:
                return new AdidasShoes();
                break;
            default:
                return NULL;
                break;
            }
        }
    };
    
    int main()
    {
        ShoesFactory shoesFactory;
        
        Shoes* pLiNingShoes = shoesFactory.CreateShoes(LINING);
        if (pLiNingShoes != NULL) { pLiNingShoes->Show(); delete pLiNingShoes; }
        
        Shoes* pAdidasShoes = shoesFactory.CreateShoes(ADIDAS);
        if (pAdidasShoes != NULL) { pAdidasShoes->Show(); delete pAdidasShoes; }
    }
    

    2. 工厂方法模式

    #include <iostream>
    using namespace std;
    
    // 抽象基类,只提供一个接口规范
    class Shoes 
    {
    public:
        virtual ~Shoes() {}
        virtual void Show() = 0; // 接口规范
    };
    
    class LiNingShoes : public Shoes
    {
    public:
        void Show() { cout << "LiNing" << endl; }
    };
    
    class AdidasShoes : public Shoes
    {
    public:
        void Show() { cout << "Adidas" << endl; }
    };
    
    // 抽象基类,只提供一个工厂类的接口规范
    class ShoesFactory
    {
    public:
        virtual Shoes* CreateShoes() = 0;
        virtual ~ShoesFactory() {}
    };
    
    class LiNingProducer : public ShoesFactory
    {
    public:
        Shoes *CreateShoes() { return new LiNingShoes(); }
    };
    
    class AdidasProducer : public ShoesFactory
    {
    public:
        Shoes *CreateShoes() { return new AdidasShoes(); }
    };
    
    int main()
    {
        ShoesFactory *pLiNingProducer = new LiNingProducer();
        Shoes* pLiNingShoes = pLiNingProducer->CreateShoes();
        pLiNingShoes->Show();
        delete pLiNingShoes;
        delete pLiNingProducer;
        
        ShoesFactory *pAdidasProducer = new AdidasProducer();
        Shoes* pAdidasShoes = pAdidasProducer->CreateShoes();
        pAdidasShoes->Show();
        delete pAdidasShoes;
        delete pAdidasProducer;
    }
    

    3. 模板工厂

    #include <iostream>
    using namespace std;
    
    class Shoes
    {
    public:
        virtual ~Shoes() {}
        virtual void Show() = 0;
    };
    
    class LiNingShoes : public Shoes
    {
    public:
        void Show() { cout << "LiNing" << endl; }
    };
    
    class AdidasShoes : public Shoes
    {
    public:
        void Show() { cout << "Adidas" << endl; }
    };
    
    template <class ConcreteProduct_t>
    class ConcreteFactory
    {
    public:
        ConcreteProduct_t* CreateShoes()
        {
            return new ConcreteProduct_t();
        }
    };
    
    int main()
    {
        ConcreteFactory<LiNingShoes> LiNingFactory;
        Shoes* pLiNingShoes = LiNingFactory.CreateShoes();
        pLiNingShoes->Show();
        delete pLiNingShoes;
    
        ConcreteFactory<AdidasShoes> AdidasFactory;
        Shoes* pAdidasShoes = AdidasFactory.CreateShoes();
        pAdidasShoes->Show();
        delete pAdidasShoes;
    }
    

    4. 自动注册工厂

    #include <iostream>
    #include <map>
    #include <string>
    #include <functional>
    using namespace std;
    
    
    class Shoes
    {
    public:
        virtual ~Shoes() {}
        virtual void Show() = 0;
    };
    
    
    class LiNingShoes : public Shoes
    {
    public:
        void Show() { cout << "LiNing" << endl; }
    };
    
    
    class AdidasShoes : public Shoes
    {
    public:
        int data;
        AdidasShoes(int data) :data(data) {}
        void Show() { cout << "Adidas " << data << endl; }
    };
    
    
    struct factory
    {
        template<typename T>
        struct register_t
        {
            register_t(string key)
            {
                factory::get().map_.emplace(key, [] { return new T(); });
            }
    
            template<typename... Args>
            register_t(string key, Args... args)
            {
                factory::get().map_.emplace(key, [args...] { return new T(args...); });
            }
        };
    
        static Shoes* produce(string key)
        {
            if (map_.find(key) == map_.end())
                throw std::invalid_argument("the key is not exist!");
            return map_[key]();
        }
    
    private:
        factory() {};
        factory(const factory&) = delete;
        factory(factory&&) = delete;
        static factory& get() // 单例工厂
        {
            static factory instance;
            return instance;
        }
        static map<string, function<Shoes* ()>> map_;
    };
    map<string, function<Shoes* ()>> factory::map_; // 不要忘记初始化私有static
    
    
    // 以下##__VA_ARGS__表示可省略可变参数宏
    #define REGISTER(T, key, ...) static factory::register_t<T> reg_msg_##T##_(key, ##__VA_ARGS__)
    
    
    // 以下为注册
    REGISTER(LiNingShoes, "LiNingShoes");
    REGISTER(AdidasShoes, "AdidasShoes", 3);
    
    
    int main()
    {
        Shoes* pLiNingShoes = factory::produce("LiNingShoes");
        pLiNingShoes->Show();
    
        Shoes* AdidasShoes = factory::produce("AdidasShoes");
        AdidasShoes->Show();
    }
    
  • 相关阅读:
    [转]char、varchar、nchar、nvarchar的区别
    【转】Asp.net 2.0中页的生存周期(Lifecycle)和动态控件 [.Net]
    git免登录sshkey
    ios8,xcode6 周边
    iOS 推送证书
    Lazarus中TreeView导出XML以及XML导入TreeView
    flac文件提取专辑封面手记
    Lazarus解决含中文文件名或路径的使用问题
    使用PowerShell管理Windows8应用
    thbgm拆包【in progress】
  • 原文地址:https://www.cnblogs.com/xytpai/p/13829624.html
Copyright © 2011-2022 走看看