Step 9. Insulating your Implementation with Object.addProperty
We 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.
TriangleClass.prototype.addProperty("height", function () {
return this.getHeight(); }, function (v) { this.setHeight(v); } );
| » Level Intermediate |
|
Added: 2002-04-16 Rating: 8 Votes: 55 |
| » Author |
| Jonathan Kaye, PhD, is the President and CTO of Amethyst Research LLC, an award-winning interactive design and engineering firm specializing in the creation of online device simulations. He and David Castillo are the authors of "Flash for Interactive Simulation: How to Construct and Use Device Simulations", to be published by Delmar Thomson Learning in November, 2002 (the accompanying web site will be www.FlashSim.com). |
| » Download |
| Download the files used in this tutorial. |
| Download (0 kb) |
| » Forums |
| More help? Search our boards for quick answers! |
-
You must have javascript enabled in order to post comments.


Comments
There are no comments yet. Be the first to comment!