Wednesday, October 27, 2010

Mutators: Set

Classes may contain mutator behaviors for some or all of their states. This enables other objects to change the value of a state. A mutator behavior is usually implemented as a subroutine, unless there is a requirement for a return message - such as a confirmation that the change took place, in which case you would use a function (which could return a value, such as True or False).

A setter subroutine requires a parameter which represents the new value of the state.

For example, the Player class now contains a number of mutator behaviors. Following convention, the names of these behaviors begin with the word Set.

In a class diagram, the format for a setter behavior(subroutine) looks like this:

Visibility SubroutineName(Parameters: DataType)

...as can be seen in the Player class example:

+SetName(NewName: String)

The implementation (code) for a mutator, reads the parameter that is passed to it and updates the value of the state with the value of the parameter. In other words, the SetName() function will read the String parameter passed to it and then change the value of the Name state.

An mutator behavior has visibility of Public.

No comments:

Post a Comment