选择部件执行C ++的策略

时间:2012-10-19 12:34:54

标签: c++ boost factory

让我首先展示几个课程:

class globalcontext
{
  public:
  /*partA context*/
  A;
  B;
  c;
  /*partB context*/
  D;
  E;
  F;
  .......
  execute();  //a method to do something (serialize) for context above

};

class mainprocess
{
     callsubprocess();
};
class subprocessA{};
class subprocessB{};
class subprocessC{};
..................

实际上,有几个后端运行主进程,因此上下文将从这里或那里发送,这就是我想执行(序列化/反序列化)的原因。

流程如下:mainprocess :: callsubprocess()---->选择一个子流程,所以选择subprocessA ---->从globalcontext类执行context的partA。

可以在boost中使用工厂吗?

1 个答案:

答案 0 :(得分:2)

也许您正在寻找策略模式?假设A-F编码行为,您可以“混合”不同的行为或将其作为策略提供:

注意:下面,静态/非静态成员函数之间的分离有点随意( mixins 完全可以包含静态成员)。

混入

#include <iostream>

struct NormalPartABehaviour {
    void A() { std::cout << "Normal A" << std::endl; }
    void B() { std::cout << "Normal B" << std::endl; }
    void C() { std::cout << "Normal C" << std::endl; }
};

struct SpecialPartABehaviour {
    void A() { std::cout << "Special A" << std::endl; }
    void B() { std::cout << "Special B" << std::endl; }
    void C() { std::cout << "Special C" << std::endl; }
};

struct NormalPartBBehaviour {
    void D() { std::cout << "Normal D" << std::endl; }
    void E() { std::cout << "Normal E" << std::endl; }
    void F() { std::cout << "Normal F" << std::endl; }
};

template <typename PartAMixin, typename PartBMixin>
struct GlobalContext : public PartAMixin, public PartBMixin
{
};

///// test method:

template <class Context>
void test(Context globalcontext)
{
    globalcontext.A();
    globalcontext.B();
    globalcontext.C();
    globalcontext.D();
    globalcontext.E();
    globalcontext.F();
}

int main()
{
    GlobalContext<NormalPartABehaviour,  NormalPartBBehaviour> ctx1;
    GlobalContext<SpecialPartABehaviour, NormalPartBBehaviour> ctx2;

    std::cout << "testing ctx1: \n";
    test(ctx1);
    std::cout << "testing ctx2: \n";
    test(ctx2);
}

输出http://liveworkspace.org/code/b6b5cfffba11df68bc70c432b030b1d5

testing ctx1:
Normal A
Normal B
Normal C
Normal D
Normal E
Normal F
testing ctx2:
Special A
Special B
Special C
Normal D
Normal E
Normal F

策略

#include <iostream>

struct NormalPartABehaviour {
    static void A() { std::cout << "Normal A" << std::endl; }
    static void B() { std::cout << "Normal B" << std::endl; }
    static void C() { std::cout << "Normal C" << std::endl; }
};

struct SpecialPartABehaviour {
    static void A() { std::cout << "Special A" << std::endl; }
    static void B() { std::cout << "Special B" << std::endl; }
    static void C() { std::cout << "Special C" << std::endl; }
};

struct NormalPartBBehaviour {
    static void D() { std::cout << "Normal D" << std::endl; }
    static void E() { std::cout << "Normal E" << std::endl; }
    static void F() { std::cout << "Normal F" << std::endl; }
};

template <typename PartAMixin, typename PartBMixin>
struct GlobalContext
{
    static void A() { PartAMixin::A(); }
    static void B() { PartAMixin::B(); }
    static void C() { PartAMixin::C(); }

    static void D() { PartBMixin::D(); }
    static void E() { PartBMixin::E(); }
    static void F() { PartBMixin::F(); }
};

///// test method:

template <class Context>
void test()
{
    Context::A();
    Context::B();
    Context::C();
    Context::D();
    Context::E();
    Context::F();
}

int main()
{
    typedef GlobalContext<NormalPartABehaviour,  NormalPartBBehaviour> ctx1;
    typedef GlobalContext<SpecialPartABehaviour, NormalPartBBehaviour> ctx2;

    std::cout << "testing ctx1: \n";
    test<ctx1>();
    std::cout << "testing ctx2: \n";
    test<ctx2>();
}

输出http://liveworkspace.org/code/8bca96d0e9784026c6357a30110bc5fd

testing ctx1: 
Normal A
Normal B
Normal C
Normal D
Normal E
Normal F
testing ctx2: 
Special A
Special B
Special C
Normal D
Normal E
Normal F