by andrew

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:

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.

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.