• 设计模式 之 《抽象工厂模式》


    #ifndef __ABSTRACT_FACTORY_MODEL__
    #define __ABSTRACT_FACTORY_MODEL__
    
    #include <string>
    #include <vector>
    #include <iostream>
    using namespace std;
    /*
    GOOD:定义了一个创建一系列相关或相互依赖的接口,而无需指定它们的具体类。
    用于交换产品系列,如ACCESS->SQL SERVER;
    产品的具体类名被具体工厂的实现分离
    */
    
    //用户抽象接口
    class IUser
    {
    public:
        virtual void getUser() = 0;
        virtual void insertUser() = 0;
    };
    
    //部门抽象接口
    class IDepartment
    {
    public:
        virtual void getDepartment() = 0;
        virtual void insertDepartment() = 0;
    };
    
    //ACCESS用户
    class CAccessUser : public IUser
    {
    public:
        virtual void getUser(){ cout<<"Access GetUser"<<endl; }
        virtual void insertUser(){ cout<<"Access InsertUser"<<endl; }
    };
    
    //ACCESS部门
    class CAccessDepartment : public IDepartment
    {
    public:
        virtual void getDepartment(){ cout<<"Access GetDepartment"<<endl; }
        virtual void insertDepartment(){ cout<<"Access InsertDepartment"<<endl; }
    };
    
    //SQL用户
    class CSqlUser : public IUser
    {
    public:
        virtual void getUser(){ cout<<"Sql User"<<endl; }
        virtual void insertUser(){ cout<<"Sql User"<<endl; }
    };
    
    //SQL部门类
    class CSqlDepartment : public IDepartment
    {
    public:
        virtual void getDepartment(){ cout<<"Sql GetDepartment"<<endl; }
        virtual void insertDepartment(){ cout<<"Sql InsertDepartment"<<endl; }
    };
    
    //抽象工厂
    class IFactory
    {
    public:
        virtual IUser* createUser() = 0;
        virtual IDepartment* createDepartment() = 0;
    };
    
    //ACCESS 工厂
    class AccessFactory : public IFactory
    {
    public:
        virtual IUser* createUser(){ return new CAccessUser(); }
        virtual IDepartment* createDepartment(){ return new CAccessDepartment(); }
    };
    
    //SQL 工厂
    class SqlFactory : public IFactory
    {
    public:
        virtual IUser* createUser(){ return new CSqlUser(); }
        virtual IDepartment* createDepartment(){ return new CSqlDepartment(); }
    };
    
    #endif //__ABSTRACT_FACTORY_MODEL__
    
    /*
    #include "AbstractFactoryModel.h"
    int _tmain(int argc, _TCHAR* argv[])
    {
        IFactory* factory = new SqlFactory();
        IUser* user = factory->createUser();
        IDepartment* depart = factory->createDepartment();
        user->getUser();
        depart->getDepartment();
    
        return 0;
    }
    */
  • 相关阅读:
    es6 学习笔记3
    flex 子元素和父元素的高度相同问题
    vue 笔记2
    vue 子父组件之间的通信
    vue+webpack 错误笔记
    vue 学习新笔记
    mongoDB 使用学习笔记
    express 学习笔记
    多态
    static修饰符与final修饰符
  • 原文地址:https://www.cnblogs.com/MrGreen/p/3416944.html
Copyright © 2020-2023  润新知