#include <iostream>
#include <vector>
using namespace std;
class Observer
{
private :
int x,y;
public :
void Update(int x,int y)
{
this->x = x;
this->y = y;
}
void Print()
{
cout << "x = " << x << ",y = " << y << endl;
}
};
class Subject
{
private :
int x,y;
vector<Observer *> vec;
public :
Subject()
{
x = 0;
y = 0;
Notify();
}
Subject(int x,int y)
{
this->x = x;
this->y = y;
Notify();
}
void AddObserver(Observer *t)
{
vec.push_back(t);
}
void Notify()
{
vector <Observer *>::iterator it;
for(it = vec.begin();it != vec.end();it++)
{
(*it)->Update(x,y);
}
}
void DataChange(int x,int y)
{
this->x = x;
this->y = y;
Notify();
}
void Print()
{
vector <Observer *>::iterator it;
for(it = vec.begin();it != vec.end();it++)
{
(*it)->Print();
}
}
};
int main()
{
Observer *obj1 = new Observer();
Observer *obj2 = new Observer();
Subject *s = new Subject();
s->AddObserver(obj1);
s->AddObserver(obj2);
s->Notify();
s->Print();
s->DataChange(3,5);
s->Print();
return 0;
}
(2)
#include <iostream>
#include <vector>
using namespace std;
class Observer
{
private :
int x,y;
Subject *s;
public :
Observer(Subject *s)
{
this->s = s;
s->Add(this);
}
void Update(int x,int y)
{
this->x = x;
this->y = y;
}
void Print()
{
cout << "x = " << x << "y = " << y << endl;
}
};
class Subject
{
private :
int x,y;
vector<Observer *> vec;
public :
Subject()
{
x = 0;
y = 0;
Notify();
}
Subject(int x,int y)
{
this->x = x;
this->y = y;
Notify();
}
void Add(Observer *t)
{
vec.push_back(t);
}
void Notify()
{
vector<Observer *>::iterator it;
for(it = vec.begin();it != vec.end();it++)
{
(*it)->Update(x,y);
}
}
void DataChange(int x,int y)
{
this->x = x;
this->y = y;
Notify();
}
void Print()
{
vector <Observer *>::iterator it;
for(it = vec.begin();it != vec.end();it++)
{
(*it)->Print();
}
}
};
int main()
{
Subject *su = new Subject();
Observer *obj1 = new Observer(su);
Observer *obj2 = new Observer(su);
su->Notify();
su->Print();
su->DataChange(3,5);
su->Print();
return 0;
}
(3)
#include <iostream>
#include <vector>
using namespace std;
class Subject
{
public :
virtual void Add(Observer *t) = 0;
virtual void Notify() = 0;
};
class ConcreteSubjectA : public Subject
{
private :
int x,y;
vector<Observer *> vec;
public :
ConcreteSubjectA()
{
x = 0;
y = 0;
Notify();
}
ConcreteSubjectA(int x,int y)
{
this->x = x;
this->y = y;
Notify();
}
void Add(Observer *t)
{
vec.push_back(t);
}
void Notify()
{
vector<Observer *>::iterator it;
for(it = vec.begin();it != vec.end();it++)
{
(*it)->Update(x,y);
}
}
void DataChange(int x,int y)
{
this->x = x;
this->y = y;
Notify();
}
};
class ConcreteSubjectB : public Subject
{
private :
int z;
vector<Observer *> vec;
public :
ConcreteSubjectB()
{
z = 0;
Notify();
}
ConcreteSubjectA(int z)
{
this->z = z;
Notify();
}
void Add(Observer *t)
{
vec.push_back(t);
}
void Notify()
{
vector<Observer *>::iterator it;
for(it = vec.begin();it != vec.end();it++)
{
(*it)->Update(z);
}
}
void DataChange(int z)
{
this->z = z;
Notify();
}
};
class Observer
{
protected :
Subject *s;
public :
Observer(Subject *t)
{
s = t;
s->Add(this);
}
virtual void Update(int x,int y) = 0;
};
class ConcreteObserverA : public Observer
{
private :
int x,y;
public :
ConcreteObserverA(Subject *t) : Observer(t){}
void Update(int x,int y)
{
this->x = x;
this->y = y;
}
};
class ConcreteObserverB : public Observer
{
private :
int z;
public :
ConcreteObserverB(Subject *t) : Observer(t){}
void Update(int x,int y)
{
this->z = x+y;
}
};
int main()
{
ConcreteSubjectA *cs1 = new ConcreteSubjectA();
ConcreteObserver *obA1 = new ConcreteObserverA(cs1);
ConcreteObserver *obA2 = new ConcreteObserverA(cs1);
cs1->DataChange(3,5);
return 0;
}