Let's take a look at the context object here so we can see the product object with reference to the
product state, which is the interface that will serve as a contract for the state implementation classes.
We can see that inside the constructor we assign the first state as the available state, which is for
sale.
Right.
This would be the first the initial state of of a product inside the store.
And we also have the letters and the setters because we encapsulate this property in a private variable.
Here we are.
Of course, we have some methods for getting to the previous state or to the next state.
And we also have a method to print the current state, which is the one that you see here, which will
have a statement, a system out of control and statement which will display the current state of the
product.
Plus, this is the interface for for the states.
So this will be the contract.
We we have only two methods.
The next and the previous.
Both of them will take a product as an argument.
So let's see the first state here, the available state, so you can see that the next method here will
switch, will set the new state of the product, which will be the product from from its arguments to
the solid state, which is the next one.
So basically, we have two states, this one being the first and the solid state being the second.
So the next method will switch the state to the next one to to the last state, basically.
And the previous state won't do anything, will just output a system.
Although the saying that the previous state, there is no previous state for this, this product, which
is of course understandable, and the souled will have the same problem for the next because we don't
have a next state and for the previous it will switch the previous state.
It was the product to its previous state, which is the available state.
So let's see how this works.
So basically, reinitialize here a new product and we here put some system of different elements to.
To bring some clarity to to this to this example, and here we switch the states to the next step,
we print the new status and we also do another action that is supposed to bring the product to its previous
state.
And then we print the status again, the status, meaning the current state.
So let's run this.
So as you can see, when the smartphone has been sold, which is our product, the product that we use
in our example, the status for the state will be equal to salt.
But when the smartphone will be returned by the customer, the status will switch again to the available
state.
So as you can see, we do not make use of complex case statements or if else, statements.
We just delegate the state of the product to a different class, to a different set of classes, actually,
which will handle all the implementation for that state and all the actions that we need to do in that
specific state for us.
Right.
So we won't need to to create here a lot of code, a lot of complexity, because this will eventually
make the code unreadable and very, very hard to refactor.
All right, so that's it for this party.
Join me in the next tutorial where we are going to discuss about the strategy of putting.