The Proxy Design Pattern in Java Tutorial Example

Using Standard UML, Junit, Hamcrest and Test Driven Development (TDD), I will walk you through several aspects of the Proxy Pattern.

Working code example:
https://github.com/mickknutson/patterns

There are many different flavors of Proxy, depending on it’s purpose. You may have a protection proxy, to control access rights to an object. A virtual proxy handles the case where an object might be expensive to create, and a remote proxy controls access to a remote object.

You’ll have noticed that this is very similar to the Adapter pattern. However, the main difference between bot is that the adapter will expose a different interface to allow interoperability. The Proxy exposes the same interface, but gets in the way to save processing time or memory.

Would I Use This Pattern?

This pattern is recommended when either of the following scenarios occur in your application:

* The object being represented is external to the system.
* Objects need to be created on demand.
Access control for the original object is required
Added functionality is required when an object is accessed.

Typically, you’ll want to use a proxy when communication with a third party is an expensive operation, perhaps over a network. The proxy would allow you to hold your data until you are ready to commit, and can limit the amount of times that the communication is called.

The proxy is also useful if you want to decouple actual implementation code from the access to a particular library. Proxy is also useful for access to large files, or graphics. By using a proxy, you can delay loading the resource until you really need the data inside. Without the concept of proxies, an application could be slow, and appear non-responsive.

https://en.wikipedia.org/wiki/Proxy_pattern

GitHub: https://github.com/mickknutson | Linked IN: https://linkedin.com/in/mickknutson | Twitter: https://twitter.com/mickknutson | Twitter: https://twitter.com/baselogic | FaceBook: https://business.facebook.com/baselogic

@BASELogic

Mick Knutson

Java, JavaEE, J2EE, WebLogic, WebSphere, JBoss, Tomcat, Oracle, Spring, Maven, Architecture, Design, Mentoring, Instructor and Agile Consulting. http://www.baselogic.com/blog/resume

View all posts

Java / JavaEE / Spring Boot Channel

BLiNC Supporters

BLiNC Adsense

Archives

Newsletter