Purpose

My intended purpose for the AbstractDelegate class in Cairngorm was two-fold:

  1. Reduce the number of files needed to manage business events by organizing Delegates into domains
  2. Make each domain Delegate class as lightweight as possible

Related Posts

Abstract Commands | Abstract Events

Concept

Abstract Delegate

Lightweight Delegate

By extending the AbstractDelegate class, each domain Delegate created by a developer become absurdly lightweight and simple. Basically, it become a series of methods mapped to remote procedure call names.

ProjectDelegate.as

package business.delegates
{
   public class ProjectDelegate extends AbstractDelegate
   {
      public function loadProjects():void
      {
         send("getProjects", {});
      }

      public function deactivate(eventData:Object):void
      {
         send("deactivateProject", eventData);
      }

      public function createProject(eventData:Object):void
      {
         send("createProject", eventData);
      }
   }
}

AbstractDelegate Class

The the AbstractDelete class will take care of holding the reference to the application’s data model, and constructing the final RPC with the designated Responder attached.

AbstractDelegate.as

package business.delegates
{
   import business.RemoteFactoryLocator;
   import flash.utils.getQualifiedClassName;
   import mx.rpc.IResponder;

   public class AbstractDelegate
   {
      private var _responder:IResponder;
      private var _remoteService:RemoteFactoryLocator = RemoteFactoryLocator.getInstance();

      public function AbstractDelegate()
      {
         if(flash.utils.getQualifiedClassName(this) == "business.delegates::AbstractDelegate")
         {
            throw new Error("ClassInstantiationException: Cannot create an instance of an abstract class.");
         }
      }

      public function setResponder(responder:IResponder):void
      {
         _responder = responder;
      }

      public function send(eventName:String, eventData:Object):void
      {
         _remoteService.send(eventName, eventData, _responder);
      }
   }
}

So now all of my delegates extend the AbstractDelegate which allows us to write less code for all of these stinking delegates for our application.

The only ramification is that the Commands (see Abstract Command post for example) now have to call the setResponder() method on each delegate.