工厂模式

 

复制代码
#include <iostream>
#include <string>
using namespace std;
class ExportFileApi {
public:
    virtual bool exportData(string data) = 0;
protected:
    ExportFileApi(){}
};

//具体化的子类
class ExportTxtFile : public ExportFileApi {
public:
    bool exportData(string data) {
        cout << "正在导出数据" << data << "到csv文件" << endl;
        return true;
    }
};

//生成数据到数据库
class ExportDB :public ExportFileApi {
public:
    bool exportData(string data) {
        cout << "正在导出数据" << data << "到数据库" << endl;
        return true;
    }
};

//实现一个ExportOperate,这个叫导出数据的业务功能对象
class ExportOperate {//他也是接口
public:
    bool exportData(string data) {
        ExportFileApi* pApi = factoryMethod();
        return pApi->exportData(data);
    }
protected:
    virtual ExportFileApi* factoryMethod() = 0;
};

//具体的实现对象,完成导出工作
class ExportTxtFileOperate : public ExportOperate {
protected:
    ExportFileApi* factoryMethod() {
        return new ExportTxtFile();
    }
};

class ExportDBOperate :public ExportOperate {
protected:
    ExportFileApi* factoryMethod() {
        return new ExportDB;
    }
};

int main() {
    ExportOperate* pOperate = new ExportTxtFileOperate();
    pOperate->exportData("Haha");
    system("pause");
    return 0;
}
简单的实现方法
复制代码

 

复制代码
#include <string>
#include <map>
#include <memory>
#include <functional>
#include <iostream>
using namespace std;

template <class T>
class IocContainer {
public:
    IocContainer(void){}
    ~IocContainer()
    {

    }
    //注册需要创建对象的构造函数,通过一个唯一的标识,以便于以后查找
    template<class Drived>
    void RegisterType(string strKey) {
        std::function<T* ()> function = [] {return new Drived();};
        RegisterType(strKey, function);
    }

    //根据唯一的标识去查找对应的构造函数
    T* Resolve(string strKey) {
        if (m_createMap.find(strKey) == m_createMap.end())
            return nullptr;
        std::function<T* ()> function = m_createMap[strKey];
        return function();
    }

    //创建智能指针
    std::shared_ptr<T> ResolveShared(string strKey) {
        T* ptr = Resolve(strKey);
        return std::shared_ptr<T>(ptr);
    }
private:
    void RegisterType(string strKey, std::function<T* ()> creator) {
        if (m_createMap.find(strKey) != m_createMap.end()) {
            throw std::invalid_argument("已经存在这个key了");
        }
        m_createMap.emplace(strKey, creator);
    }

private:
    map<string, std::function<T* ()>> m_createMap;
};

struct ICar {
    virtual ~ICar(){}
    virtual void test() const = 0;
};

struct Bus: ICar
{
    Bus(){}
    void test()const { std::cout << "Bus::test()"<<endl; }
};

struct Track :ICar {
    Track() {};
    void test() const { std::cout << "Track::test" << endl; }

};

int main() {
    IocContainer<ICar> carIOC;
    carIOC.RegisterType<Bus>("bus");
    carIOC.RegisterType<Track>("track");

    std::shared_ptr<ICar> bus = carIOC.ResolveShared("bus");
    bus->test();
   std::shared_ptr<ICar> track = carIOC.ResolveShared("track");
    track->test();
    system("pause");
    return 0;
}
Ioc容器方法
复制代码

 

posted @   泽良_小涛  阅读(5)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 【自荐】一款简洁、开源的在线白板工具 Drawnix
· 园子的第一款AI主题卫衣上架——"HELLO! HOW CAN I ASSIST YOU TODAY
· Docker 太简单,K8s 太复杂?w7panel 让容器管理更轻松!
点击右上角即可分享
微信分享提示