1. State: The Rogue State
(Anti)Pattern
Things going badly when a stateful class takes up arms against its creator
2. 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.
5. Advantages of Enum/GoF pattern
They explicitly express important concerns in a clear way.
6. Advantages of the GoF Pattern
Only variables/constants relevant to a state are available
in that state
7. 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)
8. 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
9. 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.
10. 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)