Proxy Pattern in Java
The word proxy reminds me of faking someone and marking there attendance in collage days 😅 . Unless there is a requirement for direct interaction with college officials, I can continue being his proxy (Marking his attendance).
Example 2: When you learn, you create short notes, flash cards etc. You use them to revise during exams, rather than going to through the entire text book(unless and until you can’t recall the topic). In this case, the short notes, act as a proxy for your text book.
Excerpt:
A Proxy can also be defined as a surrogate. In the real world, a cheque or credit card is a proxy for what is in our bank account. It can be used in place of cash, which is what is needed, and provides a means of accessing that cash when required. And that’s exactly what the Proxy pattern does — controls and manage access to the object they are “protecting”.
Proxies have all sorts of uses: remoting, lazy loading, caching etc
Proxy means ‘in place of’, representing’ or the authority to represent someone else, or a figure that can be used to represent the value of something. Proxy design pattern is also called surrogate, handle, and wrapper.
Code Findings:
- Picasso : Here, the image library, caches a compressed version of image on the system, and uses this where ever needed. The real image is fetched from the URL again, only if there is an updation for the image.
- Firebase : Firebase creates a local copy of the database. If the connection is lost in the middle, the library uses the local copy ie the proxy. When the connection is up again, the necessary syncing is performed.
Internet Example:
Say for example, you are using your college internet(Wi-Fi). In order to access your Attendance Portal, you have to enter college-domain URL. Sometimes you need to access things in Google.com as well. Accessing Google.com is far more expensive as it involves creating a secured connection to external network. So unless and until it is really necessary, we use the proxy version of Internet. (Hypothetical example 🐵 )
Project Structure:
Implementation in Java
- Lets create the Proxy Class which will be instantiated when ever required.
IInternet.java
ProxyInternet.java
2. Create the Real Internet, which is expensive but authentic.
RealInternet.java
3. Create a Driver class for testing.
Found it Interesting?
Please show your support by 👏. To read the complete series, click below.
Disclaimer:
I myself, has just started learning, design patterns. If you find any issues please feel free to post them in the comments section below. Thank you for reading so far 😄