As you may already know from my previous tutorials, it is better to start with the abstraction first.
So let's see this observer interface, which has only one method that updates the observers and doesn't
return anything.
And these two are the classes that concrete observers that implement this interface to store observer
and the supply chain observer.
But before diving into the observers.
Let's take a look at the product first.
So basically here we have a program that is concentrated around the product subject here.
So whenever the product will change it state, so, for example, a product will be sold, the both
of the observers will be notified and the bolt will trigger some some actions on their behalf.
So let's see this product subject first before taking a look in the observers.
So basically, the product subject will have will contain a list of observers and also a state that
is under the form of a string.
So we have a gathering center for the state.
But as you can see, the center here will also call this method notify all observers.
This is because whenever, like we said, whenever a product will change at state, we need to notify
the first of the of the state change.
Here we have an attached observer method so that we can attach multiple observers to the same product,
to the same object and then notify all observers will basically go through this list, this observer
observers, and it will call the update method for all the observers.
And now let's see the observers and we can see that they saw reference to the product subject.
And within the constructor, the product subject is initialized and is assigned to the value of of its
type.
And as you can see, also the attach observer will attach this product, this object to to the product
subject.
So you can see that this is very clear.
Whenever an observer is created, we need to attach it to to the subject.
So the object method will just contain a system of the printing.
And this is applicable for the supply chain observer as well.
And as you can see, the system now on will output a different message so that it is clear that both
of them were called and the update works well whenever the product will suffer a change in its state.
So let's see the application, the main method here.
So basically we create a new product subject.
So supposedly we have a computer store.
So we create the new computer here.
And as you can see, we initialize the store observer first with this parameter, the computer parameter.
And here we do some changes in the computer state.
So we we call we basically call the center with new strengths, with new states, new sold.
And after that, we need to also employ the supply chain observer.
And we basically created based on the same product.
And then we change the state again to order basically a new product, the product that has been already
sold.
So basically, we need to reorder it just to have it in our store again.
So let's see this in action.
Let's start by running this main method.
So as you can see, the sole observer is updated, so the the first state here is printed then for the
second update, whenever the computer has been sold, the sole observer will print this information,
will print the new state.
And finally, it will take the final stand statement here.
As you can see, also, the supply chain observer will print it state because this one is particularly
interested to trigger an event after the computer has been sold.
So that's a new computer will be ordered.
Again, this is this is it for for this tutorial.
So you may view this this pattern as having one to many relationship between your objects.
So basically, we have a single product and we have multiple observers.
So whenever the product will change the state, the observers will trigger some action.
So actually, it is pretty straightforward, but it would be very useful for you to download this code
from from the video page, from the attachments section, and you can start playing with it so that
you can get a firm grasp on on what this observer pattern is about.
So that's it for this tutorial.
Joining the next one when we are going to discuss about the state paper.