«

»

Jan 31

Print this Post

Design patterns: proxy

New 2014 year did not start easy for me in terms of describing new design pattern I have learnt by reading the book. The cause of it is next chapter I have read more than a month ago. It describes new proxy pattern but it describes it mostly in Java environment. However, it does not mean we use the pattern only in Java applications.

Book example

The history of cooperation with Mighty Gumball, Inc. continues. The CEO asks us for a better monitoring for the gumball machines. At first we add location property to GumballMachine class, assign a value to it via the constructor and added two getters: location getter and state getter. After these changes in existing class we create GumballMonitor class which is rather simple:

#
class GumballMonitor {
   private $machine;

   public function __construct( GumballMachine $machine ) {
      $this->machine = $machine;
   }

   public function report() {
      $r = '';
      $r .= "Gumball Machine:\t" . $this->machine->getLocation() . "\n";
      $r .= "Current inventory:\t" . $this->machine->getCount() . " gumballs\n";
      $r .= "Current state:\t\t" . $this->machine->getState() . "\n";
      echo $r;
   }
}

After few changes in app.php file the results are as follow:

$ php app.php 
Gumball Machine:        Seatle
Current inventory:      10 gumballs
Current state:          Machnine is waiting for quarter.

But once we show it to the CEO we learn the output is alright but he was not clear enough with us. He wants a remotely check the status of different machines (we are supposed to be Internet generation!). That changes a lot. However, not as much as we might think at the beginning. One of developers comes up with an idea of proxy and using Java RMI mechanism (java.rmi.* package). On next pages of the chapter you can read about it more. Basically, I do not think you need similar architecture in the world of web development and PHP ;) When I read it I thought that instead of RMI Stubs and RMI Skeletons we can just use HTTP protocols. Therefore, I will not describe the Java implementation here — it is in the book if you are interested :) Let’s jump to the proxy pattern definition:

The Proxy Pattern provides surrogate or placeholder for another object to control access to it.

Proxy pattern's diagram.

Proxy pattern’s diagram.

Later on we learn different kinds of proxy patterns:

  • remote proxy — manages interaction between a client and a remote object,
  • virtual proxy — controls access to an object that is expensive to instantiate,
  • firewall proxy — controls access to a set of network resources, protecting the subject from “bad” clients,
  • smart reference proxy — provides additional actions whenever a subject is referenced, such as counting the number of references to an object,
  • caching proxy — temporary storage for results of expensive operations; it can also allow multiple clients to share the results to reduce computation or network latency,
  • synchronization proxy — provides a safe access to a subject from multiple threads,
  • complexity hiding proxy — hides the complexity of and controls the access to a complex set of classes (this is sometimes called the Facade Proxy for obvious reasons); the complexity hiding proxy differs from facade pattern in that the proxy controls access, while the facade pattern just provides an alternative interface,
  • copy-on-write proxy — controls the copying of an object by deferring the copying of an object until it is required by a client; this is variant of virtual proxy.

Other examples

I read a lot on the Internat about proxy patterns used in Doctrine. A PHP object-relation mapping library. Once I read the pattern name it recalled me jQuery $.proxy() method we use a lot in our Wikia codebase. However, it is the same idea but set in the closoures’ world ;)

Other design patterns

Here is the list of described by me design patterns:

Permanent link to this article: http://blog.lukaszewski.it/2014/01/31/design-patterns-proxy/

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>