C ++:多个策略相互调用 [英] C++: Multiple Policies calling each other

查看:181
本文介绍了C ++:多个策略相互调用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对于基于策略的类设计,我需要一些策略来调用在其他策略中找到的函数:

  struct PolicyA {
void foo(){
// ...
}
};

struct PolicyB {
void bar(){
//这里需要调用foo()
}
};

template< class A,class B> MyClass:A,B {};

我看到的两个选项是:




  • 通过 MyClass< A,B> PolicyA PolicyB 作为模板参数,并执行类似 dynamic_cast< MyClass< A,B> *>(this) - > foo(); 。然而,这可能很容易变得非常复杂(特别是如果它不仅 B 调用 A ,但也是另一种方式)。

  • 为每个策略都有一个基类,其中所有函数都声明为纯虚函数。



这应该是一个常见的问题,但我没有找到什么。我想有可能是一些助推魔法或东西。您将如何解决这个问题?



编辑:另请参阅followup 这里

解决方案

为什么 PolicyB encapsulationulate PolicyA 并根据需要调用它? PolicyB 显然意识到 PolicyA - 不需要基类或继承。



似乎所有的 Policy 实现都有一些常见的 apply 方法,外部调用者可以同意



右键,所以如果它不只是一个简单的一个特定的策略实现需要访问另一个策略的情况下,您可能应该专注于为策略创建一个接口 - 甚至可能是规则类可以组合在一起形成一个特殊的策略 - 参见规范模式。

  class Rule {
allow(Foo foo);
}

class Policy {
规则规则= new NotNullRule()和(someOtherRule);

void applyTo(Foo foo){
if(rule.allows(foo)){
return foo;
}
foo.disable();
}
}

调用任何你想要的,条件规则规范以不依赖于它们的方式。然后,当有人在查看PolicyA时,他们可以清楚地看到管理其行为的规则/条件,并进行客观化。



一旦实施了策略,要对某些事实执行多个策略,可以再次使用复合模式,以便外部类只能看到一个 Policy 接口。但同样,你有一个明确的命名策略被引用,而不仅仅是交叉调用函数。


For a policy-based class-design I need some of the policies to call functions that are found in other policies:

struct PolicyA {
  void foo() {
    // ...
  }
};

struct PolicyB {
  void bar() {
    // Need to call foo() here
  }
};

template <class A, class B> MyClass : A, B {};

The two options I see are:

  • Pass MyClass<A, B> to PolicyA and PolicyB as template parameters and do something like dynamic_cast<MyClass<A, B>*>(this)->foo();. However this may easily get very complicated (especially if it's not only B calling A but also the other way).
  • Have a base class for each policy where all the functions are declared as pure virtual. This fails when one of the function needs its own template parameters, since virtual template functions are not possible.

This should be a common problem, but I didn't find anything. I guess there probably is some boost-magic or something. How would you solve this?

Edit: See also the followup here.

解决方案

Why doesn't PolicyB encapsulate PolicyA and call it as it needs? PolicyB is obviously aware of PolicyA - no need for base classes or inheritance.

It seems like all of your Policy implementations have some common apply method that outside callers can agree upon?

Edit:

Right, so if it's not just a simple case of one particular Policy implementation needing access to another, you should probably focus on creating a single interface for Policy - perhaps even a Rule class that can be composed together to form a particular Policy - see the Specification pattern.

class Rule {
    allows(Foo foo);
}

class Policy {
    Rule rule = new NotNullRule().and(someOtherRule);

    void applyTo(Foo foo) {
        if (rule.allows(foo)) {
            return foo;
        }
        foo.disable();
    }
}

Call them whatever you want, Condition, Rule, Specification - they're useful for assembling pieces of logic together in a manner that doesn't depend on them. Then when someone is looking at PolicyA they can see, clearly, the rules/conditions that govern its behavior, objectified.

Once you have policy implementations in place, if you need to enforce several policies on something, you can use the composite pattern again so that external classes only see a Policy interface. But again, you have a clear 'named' policies that are being referred to, not just cross-calling functions.

这篇关于C ++:多个策略相互调用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