State
The patterns in this chapter describe how to communicate your use of state. Objects are convenient packages of behavior which is presented to the outside world and state which is used to support that behavior. One of the advantages of objects is that they mince all of the state of a program into tiny pieces, each effectively its own little computer. Large libraries of state, promiscuously referenced, make further changes to code difficult because the effect of a code change on the state is hard to predict. With objects, it is easier to analyze what state will be affected by a change, because the namespace of referenceable state is so much smaller.
The chapter contains the following patterns:
- State
Compute with values that change over time. - Access
Maintain flexibility by limiting access to state. - Direct Access
Directly access state inside an object. - Indirect Access
Access state through a method to provide greater flexibility. - Common State
Store the state common to all objects of a class as fields. - Variable State
Store state whose presence differs from instance to instance as a map. - Extrinsic State
Store special-purpose state associated with an object in a map held by the user of that state. - Variable
Variables provide a namespace for accessing state. - Local Variable
Local variables hold state for a single scope. - Field
Fields store state for the life of an object. - Parameter
Parameters communicate state during the activation of a single method. - Collecting Parameter
Pass a parameter to collect complicated results from multiple methods. - Optional Parameter
Pass a parameter or use default parameter’s value. - Var Args
Pass various count of parameters. - Parameter Object
Consolidate frequently used long parameter lists into an object. - Constant
Store state that doesn’t vary as a constant. - Role-Suggesting Name
Name variables after the role they play in a computation. - Declared Type
Declare a general type for variables. - Initialization
Initialize variables declaratively as much as possible.
|
This work is licensed under a Creative Commons Attribution-Noncommercial-No Derivative Works 3.0 Unported License |
