-What is Observer Design Pattern?
#include<iostream>
class Runrate {public:
float run_rate, over;
int predicted;
void update(int run, int wicket, float over) {
if(over == 0)
std::cout<<'\n';
else {
this->over = over;
over = (int)over + ((float)over - (int)over)*1.667;
// std::cout<<over<<'\n';
run_rate = (float)run/over;
predicted = (float)run_rate*20;
}
}
};
class Disp_score {
int run, wicket;
float over;
public:
void display(int run, int wicket, float over, float run_rate, int predicted) {
std::cout<<run<<"/"<<wicket<<"\t"<<over<<"\tRunrate:"<<run_rate<<"\nPredicted score"<<predicted<<'\n';
}
};
class Cricket {public:
int run, wicket;
float over;
Runrate rate;
Disp_score dis;
Cricket(int runs, int wis, float overs, float rrate, int pred){
rate.update(runs, wis, overs);
dis.display(runs, wis, overs, rrate, pred);
}
void notify(int run, int wis, float over){
rate.update(run, wis, over);
dis.display(run, wis, over, rate.run_rate, rate.predicted);
}
void set_data(int runs, int wis, float overs){
run = runs;
wicket = wis;
over = overs;
notify(run, wicket, over);
}
};
int main(){
Cricket s(0,0,0.0, 0.0, 0);
int r, w;
float o;
while(1){
std::cout<<"Update:\nRuns wickets overs";
std::cin>>r>>w>>o;
s.set_data(r, w , o);
}
return 0;
}
Consider a class 'Cricket' as a subject, which contain few members on which two Observer classes; Run_rate and Disp_score which are depend on the members of the subject class. Run_rate observer needs members run and overs to calculate the run rate and predicted score. And observer Disp_score needs members runs, wickets, and overs from Cricket class, and run_rate and predicted of Runrate class.
Here, class Cricket has 2 observers Runrate and Disp, and Disp class is observer of both Runrate class as well as Cricket class.
When multiple objects aka observers are dependancies of a subject class, and it's elements, the Observer Design Pattern comes in action. So, how does it work? When an element of the subject class changes, all it's dependant objects are notified, and updated automatically with the changes occured.
Let's take an example. Consider an IPL auction going on. The auctioneer starts a bid on a player, say Sunil Narayan, who is the subject in this case. It has 1 property as price which is a dependancy in all the observer objects; teams here. Whenever a bidder i.e. an observer raises the bid price, the final price get's updated and notified to all other bidders automatically in real time, and new bid is added to the updated price. Similarly, the observer design pattern works in such manner.
Another example of ongoing T-20 cricket match, observer design pattern is used:
Another example of ongoing T-20 cricket match, observer design pattern is used:
#include<iostream>
class Runrate {public:
float run_rate, over;
int predicted;
void update(int run, int wicket, float over) {
if(over == 0)
std::cout<<'\n';
else {
this->over = over;
over = (int)over + ((float)over - (int)over)*1.667;
// std::cout<<over<<'\n';
run_rate = (float)run/over;
predicted = (float)run_rate*20;
}
}
};
class Disp_score {
int run, wicket;
float over;
public:
void display(int run, int wicket, float over, float run_rate, int predicted) {
std::cout<<run<<"/"<<wicket<<"\t"<<over<<"\tRunrate:"<<run_rate<<"\nPredicted score"<<predicted<<'\n';
}
};
class Cricket {public:
int run, wicket;
float over;
Runrate rate;
Disp_score dis;
Cricket(int runs, int wis, float overs, float rrate, int pred){
rate.update(runs, wis, overs);
dis.display(runs, wis, overs, rrate, pred);
}
void notify(int run, int wis, float over){
rate.update(run, wis, over);
dis.display(run, wis, over, rate.run_rate, rate.predicted);
}
void set_data(int runs, int wis, float overs){
run = runs;
wicket = wis;
over = overs;
notify(run, wicket, over);
}
};
int main(){
Cricket s(0,0,0.0, 0.0, 0);
int r, w;
float o;
while(1){
std::cout<<"Update:\nRuns wickets overs";
std::cin>>r>>w>>o;
s.set_data(r, w , o);
}
return 0;
}
Consider a class 'Cricket' as a subject, which contain few members on which two Observer classes; Run_rate and Disp_score which are depend on the members of the subject class. Run_rate observer needs members run and overs to calculate the run rate and predicted score. And observer Disp_score needs members runs, wickets, and overs from Cricket class, and run_rate and predicted of Runrate class.
Here, class Cricket has 2 observers Runrate and Disp, and Disp class is observer of both Runrate class as well as Cricket class.
References: https://sourcemaking.com/design_patterns/observer/cpp/1
https://www.geeksforgeeks.org/observer-pattern-set-1-introduction/
