Showing posts with label Accessors. Show all posts
Showing posts with label Accessors. Show all posts

Wednesday, October 27, 2010

Accessors: Get

Classes may contain accessor behaviors for some or all of their states. This enables other objects to read the value of a state. An accessor behavior is nearly always implemented as a function, as a function returns a value to the calling procedure.

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

In a class diagram, the format for a behavior that returns a value(function) looks like this:
Visibility FunctionName(ParametersIfAny):ReturnDataType

...as can be seen in the Player class example:
+GetName():String

The implementation (code) for an accessor, reads and returns the value of the state being accessed. In other words, the GetName() function will return a String (containing the value of the Name state) to the calling procedure.

An accessor behavior has visibility of Public.

Accessors & Mutators

Below you will find a our class diagram - notice there are a number of behaviors. These behaviors are known as accessors and mutators and will allow external objects to access (read), and in some cases, change the value of an objects state. Each of these behaviors is preceded by a plus sign. This denotes that the behavior is public and can be accessed by other objects directly.