O'Reilly logo

Professional Cairngorm™ by Jeremy Wischusen

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

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 ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required