State Pattern from GoF

8
State Pattern From GoF Chris Bushell 7 th July 2010

description

State Pattern from Gof, by Chris Bushell, x

Transcript of State Pattern from GoF

Page 1: State Pattern from GoF

State Pattern From GoF

Chris Bushell 7th July 2010

Page 2: State Pattern from GoF

Motivation

“Allow an object to alter its behavior when its internal state changes”

Page 3: State Pattern from GoF

Code

Page 4: State Pattern from GoF

Applicability

• Behavior depends on state• Operations have large conditional statements

which depend on state

Page 5: State Pattern from GoF

Participants

Page 6: State Pattern from GoF

Collaborations

• Context and Concrete State• Context is primary interface for clients

Page 7: State Pattern from GoF

Consequences

• Localizes state specific behavior. Avoids code duplication

• State changes are well defined, atomic operations

• State objects can be shared

Page 8: State Pattern from GoF

Implementation

• Context or Concrete State defines transitions?• Table based approach to defining transitions• Singleton, Flyweight