Observer Pattern

12 minutes
Share the link to this page
Copied
  Completed
You need to have access to the item to view this lesson.
One-time Fee
$69.99
List Price:  $99.99
You save:  $30
€65.14
List Price:  €93.07
You save:  €27.92
£55.73
List Price:  £79.62
You save:  £23.88
CA$95.61
List Price:  CA$136.60
You save:  CA$40.98
A$106.30
List Price:  A$151.87
You save:  A$45.56
S$94.64
List Price:  S$135.20
You save:  S$40.56
HK$546.91
List Price:  HK$781.33
You save:  HK$234.42
CHF 63.50
List Price:  CHF 90.72
You save:  CHF 27.21
NOK kr764.69
List Price:  NOK kr1,092.46
You save:  NOK kr327.77
DKK kr485.92
List Price:  DKK kr694.20
You save:  DKK kr208.28
NZ$117
List Price:  NZ$167.15
You save:  NZ$50.15
د.إ257.06
List Price:  د.إ367.25
You save:  د.إ110.18
৳7,661.98
List Price:  ৳10,946.16
You save:  ৳3,284.17
₹5,839.65
List Price:  ₹8,342.71
You save:  ₹2,503.06
RM331.75
List Price:  RM473.95
You save:  RM142.20
₦86,437.65
List Price:  ₦123,487.65
You save:  ₦37,050
₨19,492.21
List Price:  ₨27,847.21
You save:  ₨8,355
฿2,575.56
List Price:  ฿3,679.53
You save:  ฿1,103.97
₺2,262.43
List Price:  ₺3,232.18
You save:  ₺969.75
B$357.76
List Price:  B$511.10
You save:  B$153.34
R1,296.01
List Price:  R1,851.52
You save:  R555.51
Лв127.38
List Price:  Лв181.98
You save:  Лв54.60
₩95,113.23
List Price:  ₩135,881.87
You save:  ₩40,768.63
₪260.11
List Price:  ₪371.60
You save:  ₪111.49
₱3,999.61
List Price:  ₱5,713.97
You save:  ₱1,714.36
¥10,715.43
List Price:  ¥15,308.41
You save:  ¥4,592.98
MX$1,185.45
List Price:  MX$1,693.57
You save:  MX$508.12
QR254.79
List Price:  QR364.01
You save:  QR109.21
P955.69
List Price:  P1,365.33
You save:  P409.64
KSh9,427.65
List Price:  KSh13,468.65
You save:  KSh4,041
E£3,355.67
List Price:  E£4,794.02
You save:  E£1,438.35
ብር3,989.43
List Price:  ብር5,699.43
You save:  ብር1,710
Kz58,616.62
List Price:  Kz83,741.62
You save:  Kz25,125
CLP$66,326.02
List Price:  CLP$94,755.52
You save:  CLP$28,429.50
CN¥506.51
List Price:  CN¥723.62
You save:  CN¥217.11
RD$4,049.59
List Price:  RD$5,785.38
You save:  RD$1,735.78
DA9,420.19
List Price:  DA13,457.99
You save:  DA4,037.80
FJ$157.70
List Price:  FJ$225.30
You save:  FJ$67.59
Q542.62
List Price:  Q775.21
You save:  Q232.58
GY$14,613.08
List Price:  GY$20,876.73
You save:  GY$6,263.64
ISK kr9,792.30
List Price:  ISK kr13,989.60
You save:  ISK kr4,197.30
DH706.05
List Price:  DH1,008.69
You save:  DH302.63
L1,239.86
List Price:  L1,771.31
You save:  L531.44
ден4,010.92
List Price:  ден5,730.13
You save:  ден1,719.21
MOP$562.15
List Price:  MOP$803.11
You save:  MOP$240.95
N$1,302.54
List Price:  N$1,860.85
You save:  N$558.31
C$2,571.43
List Price:  C$3,673.63
You save:  C$1,102.20
रु9,317.58
List Price:  रु13,311.40
You save:  रु3,993.82
S/262.81
List Price:  S/375.46
You save:  S/112.65
K268.53
List Price:  K383.63
You save:  K115.10
SAR262.51
List Price:  SAR375.03
You save:  SAR112.52
ZK1,879.71
List Price:  ZK2,685.42
You save:  ZK805.70
L324.19
List Price:  L463.14
You save:  L138.95
Kč1,629.65
List Price:  Kč2,328.17
You save:  Kč698.52
Ft25,373.17
List Price:  Ft36,248.95
You save:  Ft10,875.77
SEK kr758.75
List Price:  SEK kr1,083.98
You save:  SEK kr325.22
ARS$61,468.94
List Price:  ARS$87,816.53
You save:  ARS$26,347.59
Bs482.36
List Price:  Bs689.12
You save:  Bs206.75
COP$272,946.91
List Price:  COP$389,940.87
You save:  COP$116,993.96
₡35,623.88
List Price:  ₡50,893.45
You save:  ₡15,269.56
L1,732.95
List Price:  L2,475.75
You save:  L742.80
₲523,151.84
List Price:  ₲747,391.81
You save:  ₲224,239.96
$U2,683.09
List Price:  $U3,833.15
You save:  $U1,150.06
zł281.85
List Price:  zł402.67
You save:  zł120.81
Already have an account? Log In

