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 stl in oop?

0 Answers  


Question 1)Read the data from one file and remove all the duplicated words and generate an output file containg only using words.Program should also print number of occurance of each words on standard output.(Program must be use STL and iostream object). Question 2)Write a program to convert the lower case contents of file to upper case using STL. Question 3)What is the output of this problem. int i=12; int &r =i; r+r/4; int +p =&r; int *p = &r; P+=r; return 1; Answer :a)12 b)17 c) 30 d)24 E)15 Question 4) #include #include #include void main() { char srcstr[30],desstr[30]; int i,len; clrscr(); cout<<"\nenter the string\n"; cin>>srcstr; len=strlen(srcstr); for(i=0;srcstr[i]!='\0';i++) { desstr[--len]=srcstr[i]; } desstr[i]='\0'; cout<<"\nreversed string is\n"; for(i=0;desstr[i]!='\0';i++) { cout< } getch(); } Answer : A) string output b)tuptuo gnirts c) string d)output Question 5) Class Test { static const Tk=LEN; int q; public: foo(intx =LEN):q(x){}; TGet Q() const { return q:} void call test Foo<long :9> foo(10); std::const<< foo.Getq(); Answer: 7,8,9,10,11 question 6) class A { int a; char b; }; class B:public A { char b; int a; }; What is the size? Answer a)5 bytes for class A and 5 bytes class B b)4 bytes for class A and 8 bytes class B. c).... d)....

2 Answers   Bally Technologies,


give me the defination of inheritance?

5 Answers   Infosys,


What is stl stand for?

0 Answers  


why does the execution of a c++ program start with main()???

11 Answers  






What are the components of stl?

0 Answers  


What does stl stand for in basketball?

0 Answers  


What does stl mean in slang?

0 Answers  


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

0 Answers  


Write a C/C++ program that connects to a MySQL server and checks if the InnoDB plug-in is installed on it. If so, your program should print the total number of disk writes by MySQL.

0 Answers  


write a program that input four digit number and find how many 7 that number contains

4 Answers  


how to get the sum of two integers?

2 Answers  


Categories