Comparing Objects in Actionscript [FINAL]
Alright, I’ve posted a few posts in the past about comparing objects in Actionscript and this one will be the last one. This will cover two approaches that I have used when having to compare objects in Actionscript (after having experimented from my other two posts).
OOD Approach
This approach leverages OOD principles. Basically as per my first post on comparing objects, you start out by providing the function valueOf in any class that is to be compared. The valueOf method is part of the Object prototype and is one of the functions that does NOT require the override keyword preceding it when it is defined. The purpose of this method is to basically return the primitive value for an object, whatever that may be is it up to how the class is used. Generally speaking your valueOf method will return a value with type String, Number, int, uint, or Boolean. Now you can use an object of your class type in any logic comparison expression:
Example:
// MyClass.vlaueOf : public function valueOf() : Object { return -1; } // somwhere in code var myObj : MyClass = new MyClass(); trace(myObj < 0); // traces true
However, as per my second post, the equality operator does not work for this approach so to test for equality you will have to perform the following:
var isGTZero : Boolean = myObj > 0; var isLTZero : Boolean = myObj < 0; var isEqualToZero : Boolean = !isLTZero && !isGTZero;
Method Strategy Approach
This other approach does not rely on the OO principles of Actionscript. For this approach you simply define a method like the following:
function compare(objA : MyClass, objB : MyClass) : int
{
if (objA.age objB.age)
return 1;
return 0;
}
However, this approach requires a compare function to be written for each class type you want to compare. You could leverage the valueOf method of objA and objB and compare the objects directly like in the OOD approach. However this could lead into issues further along the road.
Best of Both Worlds Approach
So to leverage both techniques so that we have a robust yet easy to use way to compare our objects I would recommend first defining an interface for comparable objects. The interface’s job is to cause each object that can be compared to define their valueOf method.
IComparable interface:
public interface IComparable { function valueOf() : Object; }
Now with just this interface alone we can guarantee that any object implementing this interface will have a custom valueOf method defined and can be safely used in a logic expression. This is magnitudes safer than just assuming an object’s class defines the valueOf method.
What if in AS4 the valueOf method is deprecated or that the valueOf method no longer works in logic expressions? All your code will be trash and will HAVE to be rewritten. To avoid this, I suggest using a compare interface that compares two IComparable objects.
ICompare interface:
public interface ICompare { function compare(objA : IComparable, objB : IComparable) : int; }
The interface provides just one method, the compare method. The method will return -1 if objA precedes objB, 1 if objA proceeds objB and 0 if objA is equal to objB. Now we are free to implement the comparison anyway we want. We could do the following:
Compare class:
public class Compare implements ICompare { public function compare (objA : IComparable, objB : IComparable) : int { if (objA objB) return 1; return 0; } }
You then can use the ICompare interface as a parameter to a function that relies on comparing objects.
Example:
function getSmallestInList (compare : ICompare) : IComparable;
This is the safest way I can think of comparing objects. Is there a better way? Most likely. There is more than one way of skinning a cat.
This approach worked wonders for my situation and I would assume that it will work for most cases.
Hope this sheds some light on the matter and helps you with your coding endeavors.




Brilliant! Thanks a ton!
Brandon
March 3, 2009
[...] Here, [...]
ActionScript 101: Object comparisons « :maohao:
March 8, 2009