Gang-Of-Four State Pattern

10
State: The Rogue State (Anti)Pattern Things going badly when a stateful class takes up arms against its creator

Transcript of Gang-Of-Four State Pattern

Page 1: Gang-Of-Four State Pattern

State: The Rogue State (Anti)PatternThings going badly when a stateful class takes up arms against its

creator

Page 2: Gang-Of-Four State Pattern

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.

Page 3: Gang-Of-Four State Pattern

Heading off to the code now…

Page 4: Gang-Of-Four State Pattern
Page 5: Gang-Of-Four State Pattern

Advantages of Enum/GoF pattern

They explicitly express important concerns in a clear way.

Page 6: Gang-Of-Four State Pattern

Advantages of the GoF Pattern

Only variables/constants relevant to a state are available in that state

Page 7: Gang-Of-Four State Pattern

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)

Page 8: Gang-Of-Four State Pattern

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

Page 9: Gang-Of-Four State Pattern

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.

Page 10: Gang-Of-Four State Pattern

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)