Guest User

Untitled

a guest
Feb 25th, 2018
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.25 KB | None | 0 0
  1. #include <vector>
  2.  
  3. // abstract base class (interface) for AnalyzedObject
  4. class AnalyzedObject {
  5. public:
  6. AnalyzedObject() {}
  7. virtual ~AnalyzedObject() {}
  8.  
  9. // provide getters for all the things an evaluator may consider
  10. virtual bool Do_you_support_key_feature_one() const = 0;
  11. virtual bool Do_you_support_key_feature_two() const = 0;
  12. virtual int YearsService() const = 0;
  13. virtual int Price() const = 0;
  14. };
  15.  
  16. // abstract base class (interface) for voter/evaluator
  17. class Evaluator {
  18. public:
  19. Evaluator();
  20. virtual ~Evaluator() {}
  21. virtual bool vote(const AnalyzedObject&) = 0;
  22. };
  23.  
  24. // raw pointers here is ill-advised
  25. typedef std::vector<Evaluator*> voter_panel_type;
  26.  
  27. // abstract base class (interface) for voting policy
  28. class VotingPolicy {
  29. public:
  30. VotingPolicy();
  31. virtual ~VotingPolicy() {}
  32. virtual bool vote(voter_panel_type, const AnalyzedObject&) = 0;
  33. };
  34.  
  35. // implement a voting policy
  36. class MajorityRules : public VotingPolicy {
  37. bool vote(std::vector<Evaluator*> evaluators, const AnalyzedObject &object) {
  38. voter_panel_type::size_type yes = 0;
  39. for (auto voter : evaluators) {
  40. if (voter->vote(object))
  41. yes += 1;
  42. }
  43. return yes * 2 > evaluators.size();
  44. }
  45. };
Add Comment
Please, Sign In to add comment