Transcript

The next pattern which we are going to see is observer pattern, and this is also behavior pattern. Now, in this particular pattern, we have to define two different objects and the name of this objects are actually subject and observable. And to understand these two objects, I want you to first understand the word observation. Now, observation is a simple English word. And you know when we say that keep the thing which is under observation, we are going to continuously observe that thing. The same thing happens with this pattern also, the observer observes the subject and when something specifically changes inside that, and when this changes occurs to the subject, it is going to notify this thing to all the observers of that particular subject.

So, it's something always that the changes are going to be there in this We most of the time use this observer pattern when we want such kind of notifications. In our case, we are still again taking a scenario of a KitKat computer store. And this time the KitKat computer store is going to have number of products. And the scenario is that the product prices are going to change. Now, because this is a computer store is going to have many customers in that and all of this customers who have actually purchased some particular product, whenever the price changes are going to happen in the product, we are going to send notifications to our customers. Let's see this thing in action in our Visual Studio code.

In my Visual Studio, I created one simple console application first. And if you see this console application name if KitKat CS for computer store, inside this computer store application. The first thing is we are going to right click and add new item and this new item is going to be one interface. With the name I product, this I product interface is going to have some product information inside that. But before that because this is a product in customer relationship, we need one more interface. So I'm adding one new interface.

And this new interface is going to have a name I customer. I'm sure you have created this kind of application earlier also, which is going to have a relation between product and customer. And whenever we have product and customer information, we always know that this is going to be something which is many to many relationship, because one product can be purchased by more than one customers. And one customer can purchase more than one products. To maintain the relationship of these two interfaces inside the I product. First I'm going to create a structure like this.

So you can see I have three properties on the top inside the product, which are actually going to define the name of the product, the price of the product and then we Have one simple notify method which is returning void and is going to notify for my price. We have two more methods inside that which are actually add follower and remove follower. Now add follower and remove follower methods are actually going to add and remove the customers when this particular customer is going to buy that particular product. This add follower and remove follower method is actually having one parameter which is that customer of Type II customer. So, this is actually showing me the relationship between interface I customer and I product and same way if I go to ii customer.cs interface inside this interface I'm going to have one method which is going to be update and this I customer update method is going to have one parameter which is going to be off type product.

So, we have an interrelation between this two interfaces I customer and I product. I customer Having a method, which is having a parameter of type I product. And I product is also having a method, which is having a parameter of type I customer. Let's make this two interfaces public so that we can use it anywhere. And once these two interfaces are public, we are going to move forward to use this customers and products in our code. Now, I'm going to right click on my project and I'm going to add a new C sharp class with the name customer.

This customer C sharp class is actually going to implement my II customer in that. So I'm going to right click on this and I'm going to implement interface. When we do that, we know that we'll get this method update inside this and we are going to write some simple logic inside this which is having a simple console dot write line and this is going to print the update for this particular customer that now this particular product is now at this particular price. So if because of the price a notification, we are just going to print the simple console dot write line. Once we are done with the customer, it's time to create some products now, so we are going to right click on this particular project, and I'm going to add a new feature class. And let's say that because it's a computer store, the first product which we are going to create is having a name CPU.

This CPU class is going to be public. And I'm going to add that the CPU is going to have interface I product implemented in that. So we are going to do interface implement. And you can see that I have the property name price. And those three methods are already inside this particular implementation. Now we have to write the logic inside this.

