Sunday 28 January 2018

Design Patterns | Proxy Pattern

"It provides the control for accessing the original object".

Proxy pattern is to provide a Surrogate or Placeholder for an object to control its references. It is used when we want to provide controlled access of functionality.

One reason for controlling access to an object is to defer the full cost of its creation and initialization until we actually need to use it. Consider an image viewer program that must be able to list and display high-resolution photo objects that are in a folder, but how often does someone open a folder and view all the images inside. Sometimes you will be looking for a particular photo, sometimes you will only want to see an image name. The image viewer must be able to list all photo objects, but the photo objects must not be loaded into memory until they are required to be rendered.


Subject: Interface implemented by the RealSubject and representing its services.
It is also implemented by Proxy.

Proxy: Controls access to the RealSubject and may be responsible for its creation and deletion.

RealSubject: This is the real object that the proxy represents.

Common scenarios to use the Proxy pattern
Virtual Proxies
If you want to delay the creation and initialization of expensive objects until needed, where the objects are created on demand (For example creating the RealSubject object only when the doSomething method is invoked).

Remote Proxies
In distributed object communication, a local object represents a remote object (one that belongs to a different address space). The local object is a proxy for the remote object, and method invocation on the local object results in remote method invocation on the remote object. An example would be an ATM implementation, where the ATM might hold proxy objects for bank information that exists on the remote server.
Java RMI stub objects are another good example. The stub object acts as a proxy where invoking methods on the stub would cause the stub to communicate and invoke methods on a remote object (called skeleton) found on a different machine.

Protection Proxies
A protection proxy might be used to control access to a resource based on access rights.

public interface AdminRightAccess {
      public void grantInternetAccess();
}
public class AdminProxy implements AdminRightAccess {
      private String empId;
      private AdminAccess adminAccess;

      public AdminProxy(String empId) {
            this.empId = empId;
      }

      @Override
      public void grantInternetAccess() {
            if(getRole(empId)==1) {
                  adminAccess = new AdminAccess(empId);
                  adminAccess.grantInternetAccess();
            } else {
                  System.out.println("Admin rights given to "empId);
            }
      }

      public int getRole(String empId) {
            // Check role from the database based on empId
            // return 1; if employee is admin
            return 1;
      }
}
public class AdminAccess implements AdminRightAccess {
      private String empId;
      public AdminAccess(String empId) {
            this.empId = empId;
      }

      @Override
      public void grantInternetAccess() {
            System.out.println("Admin service loaded and"
                        + "rights given to "empId);
      }
}
package designpattern.proxy;
public class ProxyPatternClient {
      public static void main(String[] args) {
            AdminRightAccess access = new AdminProxy("Michael");
            access.grantInternetAccess();
      }
}
Output: Admin service loaded and rights given to Michael

Smart References
To provide a sophisticated access to certain objects such as tracking the number of references to an object and denying access if a certain number is reached, as well as loading an object from the database into memory on demand.

Proxy Design Patterns in Java's core libraries
java.lang.reflect.Proxy
java.rmi.*
javax.ejb.EJB
javax.inject.Inject
javax.persistence.PersistenceContext

9 comments:

  1. I’m going to read this. I’ll be sure to come back. thanks for sharing. and also This article gives the light in which we can observe the reality. this is very nice one and gives indepth information. thanks for this nice article... dr-wall

    ReplyDelete
  2. I feel very grateful that I read this. It is very helpful and very informative and I really learned a lot from it. 翻墙 vpn

    ReplyDelete
  3. After reading your article I was amazed. I know that you explain it very well. And I hope that other readers will also experience how I feel after reading your article. 中国 vpn

    ReplyDelete
  4. Everyone likes to save money and you’re probably here to find the best 翻墙软件.

    ReplyDelete
  5. Thanks for the blog loaded with so many information. Stopping by your blog helped me to get what I was looking for. textbook answers

    ReplyDelete
  6. You don't need to stress as there are many proxy sites across the web. You can track down another proxy ordinary and access all sites you need to see despite the fact that they are hindered. best vpn reddit

    ReplyDelete
  7. While selecting the right proxy provider you should look whether what is the total turn around time of these proxies and after how much interval their proxies are rotated. why not try here

    ReplyDelete
  8. I was surfing net and fortunately came across this site and found very interesting stuff here. Its really fun to read. I enjoyed a lot. Thanks for sharing this wonderful information. proxies cheap

    ReplyDelete
  9. "Test banks can help students prepare for exams by providing them with a variety of different practice questions and scenarios."

    ReplyDelete

Related Posts Plugin for WordPress, Blogger...