基于规则的设计

发布于 2024-09-15 01:31:13 字数 105 浏览 2 评论 0原文

我会定期阅读一些离散状态,并对状态差异应用一些规则,我会报告一些错误。 规则可以及时更改。

解决此类问题的最佳实践是什么?

谢谢。

I will regularly read some discrete states, and applying some rules to differences in states I will report some errors. Rules can be changed in time.

What are best-practices to solve such a problem?

Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

智商已欠费 2024-09-22 01:31:13

我首先想到的是 Gof 的 设计模式,称为 策略

您可以在具体策略对象中对规则进行编码。
因此,您可以拥有一个随时间变化的特定具体策略对象。
但最好是更改具体策略对象以反映新规则,恕我直言。

维基百科链接有一个 C++ 示例。但如果您不熟悉设计模式和/或需要进一步解释,请询问。

What first comes to my mind is the Gof' Design Pattern called Strategy.

You encode your rules in the Concrete Strategy objects.
So you could have a particular Concrete Strategy object that is changing in time.
But best is to change of Concrete Strategy objects to reflect the new rule, IMHO.

The wikipedia link has an example in C++. But if you are new to design patterns and/or need further explanations about it, just ask.

笔芯 2024-09-22 01:31:13

除了策略模式之外,我还会使用单例模式。
一种可能的实现(尽管这是相当开放的,如果您想要一组灵活的规则,您应该使用另一个类作为“规则”实体。但是,这种方式更容易理解):

class Rules {
public:
    virtual bool rule_1(Data *) = 0;
    // ...
    virtual bool rule_n(Data *) = 0;

    static Rules * getRules()
        {
            // The only place in which to change the rule set
            if ( ruleSet == NULL ) ruleSet = new Rules_September2010();
            return ruleSet;
        }
protected:
    Rules();
    static Rules * ruleSet;
};

class Rules_August2010 : public Rules {
public:
    bool rule_1(Data *);
    bool rule_n(Data *);
};

class Rules_September2010 : public Rules {
public:
    bool rule_1(Data *);
    bool rule_n(Data *);
};

当然,这是标头的指示(s)。缺少实施文件。希望这有帮助。

I'd also use the singleton pattern, apart from the Strategy one.
One possible implementation (though this is quite open, if you want a flexible set of rules you shoud use another class for the "Rule" entity. However, this way it is simpler to understand):

class Rules {
public:
    virtual bool rule_1(Data *) = 0;
    // ...
    virtual bool rule_n(Data *) = 0;

    static Rules * getRules()
        {
            // The only place in which to change the rule set
            if ( ruleSet == NULL ) ruleSet = new Rules_September2010();
            return ruleSet;
        }
protected:
    Rules();
    static Rules * ruleSet;
};

class Rules_August2010 : public Rules {
public:
    bool rule_1(Data *);
    bool rule_n(Data *);
};

class Rules_September2010 : public Rules {
public:
    bool rule_1(Data *);
    bool rule_n(Data *);
};

Of course this is an indication of the header(s). The implementation files are missing. Hope this helps.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文