Explain when u will use Observer pattern and how u will
implement in c++ .
Answer Posted / subeetha
The observer pattern (a subset of the asynchronous
publish/subscribe pattern) is a software design pattern in
which an object, called the subject, maintains a list of
its dependents, called observers, and notifies observers
automatically of any state changes, usually by calling one
of their methods. It is mainly used to implement
distributed event handling systems.
Sample Program:
#include <iostream>
#include <vector>
using namespace std;
class Subject {
// 1. "independent" functionality
vector < class Observer * > views; // 3. Coupled only
to "interface"
int value;
public:
void attach(Observer *obs) {
views.push_back(obs);
}
void setVal(int val) {
value = val;
notify();
}
int getVal() {
return value;
}
void notify();
};
class Observer {
// 2. "dependent" functionality
Subject *model;
int denom;
public:
Observer(Subject *mod, int div) {
model = mod;
denom = div;
// 4. Observers register themselves with the Subject
model->attach(this);
}
virtual void update() = 0;
protected:
Subject *getSubject() {
return model;
}
int getDivisor() {
return denom;
}
};
void Subject::notify() {
// 5. Publisher broadcasts
for (int i = 0; i < views.size(); i++)
views[i]->update();
}
class DivObserver: public Observer {
public:
DivObserver(Subject *mod, int div): Observer(mod, div){}
void update() {
// 6. "Pull" information of interest
int v = getSubject()->getVal(), d = getDivisor();
cout << v << " div " << d << " is " << v / d
<< '\n';
}
};
class ModObserver: public Observer {
public:
ModObserver(Subject *mod, int div): Observer(mod, div){}
void update() {
int v = getSubject()->getVal(), d = getDivisor();
cout << v << " mod " << d << " is " << v % d
<< '\n';
}
};
int main() {
Subject subj;
DivObserver divObs1(&subj, 4); // 7. Client configures
the number and
DivObserver divObs2(&subj, 3); // type of Observers
ModObserver modObs3(&subj, 3);
subj.setVal(14);
}
14 div 4 is 3
14 div 3 is 4
14 mod 3 is 2
Is This Answer Correct ? | 3 Yes | 0 No |
Post New Answer View All Answers
What is the stl, standard template library?
When did c++ add stl?
How connect plc and pc through software
how can u do connectivity in c++ language? plz send me connectivity code in c++ ?
To modify an, existing worksheet. What steps are involved for: 1. Inserting and deleting rows and columns. 2. Printing cell formulas 3Jld displayed values 3. Using the page setup command
What is stl language?
Is stl open source?
what is template and type convertion
Is stl part of c++ standard?
What are the various types of stl containers?
Name the different types of stl containers.
How do I convert a stl file?
Describe the My Computer and My Documents folders; identify the elements that are present in every Window.
Write a C/C++ program to add a user to MySQL. The user should be permitted to only "INSERT" into the given database.
Do you like to Submit Questions in Bulk under Same Category?? Then use our Bulk ListerDo you like to Submit Questions in Bulk under Same Category?? Then use our Bulk Lister