c++ pipeline设计模式的实现

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

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

在这里插入图片描述

#include <string.h>

#include <algorithm>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>
#include <thread>
#include <atomic>
#include <typeinfo>
#include <pthread.h>

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 <algorithm>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>
#include <thread>
#include <atomic>

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();
}

输出
在这里插入图片描述

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值