c++ pipeline设计模式的实现

pipeline 设计模式的实现, 有几个小问题暂时还懒得解决
第二版已修复

  1. getDownStream 每次都重新生成新的对象,可以优化下
  2. pipeline 模式的核心是 downstream 可以方便提供, 目前写死了。

c++ pipeline设计模式的实现_第1张图片

#include 

#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 

using namespace std;

class LifeCycle {
public:
    virtual void init(std::string config) = 0;

    virtual void startUp() = 0;

    virtual void shutDown() = 0;
};

template<typename T>
class Component : public LifeCycle {
public:
    virtual std::string getName() = 0;

    virtual void execute(T t) = 0;
};

template<typename T, typename R>
class AbstractComponent : public Component<T> {
protected:
    virtual std::vector<shared_ptr<Component<R>>>  getDownStream() = 0;

protected:
    virtual R doExecute(T t) = 0;

public:
    void init(std::string config) override {

    }

    void execute(T t) override {
        R r = doExecute(t);
        cout << this->getName() + "\treceive\t" << typeid(t).name() << "\t" << t << "\treturn\t" << typeid(r).name()
             << "\t" << r << endl;
        if constexpr (is_same_v<R, void>) {
            return;
        }
        for (auto &&obj : getDownStream()) {
            obj->execute(r);
        }
    }

    void startUp() override {
        for (auto &&obj : this->getDownStream()) {
            obj->startUp();
        }
        cout << "------------------ " + this->getName() + " is starting ----------------------" << endl;
    }

    void shutDown() override {
        auto downStreams = this->getDownStream();
        for (auto &&obj : downStreams) {
            obj->shutDown();
        }
        cout << "------------------ " + this->getName() + " is starting ----------------------" << endl;
    }


};

template<typename T, typename R>
using Source = AbstractComponent<T, R>;

template<typename T, typename R>
using Channel = AbstractComponent<T, R>;

template<typename T, typename R>
using Sink = AbstractComponent<T, R>;

class printSink;

class intStringChannel;

class printSink : public Sink<string, int> {
public:
    string getName() override {
        return "printSink";
    }

protected:
    vector<shared_ptr<Component<int>>> getDownStream() override {
        return {};
    }


protected:
    int doExecute(string t) override {
        return INT_MIN;
    }
};

class intStringChannel : public Channel<int, string> {
public:
    void init(std::string config) override {

    }

    string getName() override {
        return "intStringChannel";
    }

    vector<shared_ptr<Component<string>>> getDownStream() override {
        return vector<shared_ptr<Component<string>>>{make_shared<printSink>()};
    }

    void startUp() override {

    }

    void shutDown() override {

    }

protected:
    string doExecute(int t) override {
        return to_string(t + 100);
    }
};

class IntSource : public Source<int, int> {
private:
    int val = 0;
public:
    void init(std::string config) override {
        cout << "--------- " + getName() + " init --------- ";
        val = 1;
    }

    string getName() override {
        return "Int Source";
    }

    vector<shared_ptr<Component<int>>> getDownStream() override {
        return {make_shared<intStringChannel>()};
    }

    void startUp() override {
        this->execute(val);
    }

protected:
    int doExecute(int) override {
        return val + 1;
    }
};

class pipeline : public LifeCycle {
private:
    shared_ptr<Source<int, int>> source;

public:
    pipeline() {
        source = make_shared<IntSource>();
    }

    void init(std::string config) override {
    }

    void startUp() override {
        source->startUp();
    }

    void shutDown() override {
        source->shutDown();
    }
};

int main() {
    pipeline p;
    p.startUp();
}

第二版

增加下游可配置功能

#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 

using namespace std;

class LifeCycle {
 public:
    virtual void init(std::string config) = 0;

    virtual void startUp() = 0;

    virtual void shutDown() = 0;
};

template<typename T>
class Component : public LifeCycle {
 public:
    virtual std::string getName() = 0;

    virtual void execute(T t) = 0;
};

template<typename T, typename R>
class AbstractComponent : public Component<T> {
 private:
    std::unordered_set<shared_ptr<Component<R>>> down_stream;
 protected:
    const std::unordered_set<shared_ptr<Component<R>>> &getDownStream() {
        return down_stream;
    }

 protected:
    virtual R doExecute(T t) = 0;

 public:
    void addDownStream(shared_ptr<Component<R>> component) {
        down_stream.insert(component);
    }

    void init(std::string config) override {

    }

    void execute(T t) override {
        R r = doExecute(t);
        cout << this->getName() + "\treceive\t" << typeid(t).name() << "\t" << t << "\treturn\t" << typeid(r).name()
             << "\t" << r << endl;
        if constexpr (is_same_v<R, void>) {
            return;
        }
        for (auto &&obj : getDownStream()) {
            obj->execute(r);
        }
    }

    void startUp() override {
        for (auto &&obj : this->getDownStream()) {
            obj->startUp();
        }
        cout << "------------------ " + this->getName() + " is starting ----------------------" << endl;
    }

    void shutDown() override {
        auto downStreams = this->getDownStream();
        for (auto &&obj : downStreams) {
            obj->shutDown();
        }
        cout << "------------------ " + this->getName() + " is starting ----------------------" << endl;
    }


};

template<typename T, typename R>
using Source = AbstractComponent<T, R>;

template<typename T, typename R>
using Channel = AbstractComponent<T, R>;

template<typename T, typename R>
using Sink = AbstractComponent<T, R>;

class printSink;

class intStringChannel;

class printSink : public Sink<string, int> {
 public:
    string getName() override {
        return "printSink";
    }

 protected:
    int doExecute(string t) override {
        return INT_MIN;
    }
};

class intStringChannel : public Channel<int, string> {
 public:
    void init(std::string config) override {

    }

    string getName() override {
        return "intStringChannel";
    }

    void startUp() override {

    }

    void shutDown() override {

    }

 protected:
    string doExecute(int t) override {
        return to_string(t + 100);
    }
};

class IntSource : public Source<int, int> {
 private:
    int val = 0;
 public:
    void init(std::string config) override {
        cout << "--------- " + getName() + " init --------- ";
        val = 1;
    }

    string getName() override {
        return "Int Source";
    }

    void startUp() override {
        this->execute(val);
    }

 protected:
    int doExecute(int) override {
        return val + 1;
    }
};

template<typename R, typename T>
class pipeline : public LifeCycle {
 private:
    shared_ptr<Source<R, T>> source;

 public:
    void setSource(shared_ptr<Source<R, T>> component) {
        source = component;
    }

    void init(std::string config) override {
    }

    void startUp() override {
        assert(source.get() != nullptr);
        source->startUp();
    }

    void shutDown() override {
        source->shutDown();
    }
};

int main() {
    pipeline<int, int> p;
    // source
    auto is = make_shared<IntSource>();

    // channel
    auto isc = make_shared<intStringChannel>();

    // sink
    auto ps = make_shared<printSink>();

    is->addDownStream(isc);
    isc->addDownStream(ps);

    // 设置 source
    p.setSource(is);

    // 启动
    p.startUp();
}

输出
c++ pipeline设计模式的实现_第2张图片

你可能感兴趣的:(c++,个人,设计模式,c++)