State Method
By: Chip Salim
TBD 2017
#2
Types
Defines a family of algorithms, encapsulates each one, and makes them interchangeable. Strategy lets the algorithm vary independently from clients that use it.
Allows an object to alter its behavior when its internal state changes. The object will appear to change its class.
Off
Locked
Unlocked
Display Main Screen
Display Login Screen
Password
Required
?
No
Yes
Password
Valid
?
No
Switch
Switch
Switch
Yes
Off
Locked
Unlocked
Locked
Out
Display Main Screen
Display Login Screen
Password
Required
?
No
Yes
Password
Valid
?
No
3rd
Attempt
?
Yes
No
Switch
Switch
Switch
Yes
The State pattern allows an object to alter its internal state (behavior) via delegation at run-time
Characterized by a long list of conditional statements
The State and Strategy patterns share the same structure, the difference is in the intent.
Result in more numbers of classes in your design
Next Topic by Scott Carroll