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

what is compiler?

4 Answers   NASA,


Define stl.

0 Answers  


Describe how to safeguard a system through acquisition of an antivirus Program and systematic backup.

0 Answers  


what is electronic software

1 Answers  


What is the Difference between CArray and CList?

1 Answers   ProdEx Technologies, Siemens,






how can u do connectivity in c++ language? plz send me connectivity code in c++ ?

0 Answers   Ascent,


Is stl part of c++ standard?

0 Answers  


Define the terms: field, record, table and database

5 Answers  


In what scenario does the Logical file and Physical file being used?

0 Answers   informatics,


WHAT IS FIBONACCI SERIES?

4 Answers   Stewart,


If P is the population on the first day of the year, B is the birth rate, and D is the death rate, the estimated population at the end of the year is given by the formula: The population growth rate is given by the formula: B – D Write a program that prompts the user to enter the starting population, birth and death rates, and n, the number of years. The program should then calculate and print the estimated population after n years. Your program must have at least the following functions: 1. growthRate: This function takes its parameters the birth and death rates, and it returns the population growth rate. 2. estimatedPopulation: This function takes its parameters the current population, population growth rate, and n, the number of years. It returns the estimated population after n years Your program should not accept a negative birth rate, negative death rate, or a population less than 2. please answer my question ....

0 Answers   Microsoft,


What is a stl vector?

0 Answers  


Categories