The State Design Pattern

13
The State Design Pattern A behavioral design pattern. Shivraj Persaud E-mail: [email protected]

description

The State Design Pattern. A behavioral design pattern. Shivraj Persaud E-mail: [email protected]. State Design Pattern. Encapsulate states as concrete objects. Abstract them into an abstract class. - PowerPoint PPT Presentation

Transcript of The State Design Pattern

Page 1: The State Design Pattern

The State Design Pattern

A behavioral design pattern.

Shivraj PersaudE-mail: [email protected]

Page 2: The State Design Pattern

State Design Pattern

Encapsulate states as concrete objects.

Abstract them into an abstract class. The system contains a reference to an

abstract state class, which can be any of the concrete state classes dynamically.

Page 3: The State Design Pattern

Example – TCPConnection class

A TCPConnection object can be in one of several different states, established, listening or closed.

The behavior of TCPConnection is different in each of these three states.

Page 4: The State Design Pattern

Abstract state class

TCPConnection TCPState

Open()

Close()

Acknowledge()

Open()

Close()

Acknowledge()

state->Open() TCPEstablished

Open()

Close()

Acknowledge()

TCPListen TCPClosed

Open()

Close()

Acknowledge()

Open()

Close()

Acknowledge()

Page 5: The State Design Pattern

TCPConnection maintains a state object which is a subclass of TCPState.

When the connection changes state the TCPConnection object changes the state object it uses.

Page 6: The State Design Pattern

Applicatiblity- when to use the state design pattern

An object’s behavior depends on its state and changes its behavior at run-time.

Operations have large, multipart conditional statements that depend on the object’s state. The State pattern puts each branch of the conditional in a separate class.

Page 7: The State Design Pattern

UML Diagram of the State Design Pattern

Page 8: The State Design Pattern

Participants

Context (TCPConnection)- defines the interface of interest to clients.- maintains an instance of a ConcreteState subclass that defines the current state.

Page 9: The State Design Pattern

State (TCPState)- defines an interface for encapsulating the behavior associated with a particular state of the Context.

ConreteState Subclasses (TCPEstablished, TCPListen, TCPClosed)- each subclass implements a behavior associated with a state of the Context.

Page 10: The State Design Pattern

Collaborations

Context delegates state-specific requests to the current ConcreteState object.

A context may pass itself as an argument to the state object handling the request. This lets the state object access the context if necessary.

Page 11: The State Design Pattern

Context is the primary interface for clients. Clients can configure a context with State objects. Once a context is configured, its clients don’t have to deal with the State objects directly.

Either Context or the ConcreteState subclasses can decide which state succeeds another and under what circumstances.

Page 12: The State Design Pattern

Consequences

It localizes state-specific behavior and partitions behavior for different states.

It makes state transitions explicit.

State objects can be shared.

Page 13: The State Design Pattern

Related patterns

Flyweight