I've been utilizing the command pattern in my Flex projects, with asynchronous callback routes required between:
- whoever instantiated a given command object and the command object,
- the command object and the "data access" object (i.e. someone who handles the remote procedure calls over the network to the servers) that the command object calls.
Each of these two callback routes has to be able to be a one-to-one relationship. This is due to the fact that I might have several instances of a given command class running the exact same job at the same time but with slightly different parameters, and I don't want their callbacks getting mixed up. Using events, the default way of handling asynchronicity in AS3, is thus pretty much out since they're inherently based on one-to-many relationships.
Currently I have done this using callback function references with specific kinds of signatures, but I was wondering if someone knew of a better (or an alternative) way?
Here's an example to illustrate my current method:
- I might have a view object that spawns a
DeleteObjectCommand
instance due to some user action, passing references to two of its own private member functions (one for success, one for failure: let's say"deleteObjectSuccessHandler()"
and"deleteObjectFailureHandler()"
in this example) as callback function references to the command class's constructor. - Then the command object would repeat this pattern with its connection to the "data access" object.
- When the RPC over the network has successfully been completed (or has failed), the appropriate callback functions are called, first by the "data access" object and then the command object, so that finally the view object that instantiated the operation in the first place gets notified by having its
deleteObjectSuccessHandler()
ordeleteObjectFailureHandler()
called.