1 / 14

State Pattern

State Pattern. Vijay Vujjini. Intent. “Allow an object to alter its behavior when its internal state changes. The object will appear to change its class.” Also known as “Objects for States”. Motivation. Provide transparency and a flexible way to add responsibilities to objects at run time.

mpuente
Télécharger la présentation

State Pattern

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. State Pattern Vijay Vujjini

  2. Intent • “Allow an object to alter its behavior when its internal state changes. The object will appear to change its class.” • Also known as “Objects for States”.

  3. Motivation • Provide transparency and a flexible way to add responsibilities to objects at run time. • Easy technique to manage large number of subclasses.

  4. Motivation Example-1

  5. Motivation Example-2

  6. Applicability • When is this pattern useful? • An object's behavior depends on its state, and it must change its behavior at run-time depending on that state • Operations have large, multipart conditional statements that depend on the object's state.

  7. Structure

  8. Participants • Context: • Defines the interface of interest to clients. • Maintains an instance of a ConcreteState subclass that defines the current state. • State: • Defines an interface for encapsulating the behavior associated with a particular state of the Context. • ConcreteStateSubclasses: • Each subclass implements a behavior associated with a state of the Context

  9. Collaborations • Context delegates state-specific requests to the current ConcereteState object • A context may pass itself as an argument to the state object handling the request. • Context is the primary interface for clients. Clients can configure a context with State objects. • Either Context or the ConcereteSubclass can decide which state succeeds another and under what conditions.

  10. Consequences • Localizes state specific behavior for different states. • Makes the state transition explicit. • State objects can be shared.

  11. Implementation • Who defines the state transition. • A table based alternative: table-lookup approach. • Creating and destroying State objects. • Using dynamic inheritance.

  12. Related Patterns • Flyweight • State and Flyweight patterns can be shared. • Singletons • State objects are often singleton.

  13. Known Uses • TCP Connection Protocols • Graphical Editors

  14. FileManger

More Related