Proxy design pattern

  • A proxy, in its most general form, is a class functioning as an interface to something else.
  • In short, a proxy is a wrapper or agent object that is being called by the client to access the real serving object behind the scenes.

example-1

See example on https://en.wikipedia.org/wiki/Proxy_pattern

“The idea of the Proxy pattern is that you have an object that stays in front of another object and has the same interface as this object. So, if some operation has to be performed before another operation, you can make use of a proxy, to abstract the caller and simplify its code. For example, we have an object that has a getter method that retrieves a list of Strings, but we have to go to the database to retrieve this list. If there’s a proxy, the caller simply invokes this getter method on the proxy object, and the proxy verifies whether the target object already has the list. If not, it goes to the database, retrieves the list, sets it on the target object and returns to the caller. Therefore, the proxy object can execute code before and after the code of the actual targeted object”.

Credits for the last paragraph:  RobertoPerillo@http://www.coderanch.com/t/637844/patterns/Difference-Proxy-Delegate

example-2

I would say another good example of Proxy pattern in action is in Spring Declarative transaction management when you use @Transactional.
Declarative transaction management is enabled with the support of AOP Proxies.

See this diagram

http://docs.spring.io/spring/docs/2.5.x/reference/transaction.html#transaction-declarative

spring-declarative-tx-aop-proxy

Delegate pattern

The delegation pattern is a design pattern in object-oriented programming where an object, instead of performing one of its stated tasks, delegates that task to an associated helper object. There is an Inversion of Responsibility in which a helper object, known as a delegate, is given the responsibility to execute a task for the delegator.

example-1

See example on https://en.wikipedia.org/wiki/Delegation_pattern

Decorator Pattern

It’s a complex case of Delegate pattern.

The more complex case is a Decorator Pattern that by using interfaces, delegation can be made more flexible and typesafe

example-1

See example on https://en.wikipedia.org/wiki/Delegation_pattern

Other design patterns

Simple Factory,Factory method,Abstract Factory

Design patterns – Simple Factory,Factory method,Abstract Factory