views:

85

answers:

3
+1  Q: 

Rule Based Design

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.

A: 

You can use some rule engine. There are lot of rule engines in java, I am not sure abt C++. This post may help you.

+3  A: 

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.

Stephane Rolland
+1 for Strategy Pattern
KenB
+2  A: 

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.

Baltasarq