Advertisement
fimas

Untitled

Jan 13th, 2014
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.71 KB | None | 0 0
  1. #include <iostream>
  2. #include <vector>
  3. #include <cmath>
  4. #include <string>
  5.  
  6. using namespace std;
  7.  
  8. class observer;
  9.  
  10. class subject
  11. {
  12.   public:
  13.     subject()
  14.     {
  15.     };
  16.     ~subject()
  17.     {
  18.     };
  19.     void addObserver(observer *);
  20.     void removeObserver(observer *);
  21.     virtual void update() = 0;
  22.  
  23.   protected:
  24.     vector < observer * >observerList;
  25. };
  26.  
  27. class observer
  28. {
  29.   public:
  30.     observer()
  31.     {
  32.     };
  33.     ~observer()
  34.     {
  35.     };
  36.     void subscribe(subject *);
  37.     void unsubscribe(subject *);
  38.     virtual void update(vector < int >) = 0;
  39. };
  40.  
  41. void subject::addObserver(observer * o)
  42. {
  43.     observerList.push_back(o);
  44. }
  45.  
  46. void subject::removeObserver(observer * o)
  47. {
  48.     for (vector < observer * >::iterator it =
  49.          observerList.begin(); it != observerList.end(); ++it)
  50.     {
  51.         if (*it == o)
  52.         {
  53.             observerList.erase(it);
  54.             break;
  55.         }
  56.     }
  57. }
  58.  
  59. void observer::subscribe(subject * s)
  60. {
  61.     s->addObserver(this);
  62. }
  63.  
  64. void observer::unsubscribe(subject * s)
  65. {
  66.     s->removeObserver(this);
  67. }
  68.  
  69. class value:public subject
  70. {
  71.   public:
  72.     value()
  73.     {
  74.         update();
  75.     }
  76.  
  77.     void update()
  78.     {
  79.         int t;
  80.         cout << "Enter value: ";
  81.         cin >> t;
  82.         values.push_back(t);
  83.  
  84.         for (vector < observer * >::iterator it =
  85.              observerList.begin(); it != observerList.end(); ++it)
  86.         {
  87.             observer *o = *it;
  88.             o->update(values);
  89.         }
  90.     }
  91.  
  92.   private:
  93.     vector < int >values;
  94. };
  95.  
  96. class avg3:public observer
  97. {
  98.   public:
  99.     avg3(subject * s)
  100.     {
  101.         subscribe(s);
  102.     }
  103.  
  104.     void update(vector < int >t)
  105.     {
  106.         int sum = 0;
  107.         for (int i = 0; i < 3; i++)
  108.         {
  109.             int buf = t.back();
  110.             sum += buf;
  111.             t.pop_back();
  112.         }
  113.         cout << "The average for the last three is " << float (sum) /
  114.             3 << endl;
  115.     }
  116. };
  117.  
  118. int main()
  119. {
  120.     subject *s = new value();
  121.     observer *o;
  122.  
  123.     for (int j = 2; j <= 10; j++)
  124.     {
  125.         if (j == 3)
  126.             o = new avg3(s);
  127.         s->update();
  128.     }
  129.  
  130.     delete s;
  131.     delete o;
  132.     return 0;
  133. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement