Mutable Properties in ActionScript
ActionScript has a really nice implementation of properties, such that you can add get/set filters to a property without chaging its access profile. This is worthwhile to note because it solves a problem that exists in OOP in general. Because traditionally the access of a scalar property and a functional property has been different -- for instance, in PHP:
class Radio {
private $_volume = 0;
// int 0..100
public function getVolume(){ return $this->_volume; }
public function setVolume($v){ $this->_volume = max(0, min(100, (int) $v)); }
public $channel = 0.0;
public $band = 'FM';
public function __toString(){ return 'Radio : channel = ' . $this->channel . ', band = ' . $this->band . ': volume = ' . $this->getVolume() };
}
$r = new Radio();
$r->setVolume(40);
$r->channel = 88.5;
$r->band = 'FM';
echo 'I made a radio! ', $r;
in the above example, Radio has three properties: volume, channel and band. The first property is channeled through get/set accessors. The last two are scalar, with no control put on accesss or assigned value.
If you troll along with this class and then later decide that you want to control access to band (say to ensure it is either 'FM' or 'AM' you either have to rewrite it in the functional access as was done with Volume -- and change EVERY reference to the property across your codebase -- or use the __get __set magic methods -- which work, but force you to gang all your functionality in a case branch, which really isn't elegant.
In ActionScript 3,you don't have this issue because they have a formal way to override scalar (or at least direct) access to a property with a functional profile without changing the access profile.
If you declared the above class in actionscript it would look like this:
class Radio {
private var _volume: Number; // scalar type hinting -- neat!
public function get volume(){ return _volume; }
public function set volume(v: Number) { _volume = v; }
public var channel: Number = 0.0;
public var band: String = 'FM';
public function toString(){
return 'Radio : channel = ' + channel + ', band = ' + band + ': volume = ' + volume;
}
}
var r = new Radio();
r.volume = 40;
r.channel= 88.5;
r.band = 'FM';
trace('I made a radio! ' + r);
note how the access to volume is identical to the other two properties. because the get and set modifiers are actually formal parts of the ActionScript syntax, they are "Magic" methods that create a virtual property that delegates to functions without changin the API of the class.
The value comes in that if you wanted to change band to a formal property,
class Radio { ...
private var _band: String;
public function get band () : String { return _band;}
public function set band(s: String) { _band = s; }
..
}
// the accessing code does not change
var r = new Radio();
r.volume = 40;
r.channel= 88.5;
r.band = 'FM';
trace('I made a radio! ' + r);you don't have to worry about any side effects in any of the code that accessed the version of the class that existed before you formalized the property. This means that you can use simple scalar properties for economy of development without painting yourself into a corner, because you can formalize property access later, if it becomes necessary. (or, I suppose, simplify a formal property to scalar access if it becomes obvious that formal notation isn't adding value. )

Post new comment