The asdk.condition package that ships with ASDK includes several convenience classes for executing conditional logic. These classes allow developers to encode business rules into reuseable components encapsulated within objects. The two primary interfaces in this package are: asdk.condition.UnaryCondition and asdk.condition.BinaryCondition. The Conditions section of the user guide provides extensive coverage of these APIs and how they can be used within an application; this tutorial will briefly discuss these interfaces along with two commonly used implementations.
The UnaryCondition provides an interface to executing a command on a single operand and returning a boolean value based on the result. ASDK ships with several implementation of the UnaryCondition interface including the asdk.condition.ObjectsEqual class that is used in the example below.
Below is an example of a how to use the EqualsObject implemenation of the UnaryCondition interface.
1 2 3 4 5 6 7 8 9 |
var orange = "orange"; |
In this simple example the unary condition allows for encapsulating the conditional logic of comparing objects. This is surface through a single execute method that determines where the object that is passed into the method is equal to the object passed to the constructor. There are several other implemenations of the UnaryCondition interface that are discussed in the Conditions section of the user guide.
The BinaryCondition interface provides and API for executing an operation on two operands, again returning a boolean value based on the result. The example below demonstrates using this interface with the asdk.condition.GreaterNumber implementation class.
1 2 3 4 5 6 7 8 9 10 |
var greaterNumber : asdk.condition.BinaryCondition = new asdk.condition.GreaterNumber(); |
The code above utilizes the GreaterNumber condition to execute several comparisons among two numbers. Note that in this example that the first operand is always compared against the second operand so that the result is relative to the first operand. In the example, the first operand of 2 is only greater than the second operand in the first case on line 2. The other comparisons return false when 2 is compared to 2 (line 5) and when 2 is compared to 3 (line 8). There are several other implemenations of the BinaryCondition interface that are also discussed in the Conditions section of the user guide.