Finding software design patterns in the normal world: decorator and proxy patterns

By | April 9, 2016

This is a continuation of the original “finding software design patterns in the normal world” challenge. For this one, let’s have a look at the proxy and decorator patterns.

The difference between those two seems to be more conceptual than structural.

For example, let’s say we have a soldier who can fire. Let’s put that soldier into a tank.. He can still fire, though he has been turned into a much more powerful weapon now. Is there still a soldier in this picture? Yes, there is. However, what really matters is that there is a tank. They are together, but, when looking at that “firing object” on the battlefield, we see a tank. One can say, our soldier has been improved, yet he was not really changed. In a somewhat weird sense, the soldier has been decorated. He is hidden from us by the tank, and, because of that tank, he is capable of delivering much more fire power than he could on his own.

Decorator pattern is all about giving other objects improved characteristics without really changing the essence of what we use those objects for.  A soldier alone can deliver that much firepower. When put into a tank, he can be turned into a much more powerful weapon.

What about the proxy, though? There is still a tank, and there is a soldier. But we move our attention away from what the soldier can do, and, instead, we now concentrate on how we access the soldier. Let’s say that tank was firing on us, so it would not be our tank, really.. so, our goal might be to somehow get through tank’s armor all the way to the soldier who is controlling the tank. That’s, though, one of the reasons  there are proxies. They can control access to those objects we really care about, and, in particular, proxies can protect those other objects.

So, proxy and decorator are two patterns with very similar implementation, though there are quite different intentions. In case with the proxy, we need to control access to the object. In case with the decorator, we need to give our object some extra/different power without actually modifying the object. There is still a tank and a soldier in both cases. It just that we are concerned about different attributes of that tank depending on whether we are using it as a proxy or as a decorator.

 

References:

Proxy Pattern

Decorator Pattern

Leave a Reply

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