Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <vector>
- // abstract base class (interface) for AnalyzedObject
- class AnalyzedObject {
- public:
- AnalyzedObject() {}
- virtual ~AnalyzedObject() {}
- // provide getters for all the things an evaluator may consider
- virtual bool Do_you_support_key_feature_one() const = 0;
- virtual bool Do_you_support_key_feature_two() const = 0;
- virtual int YearsService() const = 0;
- virtual int Price() const = 0;
- };
- // abstract base class (interface) for voter/evaluator
- class Evaluator {
- public:
- Evaluator();
- virtual ~Evaluator() {}
- virtual bool vote(const AnalyzedObject&) = 0;
- };
- // raw pointers here is ill-advised
- typedef std::vector<Evaluator*> voter_panel_type;
- // abstract base class (interface) for voting policy
- class VotingPolicy {
- public:
- VotingPolicy();
- virtual ~VotingPolicy() {}
- virtual bool vote(voter_panel_type, const AnalyzedObject&) = 0;
- };
- // implement a voting policy
- class MajorityRules : public VotingPolicy {
- bool vote(std::vector<Evaluator*> evaluators, const AnalyzedObject &object) {
- voter_panel_type::size_type yes = 0;
- for (auto voter : evaluators) {
- if (voter->vote(object))
- yes += 1;
- }
- return yes * 2 > evaluators.size();
- }
- };
Add Comment
Please, Sign In to add comment