I'm new to design patterns, when I looked at the state class diagram, I found it's just a polymorphism applied. Nothing is special about it. Am I wrong?
-
Nearly all GoF design patterns apply polymorphism. How it is used makes all the difference.jaco0646– jaco06462020-05-09 14:00:39 +00:00Commented May 9, 2020 at 14:00
-
@jaco0646, What I mean is that I see the pattern is pure polymorphism. Nothing more than that.Youssef13– Youssef132020-05-09 14:04:20 +00:00Commented May 9, 2020 at 14:04
-
Please explain how other patterns using polymorphism are not pure.jaco0646– jaco06462020-05-09 14:29:14 +00:00Commented May 9, 2020 at 14:29
-
@jaco0646, Well, let me ask it in a different way. What's the difference between Polymorphism and state design pattern.Youssef13– Youssef132020-05-09 14:59:08 +00:00Commented May 9, 2020 at 14:59
-
The context object implements its behavior in terms of the state object, so it will change its behavior depending on what state (object) it currently has. Since users of the context object know nothing of the internal state object, that gives them one continuing object with state-dependent behavior. Polymorphism alone gives different objects with different behaviors.Brendan E. Coughlan– Brendan E. Coughlan2020-05-11 09:41:43 +00:00Commented May 11, 2020 at 9:41
1 Answer
Polymorphism is a mechanism that can be used to implement the OO modelling known as a State pattern. When using a State pattern, you have one logical object (which might be implemented as a pointer to an implementation) and during its lifetime - you may switch it between implementations based on its current state. Each state tends to work independently, so you can study the implementations independently to understand the behaviour in that state.
You don't have to use polymorphism this way. For example, you might have a logical objects created by a factory based on some configuration read during process startup, and once created the objects retain their runtime type until the process is shutting down and they're destroyed. Or, you could have a base class that represents say a network device, and use an implementation matching your hardware, without having the runtime type change as the hardware performs different tasks - in that case you're also using polymorphism without it being a State pattern, even though one implementation (one derived type) may be tracking the state of the network device using various member variables.
Some objects might not last the entire process lifetime, but still won't change implementations - for example, a simple drawing program might let you pick between different Shape implementations to drop into the diagram, but having picked a Circle - not let you change it into a Square - you'd have to delete the Circle and insert a Square instead. Circle and Square may not be states that Shapes can switch between. In some game, they might be: perhaps a Circle object will transition to a Square object to indicate something about it, such as the current damage level, or the time left until it explodes. So, whether particular implementations are used for a State design pattern or not is not necessarily inherent in the polymorphic derivation relationship - it may be about the way the client code changes the same logical object.
It's not uncommon for polymorphism to be used even when there is no intent to create multiple implementations - that can be used similarly to the pImpl idiom in C++, as what's called a "compilation firewall", mainly to improve build times. Still, some people just like designing their entire system using abstract classes, using polymorphism everywhere whether needed or not.
State patterns tend to be good for things like callback handling for a TCP connection - you can transition between states that just have the behaviours relevant to the responses or connection events that you could get next, which makes for more understandable and maintainable code.