-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathobserver design pattern.cpp
More file actions
136 lines (127 loc) · 3.1 KB
/
Copy pathobserver design pattern.cpp
File metadata and controls
136 lines (127 loc) · 3.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
#include<iostream>
#include<vector>
using namespace std;
/*
A program to simulate stock market system
Author:Ankit Mathur
*/
//Observer abstract class
class Observer
{
public:
virtual void update()=0;
};
//Subject abstract class
class Subject
{
public:
virtual void RegisterObserver(Observer *o)=0;//Used to register obserervers for this subject
virtual void notifyObserver()=0;//Used to notify all Observers
};
class StockPriceGrabber:public Subject//Class for stock price subject
{
/*
It will privide methods to update and get stock prices of different companies
Whenever a stock price updates,It will notify all the Observers.
*/
private:
int IbmPrice;//It will store IBM Stock Price
int GooglePrice;//It wil store Google Stock Price
int SamsungPrice;//It will store Samsung Stock Price
vector<Observer *>Observer_list;//It will store list of observers
public:
StockPriceGrabber(int IbmPrice,int GooglePrice,int SamsungPrice)
{
this->IbmPrice=IbmPrice;
this->GooglePrice=GooglePrice;
this->SamsungPrice=SamsungPrice;
}
int getIbmPrice()
{
return IbmPrice;
}
int getGooglePrice()
{
return GooglePrice;
}
int getSamsungPrice()
{
return SamsungPrice;
}
void setIbmPrice(int val)
{
IbmPrice=val;
notifyObserver();
}
void setGooglePrice(int val)
{
GooglePrice=val;
notifyObserver();
}
void setSamsungPrice(int val)
{
SamsungPrice=val;
notifyObserver();
}
void RegisterObserver(Observer *o)//Used by Observers to register
{
Observer_list.push_back(o);
}
void notifyObserver()//Used for notifying all Observers
{
for(int i=0;i<Observer_list.size();++i)
{
Observer_list[i]->update();
}
}
};
class GoogleObserver:public Observer//A class for Observing only Google stock prices
{
private:
int GooglePrice;//variable for storing current Google stock price
StockPriceGrabber *model;
public:
GoogleObserver(StockPriceGrabber *s)
{
model=s;
model->RegisterObserver(this);//registering this object
GooglePrice=model->getGooglePrice();
}
void update()//This will be called as soon as price gets updated
{
GooglePrice=model->getGooglePrice();
cout<<"GooglePrice updated "<<GooglePrice<<endl;
}
};
class AllStockPriceObserver:public Observer//Class for observing all Stock Prices
{
private:
int GooglePrice;
int IbmPrice;
int SamsungPrice;
StockPriceGrabber *model;
public:
AllStockPriceObserver(StockPriceGrabber *s)
{
model = s;
model->RegisterObserver(this);
GooglePrice=model->getGooglePrice();
SamsungPrice=model->getSamsungPrice();
IbmPrice=model->getIbmPrice();
}
void update()
{
GooglePrice=model->getGooglePrice();
SamsungPrice=model->getSamsungPrice();
IbmPrice=model->getIbmPrice();
cout<<"GooglePrice: "<<GooglePrice<<" IbmPrice: "<<IbmPrice<<" SamsungPrice: "<<SamsungPrice<<endl;
}
};
int main()
{
StockPriceGrabber *source = new StockPriceGrabber(10,15,20);//stock price grabber instance
GoogleObserver *dest1 = new GoogleObserver(source);
AllStockPriceObserver *dest2 = new AllStockPriceObserver(source);
source->setGooglePrice(10);
return 0;
}