Explain when u will use Observer pattern and how u will
implement in c++ .



Explain when u will use Observer pattern and how u will implement in c++ ...

Answer / 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

More STL Interview Questions

How does an stl file work?

0 Answers  


Describe the My Computer and My Documents folders; identify the elements that are present in every Window.

0 Answers  


What is the underlying datastructure of map?

5 Answers   BIA, Siemens,


a program using one dimensional array that searches a number if it is found on the list of given input numbers given by the user and locate its exact location in the list.

0 Answers  


What is the disadvantage of templates ?

2 Answers   NSN, Symphony,






Is stl open source?

0 Answers  


if x<>=z then statement end what is the cyclomatic complexity

5 Answers  


WHAT IS THE DIFFERENCE BETWEEN C++ AND VC++

1 Answers   Syntel,


tell about sorted linked list

1 Answers  


How do you convert stl to steps?

0 Answers  


What is the STL?

2 Answers   Epson, HP,


What are the components of stl?

0 Answers  


Categories