And I'm going to replace this code with my logic which is going to be like this. So you can see on the top we have two private variables, one is price and the other one is actually a generic collection of Type II customers, we know that one particular product is going to have lists of customers associated with that. So we have the generic list, which is going to hold the customers. And then inside the constructor of this particular CPU class, we are having one parameter, which is going to set the price for that, then we are just dealing with the name of the product. So the property name is going to have a name CPU, we never change the name. So it's just going to return this always.

And in the price, we are going to first return the base by default price which we are going to set in the constructor. And then whenever we have a price change, we are going to call the set at that time. And inside the set we have a method notify, in which we are actually taking the new price whatever notification price is going to be there and then we are going to notify this price to the list of customers which are associated with this product. So at that time, we are actually running For each loop and this for each loop is going to deal with a list of customers and for each customer is going to call the method update which is actually already defined in our customer class and for each customer is going to print that the new price of the product is now this one.

After this you can see that we have a simple add follower and remove follower methods which are actually just adding our customer into the collection and removing the customers from that. So when my customer is going to purchase this particular CPU product, we are going to call add follower and if he is going to return the product we are going to call remove follower. It's so simple like this, using the same class we can create one more product. So let me just add a new C sharp class. And suppose that the second product which we are adding is the RAM and inside this ram also we are going to make this class public Go to specify that this is also going to be one of the I product implementation. And the logic of this RAM is also going to be similar to this.

So I'm going to copy this full code from the CPU. And by pasting this thing into this, we are just going to make sure that this is going to be a constructor RAM, which is going to have the price parameter in that the name of the class is going to be RAM, as well as all the other things are going to be seen because this is going to be a list of customers for the ram product. Once we are done with this, we are all set to go with our main method inside the program.cs file. But I hope you understood this two interfaces and the connection between these two interfaces. And then using the eye customer interface, we created one customer class. By using the eye product interface we created two products, RAM and CPU.

If you want to add some more customers and some more products, you can go with that. But I'm moving forward to my program. dot CS file, where I have a main method, my main method logic is going to be very simple, in which I'm simply first creating a new product CPU and RAM. We know that when we call this thing, it's going to pass on this values into our CPU and RAM constructor. So the price of the CPU is going to be 2500 in the beginning, and then save in the RAM, it's going to be 900. Now after this, you can see that we have created three customers.

Customer one, class two and class three are my three objects of the three customer classes. And then I'm certifying that I'm going to do CPU dot add follower cost one. It's something like this that our first customer purchase CPU, same way the second customer also purchase CPU. The third customer which we created is actually purchased using Ram. So now this two are actually followers of the CPU. And the third one is a follower of RAM.

It is also possible that one particular customer is going to purchase both products. Like for example, let's say that we are adding one line inside this which is Ram dot add follower. And then in this we are going to add cost to. So customer two is actually that same customer who purchased CPU as well as ram both the products, while cost one has purchased CPU and cost three has purchased Ram. When we do this thing, now we are doing a price change. We know that when we do the price change is actually going to call the set of each property price.

And inside that we have a notify method, which is actually going to notify for all the customers. To check this thing we are just going to have console dot read and let's see this thing in action. So when we run this application, you can see that it's showing me that CPU is now at two one double zero rupees only. CPU is now at two one double zero rupees only. This is printing this Think twice because CPU is purchased by two different customers. So this is actually for the first customer.

And this is actually for the customer to well, if you see the last two lines are actually for customer three and two, we are not printed the name of the customers in this right now. But if you want to try that you can even print the name of the customer and the customer object also with that. So the notification is four times. Now suppose one of the customer has removed that particular product. So he has returned that thing. So we are just going to do one thing that maybe my customer three has returned that thing.

So I'm going to do ram dot remove follower and from the Remove follower, we are going to remove our customer which is customer three, the moment customer three has returned the product now will not give him a notification for the price change. And we'll have only three lines in the output. I hope you're getting this application and the structure of the notification in this observer pattern. So the thing which is done observation in our case is a price of the product. People are using this observer pattern by different different ways. Like in my case, I'm using this notification of the observation of the price under the set of the property.

While in other cases, sometime people even use the delegates and events to handle this thing. Whatever you want to use, that's your choice. But this is something which is allowing us to send the notification to the other applications or maybe to the group of customers in our case. Thank you.

Sign Up

Share

Share with friends, get 20% off
Invite your friends to LearnDesk learning marketplace. For each purchase they make, you get 20% off (upto $10) on your next purchase.