In this section of the course we're going to take a look at the state design pattern and so you might
be wondering Well hold on every object or most objects have stayed in their fields and so why are we
even discussing it.
Why is this a separate design pattern.
Well in the representation of the Gang of Four the state design pattern basically suggests that the
state of the system kind of controls the way that it operates and it also ties to this idea of state
machines or finite state machines.
So what is the motivation behind using the state design power.
Well if you consider an ordinary telephone for example then what you can do with a telephone typically
depends on the current state of the telephone as well as the phone line.
So for example if the phone is ringing or if you want to make a call you need to pick up the phone.
That's a requirement to be able to actually respond to somebody or to actually make a call somewhere.
Now the phone must be off the hook to actually talk to somebody or make a call.
You cannot be talking to them while the phone is still hanging on the hook and ringing.
Now if you try calling someone and the line is busy then you put the handset down.
That's atypical operation of the phone.
Now changes in the state of the system so in our case that would be the phone changes in the state can
be explicit so we can actually sort of specify Oh by the way we're doing this change or these changes
can be in response to any event that's the go Observer pattern.
So it's really up to you how your state machine operator or some state machines would have explicit
control.
So for example you query the user what they want to do and thereby they transition from one state to
another and other systems would run state machines based on events happening externally.
So for example since I'm in algo trading what I typically have is I connect to a stock market and I
listen to events which come from the stock market so that is the observer pattern.
And then I have a state machine which moves me from the connecting state to the connected state to the
state where I got some initial market data and so on and so forth.
So the state design pattern is quite simply a pattern where the object's behavior is determined by its
state.
So you have some state and it doesn't have to be a single field or anything it can be quite expansive.
And then the behavior which is possible which is performed by the object depends on that state.
Now the there is also this paradigm of an object transitioning from one state to another.
So for example the phone can move from the off hook stage to let's say dialing stage for example and
this happens to be called a transition so you transition from one state to another and sometimes these
transitions are triggered by some events or we have this idea of a trigger as an event which causes
a transition from one state to another.
Now we have this formalized construct which manages state and transitions from one state to another
and that formalized construct is called a state machine and once again this is typically a class.
It can be a class that you yourself build or a class take in from the extent of library.
And we're actually going to look at both of these examples.