CHAPTER 14

image

State Patterns

GoF Definition: Allow an object to alter its behavior when its internal state changes. The object will appear to change its class.

Concept

The concept is best described by the examples that follow.

Real-Life Example

Consider a network connection for the moment. Here the object (that is responsible for communication) can be in various states (e.g., already a connection is established, a connection is closed, or the object is listening through the connection). We can also think of a traffic signal in this context.

Computer World Example

The above example is applicable in the computer world also. Let us look at an additional ...

Get Java Design Patterns now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.