• 抽象工厂模式


    1】什么是抽象工厂模式?
    
    为创建一组相关或相互依赖的对象提供一个接口,而且无需指定他们的具体类
    
    【2】抽象工厂模式的代码示例:
    
    代码示例:
    #include <iostream>
    #include <string>
    using namespace std;
    
    class IUser
    {
    public:
        virtual void getUser() = 0;
        virtual void setUser() = 0;
    };
    
    class SqlUser : public IUser
    {
    public:
        void getUser()
        {
            cout << "在sql中返回user" << endl;
        }
        void setUser()
        {
            cout << "在sql中设置user" << endl;
        }
    };
    
    class AccessUser : public IUser
    {
    public:
        void getUser()
        {
            cout << "在Access中返回user" << endl;
        }
        void setUser()
        {
            cout << "在Access中设置user" << endl;
        }
    };
    
    class IDepartment
    {
    public:
        virtual void getDepartment() = 0;
        virtual void setDepartment() = 0;
    };
    
    class SqlDepartment : public IDepartment
    {
    public:
        void getDepartment()
        {
            cout << "在sql中返回Department" << endl;
        }
        void setDepartment()
        {
            cout << "在sql中设置Department" << endl;
        }
    };
    
    class AccessDepartment : public IDepartment
    {
    public:
        void getDepartment()
        {
            cout << "在Access中返回Department" << endl;
        }
        void setDepartment()
        {
            cout << "在Access中设置Department" << endl;
        }
    };
    
    class IFactory
    {
    public:
        virtual IUser *createUser() = 0;
        virtual IDepartment *createDepartment() = 0;
    };
    
    class SqlFactory : public IFactory
    {
    public:
        IUser *createUser() 
        {
            return new SqlUser();
        }
        IDepartment *createDepartment() 
        {
            return new SqlDepartment();
        }
    };
    
    class AccessFactory : public IFactory
    {
    public:
        IUser *createUser()
        {
            return new AccessUser();
        }
        IDepartment *createDepartment() 
        {
            return new AccessDepartment();
        }
    };
    
    /*************************************************************/
    
    class DataAccess
    {
    private:
        static string db;
    public:
        static IUser *createUser()
        {
            if (db == "access")
            {
                return new AccessUser();
            }
            else if (db == "sql")
            {
                return new SqlUser();
            }
        }
        static IDepartment *createDepartment()
        {
            if (db == "access")
            {
                return new AccessDepartment();
            }
            else if (db == "sql")
            {
                return new SqlDepartment();
            }    
        }
    };
    
    string DataAccess::db = "sql";
    
    /*************************************************************/
    
    int main()
    {
        IFactory *factory;
        IUser *user;
        IDepartment *department;
    
        factory = new AccessFactory();
        user = factory->createUser();
        department = factory->createDepartment();
        
        user->getUser();
        user->setUser();
        department->getDepartment();
        department->setDepartment();
    
        user = DataAccess::createUser();
        department = DataAccess::createDepartment();
    
        user->getUser();
        user->setUser();
        department->getDepartment();
        department->setDepartment();
    
        return 0;
    }

    http://www.cnblogs.com/Braveliu/p/3946808.html

  • 相关阅读:
    老司机心得之时间管理"入坑"
    shell的字符串和数字的转化(数字自动做字符串处理,变量名做字符串输出用单引号)
    shell的date命令:使用方法,以及小时、分钟的计算
    一篇详细的linux中shell语言的字符串处理
    linux的string操作(字符串截取,长度计算)
    linux下数学运算器:expr命令(shell中完成数学运算)
    shell脚本格式的几点注意:格式严格,空格不能随便出现(一写就记不住)
    vim的颜色修改,高亮设置。
    pyhton exit
    python判断类型:想知道一个对象(实例或者变量)是什么类型,什么结构的
  • 原文地址:https://www.cnblogs.com/leijiangtao/p/4534597.html
Copyright © 2020-2023  润新知