2.8. The Proxy Pattern

The GOF book defines the intent of the Proxy pattern as an object that acts as a placeholder or surrogate for another object to control access to that object.

Proxies come in various types, but as far as Cairngorm is concerned, you are interested in the type known as a remote proxy. A remote proxy is an object that controls access to data from a remote source. An example of such an object might look like the following pseudo-code:

package com.somedomain.somepackage{
  import flash.events.ResultEvent;
  import mx.rpc.http.HTTPService;
  import mx.rpc.AsyncToken;
  public class DataLoader extends EventDispatcher{
    public function DataLoader():void{}
    private function onResult(event:ResultEvent):void{
      dispatchEvent(new DataEvent(Event.COMPLETE, false, false,
      XML(event.target.data)));
    }
    public function loadData():void{
     var service:HTTPService = new HTTPService("someUrl");
     service.addEventListener(ResultEvent.RESULT, this.onResult);
     service.send();
    }
  }
}

A class using this proxy might look something like the following:

package com.somedomain.somepackage{
  import flash.events.ResultEvent;
  import com.somedomain.somepackage.DataLoader;
  public class ProxyExample{
    public function ProxyExample():void{
      var dataLoader:DataLoader = new DataLoader();
      dataLoader.addEventListener(Event.COMPLETE, this,onDataLoaded);
      dataLoader.loadData();
    }
private function onDataLoaded(event:ResultEvent):void{
      trace (event.data);
    }
  }
}

In the preceding example, the DataLoader class stands in for and ...

Get Professional Cairngorm™ now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.