Skip to content Skip to navigation

OpenStax_CNX

You are here: Home » Content » State Design Pattern

Navigation

Lenses

What is a lens?

Definition of a lens

Lenses

A lens is a custom view of the content in the repository. You can think of it as a fancy kind of list that will let you see content through the eyes of organizations and people you trust.

What is in a lens?

Lens makers point to materials (modules and collections), creating a guide that includes their own comments and descriptive tags about the content.

Who can create a lens?

Any individual member, a community, or a respected organization.

What are tags? tag icon

Tags are descriptors added by lens makers to help label content, attaching a vocabulary that is meaningful in the context of the lens.

This content is ...

In these lenses

  • Design Patterns display tagshide tags

    This module is included inLens: Design Patterns
    By: Stephen Wong

    Comments:

    "Allows an object to dynamically change its behaviors as per different states of being."

    Click the "Design Patterns" link to see all content selected in this lens.

    Click the tag icon tag icon to display tags associated with this content.

Recently Viewed

This feature requires Javascript to be enabled.

Tags

(What is a tag?)

These tags come from the endorsement, affiliation, and other lenses that include this content.
 

State Design Pattern

Module by: Stephen Wong, Dung Nguyen. E-mail the authors

Summary: The State Design Pattern models objects that changes state, i.e. change behavior as a result of what has happened to them. This is also called "dynamic reclassification".

Objects are often discussed in terms of having a "state" that describes their exact conditions in a given time, based upon the values of their properties. The particular values of the properties affect the object's behavior. For instance, one can say that the exact behavior of an object's getColor() method is different if the "color" property of the given object is set to "blue" instead of "red" because getColor() returns a different value in the two situations.

Furthermore, the object may make decisions at run time as to exactly what to do dependent upon the values its properties possess. For instance, if the sky is blue (sky.setColor(Color.blue)), then the sun should be visible.


public boolean sunIsVisible() {
    if(getColor()==Color.blue) {
        return true;
    }
    else {
        return false;
    }
}

One issue with the above solution is that it is a hard-coded logic solution, not an architected solution. The sky does not intrinsically behave a certain way if it is blue, but rather it should figure out what to do in that situation.

Wouldn't it be better if the sky intrinsically acted properly if it were blue? One could imagine two objects: a SkyBlue and a SkyNonBlue. The SkyBlue class' sunIsVisible() method would always return true while the SkyNonBlue version would always return false.

What one needs now is the ability for a sky object to dynamically (i.e. at run time) change its class to/from SkyBlue and SkyNonBlue. What we'd like to accomplish is called "dynamic reclassification".

We've seen code that does change its specific behavior depending on what particular strategy was installed. So, the setColor() method could install a strategy that would always return true if its sunIsVisible() method were to be called.

Exercise 1

But does the user of the Sky class care about the stratregy?

Solution

Of course not. The user only cares that it does its job.

The State Design Pattern is a fully encapsulated, self-modifying Strategy Design Pattern.

Figure 1: UML Class Diagram of the State Design Pattern
Figure 1 (graphics1.png)

One design pattern that is used very often in conjunction with the state pattern is the Null Object Pattern.

Notice these things about the pattern:

  1. Any methods whose behaviors depend on the state of the object are simply delegated on in to the state, and handled there. Thus you will see the same methods in the context as in the states. Since the states are separate objects from the context, all the properties of the context need to have accessor methods that are at least package visible.
  2. The "Context" object needs to add a "set" accessor method so the states can modify which state is the active state. This method would be package visible so as to encapsulate the behavior away from the sight of the user.

Content actions

Download module as:

PDF | EPUB (?)

What is an EPUB file?

EPUB is an electronic book format that can be read on a variety of mobile devices.

Downloading to a reading device

For detailed instructions on how to download this content's EPUB to your specific device, click the "(?)" link.

| More downloads ...

Add module to:

My Favorites (?)

'My Favorites' is a special kind of lens which you can use to bookmark modules and collections. 'My Favorites' can only be seen by you, and collections saved in 'My Favorites' can remember the last module you were on. You need an account to use 'My Favorites'.

| A lens I own (?)

Definition of a lens

Lenses

A lens is a custom view of the content in the repository. You can think of it as a fancy kind of list that will let you see content through the eyes of organizations and people you trust.

What is in a lens?

Lens makers point to materials (modules and collections), creating a guide that includes their own comments and descriptive tags about the content.

Who can create a lens?

Any individual member, a community, or a respected organization.

What are tags? tag icon

Tags are descriptors added by lens makers to help label content, attaching a vocabulary that is meaningful in the context of the lens.

| External bookmarks