State Pattern from GoF
8
State Pattern From GoF Chris Bushell 7 th July 2010
-
Upload
melbournepatterns -
Category
Documents
-
view
1.839 -
download
1
description
State Pattern from Gof, by Chris Bushell, x
Transcript of State Pattern from GoF
![Page 1: State Pattern from GoF](https://reader036.fdocuments.in/reader036/viewer/2022082603/546b2228af7959221d8b6d43/html5/thumbnails/1.jpg)
State Pattern From GoF
Chris Bushell 7th July 2010
![Page 2: State Pattern from GoF](https://reader036.fdocuments.in/reader036/viewer/2022082603/546b2228af7959221d8b6d43/html5/thumbnails/2.jpg)
Motivation
“Allow an object to alter its behavior when its internal state changes”
![Page 3: State Pattern from GoF](https://reader036.fdocuments.in/reader036/viewer/2022082603/546b2228af7959221d8b6d43/html5/thumbnails/3.jpg)
Code
![Page 4: State Pattern from GoF](https://reader036.fdocuments.in/reader036/viewer/2022082603/546b2228af7959221d8b6d43/html5/thumbnails/4.jpg)
Applicability
• Behavior depends on state• Operations have large conditional statements
which depend on state
![Page 5: State Pattern from GoF](https://reader036.fdocuments.in/reader036/viewer/2022082603/546b2228af7959221d8b6d43/html5/thumbnails/5.jpg)
Participants
![Page 6: State Pattern from GoF](https://reader036.fdocuments.in/reader036/viewer/2022082603/546b2228af7959221d8b6d43/html5/thumbnails/6.jpg)
Collaborations
• Context and Concrete State• Context is primary interface for clients
![Page 7: State Pattern from GoF](https://reader036.fdocuments.in/reader036/viewer/2022082603/546b2228af7959221d8b6d43/html5/thumbnails/7.jpg)
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](https://reader036.fdocuments.in/reader036/viewer/2022082603/546b2228af7959221d8b6d43/html5/thumbnails/8.jpg)
Implementation
• Context or Concrete State defines transitions?• Table based approach to defining transitions• Singleton, Flyweight