Search Tutorials
Step 9. Insulating your Implementation with Object.addPropertyWe designed the triangle component with good programming practice in mind, namely, defining access methods such as setBase, getBase, etc. for the outside world to use in order to access internal variables tbase, etc. In that way, if we changed how we store the base internally, we would just have to fix getBase and setBase, for example, and we would not break developer's code that used the component. However, Flash does not have a way of enforcing public and internal (private) class areas, such as other object-oriented languages like C++. Unfortunately, that means rogue developers can go in and access and manipulate properties that you really never intended for them to see. The new addProperty method allows you to define a property that, when accessed, calls a function to compute a value, instead of storing the value in a real variable. For example, we can define a property called "base" that uses setBase and getBase to do its setting and reading, respectively. When we tell the outside world to use "base", "height", and other of these properties, we know that they are really calling the functions we have defined and not using the internal variables. Therefore, if we change the internal implementation, we only have to update the access functions and code built with the component will behave properly. Here is the code to add within the #initclip/#endinitclip block (after getBase and setBase have been defined): TriangleClass.prototype.addProperty("base", TriangleClass.prototype.getBase, TriangleClass.prototype.setBase); The addProperty method takes three arguments: the name (in double quotes) of the new property, the method to use in order to access the 'value' of the property, and the method to use to store a new value. The get function must return the value, and the set function must accept one argument, which is the new value. The functions can do whatever computation is necessary to produce the property value. An example of this is the "area" property: TriangleClass.prototype.addProperty("area", TriangleClass.prototype.getArea, null); As you can see from the code, we do not explicitly store the triangle's area, rather, we compute when requested to do so. From the user's perspective, it does not matter because we compute it when the user requests that property. For area of a triangle, there is no way to set its value explicitly, so we pass in null. For completeness, we also define a "height" property (not to be confused with the _height property of the movie clip, which is the y axis extent of the component) TriangleClass.prototype.addProperty("height", TriangleClass.prototype.getHeight, TriangleClass.prototype.setHeight);Suppose a developer creates an instance of your triangle component called "tri0". We tell the developer about the public properties, namely area, base, and height. Now we do not have to worry about potential problems caused by implementation changes in the future. Advanced: Robin Debreuil pointed out (thank you!) that
one catch with this way of defining properties is that it will not work if you
override the get or set methods for a sub-class, because the method will remain
set from TriangleClass. To be the safest possible, if you believe your class
may be sub-classed and the methods overridden, use the following as an example:
TriangleClass.prototype.addProperty("height", function () {
return this.getHeight(); }, function (v) { this.setHeight(v); } );
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
![]() |
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|