Accessors and Mutators

mutator thumb**Warning: The following post is about Object Oriented Design, and may be VERY annoying for those who don’t spend a STUPID amount of time in front of the computer reading the driest material ever known to mankind. **

For those into OOD/OOP, please read on… This is my attempt to make lucid some very spiraling concepts. I’m no expert, I’m a budding programmer, so as such I may be able to present these ideas with a new perception. At any rate… getting them down on paper solidifies the methods in my mind. That’s why I’m sparse on code, and big on concept. So if I get anything wrong, feel free to send evocative comments, I love the sweet vigor of a verbal joust.

Note: All OOD jargon has been italicized and colored for no good reason at all.

Summary

Getter and setter methods in ActionScript3 differ from regular methods, and resemble characteristics of objects. They do not use regular call statement form, but use dot notation (as a variable would). To use them, you simply call an instantiated object through its variable name and append the getter or setter method minus the function call operator. It’s a prety nice and succinct way to modify states of an object.

Defining The Class

Let’s assume we have a class called “humanEntity()”, and in this class we declare a private instance variable called “lbs” describing its size. Through the constructor method, we pass a parameter called “mass” without an initializer so it is required in the instantiation of the object. In the constructor we assign the value of “mass” to the class’s instance variable “lbs” with an assignment operator.

Great, now all is set to define our getter and setter methods. (These are also loosely known as mutators, accessors, modifiers and retrievers…. what a roguish set of descriptors). Good getters and setters have identical method names and are defined not as actions (as a normal method might be) but as characteristics. So instead of using “changeWeight” or “returnWeight” we would use one variable called “weight”. Through these methods, we can change and return the private variable “lbs” to a main program that uses the “humanEntity()” object. By definition, a getter method explicitly returns a value… in this case “lbs”, and a setter method alters that value. Setters automatically return values, but it is illegal to use a return statement in a setter method.

They are defined like this.

function get weight():Number{
return lbs;
}
function set weight(newLbs:Number):void{
lbs = newLbs;
}

Note that the method name is exactly the same in both. It is the keywords “get” and “set” that differentiate them.

Recap

So to recap, we have a class named “humanEntity()” with a private instance variable named “lbs”. “lbs” is assigned through the constructor method with a required parameter called “mass”. There is one getter method and one setter method, both named “weight”.

Just as an aside, you might want to create another method in this class that would set sensible default values to “mass” if someone tried to pass a value into the object that was too high or low. You would use a conditional statement of some sort. (I guess that’s for another discussion).

Instantiating the Object

Alright, so now we are using the “humanEntity()” class in our application, and we will assign the object to a variable called “conradVonDuchance”. Based on everything I have written thus far, it is instantiated into our main class (or whatever class) thusly.

private var conradVonDuchance:humanEntity
= new humanEntity(200);

The Crux

Sweet, now for the crux of this entire article.

What makes getter and setter methods so cool, is that you can now both reset and return “lbs” (the private variable) by using the method “weight”.

Here are two Examples:
Setting a dynamic style variable to the “weight” getter of our object:

var conradSize:Number = conradVonDuchance.weight;
return conradSize;

To reset “weight”:

conradVonDuchance.weight = 220;

Why this is good

At first glance this might seem like a ton of work to accomplish, but I feel it vastly improves your class’s API.

The idea of setting and returning a characteristic of an object should be as intuitive as possible, and having to call two different methods with different names and function call operators, to accomplish this is a little cumbersome and ugly. Using the methods “getWeight()” and “setWeight()”, do makes sense but do not feel streamlined and sexy.

And after all… isn’t that what programming is all about… making the most streamlined sexy block of code possible so you can appeal to the opposite (or same) sex.

Leave a Reply