22 State

THE STATE OF an object is a combination of the current values of its attributes. When you call an object’s set- method or assign a value to one of the object’s fields, you are changing the object’s state. Objects also commonly change their own state as their methods execute.

We sometimes use the word state to refer to a single, changing attribute of an object. For example, we may say that the state of a machine is up or down. In such a case, the changeable part of an object’s state may be the most prominent aspect of its behavior. As a result, logic that depends on the object’s state may spread through many of the class’s methods. Similar or identical logic may appear many times, creating a maintenance burden.

One way to counter ...

Get Design Patterns in Java™, Second Edition now with the O’Reilly learning platform.

O’Reilly members experience live online training, plus books, videos, and digital content from nearly 200 publishers.