Wednesday, October 27, 2010

Why hide object state?

By restricting access to an objects internal state via getter behaviors, we can ensure that all values are valid before assigning them to internal state.

For instance, the Player class has a state called Name. If an external object has direct access to this state, there is the possibility that an incorrect or invalid value may be assigned to it, however, if that state can only be modified via a setter behavior, the behavior implementation (code) can check the value for its validity before assigning the value to the Name state. Eg. Is the name the correct length, type, etc.

No comments:

Post a Comment