Post on 19-Mar-2017
State: The Rogue State (Anti)PatternThings going badly when a stateful class takes up arms against its
creator
Implement a counter
1. Count to a specified maximum value, then stop.2. Do not start counting until an initialized signal is received.3. If a reset signal is received, go back to uninitialized.4. If a clear signal is received, set the count back to 0, but only if
counting is not in progress.5. The maximum value can be changed, as long as the counter is not
already counting.
Heading off to the code now…
Advantages of Enum/GoF pattern
They explicitly express important concerns in a clear way.
Advantages of the GoF Pattern
Only variables/constants relevant to a state are available in that state
Advantages of the GoF Pattern
Easier to reason about, because determining what state the system is in, has been separated from what to do in response to various signals/messages.Specifically, class structure has replaced conditional logic.
(Single Responsibility Principle)
Advantages of the GoF Pattern
Changes largely result in adding classes/methods, as opposed to editing existing methods.
Open [for extension]/Closed [for modification] Principle
Advantages of the GoF Pattern
Code is about communication with other developers (as a fringe benefit users get to do stuff).The state pattern is easier to understand.
Addendum to last week:Program Architecture and SRP
Data Object(the business domain data)
Business Methods(only operate on data
objects, NO dependency injection)
Composing Dependencies(Manager/Handler/BL
layer)