Reputation: 4211
I need to filter data on the basis of certain rules. For example, I have a list of records, and on the other hand I have a set of rules. Each rule will filter out this list. I am also maintaining a configuration file for these rules. These rules could be turn on/off through a configuration file.
For example, consider my configuration file..
[
{
"rule": "rule-1",
"class": "org.company.RuleOneFilter.class",
"isEnabled": true
},
{
"rule": "rule-2",
"class": "org.company.RuleTwoFilter.class",
"isEnabled": false
},
{
"rule": "rule-3",
"class": "org.company.RuleThreeFilter.class",
"isEnabled": true
}
]
Using this file, rules would be plugged-in or unplugged easily. I have checked the specification of Chain Of Responsibility but still confused whether to go for it or not! Can someone suggest me an appropriate design pattern to implement this? Or if there's any framework to fulfill this need?
Upvotes: 0
Views: 279
Reputation: 131456
If the rules are accumulative, the chain of responsibility seems an overhead.
You don't need to link rules in a chain and you don't have the notion of chain/successor/and so for too.
What you need is basic : an iterator pattern. You iterate on each element of a rules list and according to their state (enabled or not), the rule will be executed or not.
Of course each concrete rule has to derive from a base class to be manipulated in an uniform way.
Now if the rules are not accumulative but exclusive concerning their execution and that you need to set a specific order between the candidates rules, the chain of responsibility makes sense.
Upvotes: 1