工厂模式

 

#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 @ 2023-05-09 09:18  泽良_小涛  阅读(3)  评论(0编辑  收藏  举报