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.