Proxy design pattern in java

CodeProjectProxy design pattern allows you to create a wrapper class over real object.Wrapper class which is proxy,controls access to real object so in turn you can add extra functionalities to real object without changing real object’s code.
As described by GoF:

“Provide a surrogate or placeholder for another object to control access over it.”

Real life example may be proxy server used in IT companies for internet access.It blocks access to social networking sites like facebook,twitter and other contents which are not work related.

When to use it:

Proxy is required whenever there is need for more sophisticated or versatile reference to an object than a simple pointer.Here are some situations when proxy pattern is applicable.
  1. A remote proxy provides a local representative for an object in a different address space.Providing interface for remote resources such as web service or REST resources.
  2. A virtual proxy creates expensive object on demand.
  3. A protection proxy controls access to the original object.Protection proxies are useful when objects should have different access rights.
  4. A smart reference is a replacement for a bare pointer that performs additional actions when an object is accessed.
  5. Adding a thread-safe feature to an existing class without changing the existing class’s code.

UML diagram for proxy design pattern:

Elements:

  • Proxy
    • maintains a reference that lets the proxy access the real subject.Proxy may refer to a subject if the RealSubject and Subject interface are the same.
    • provides an interface identical to Subject’s so that proxy can be substituted for the real subject.
    • controls access to the real subject and may be responsible for creating and deleting it.
  • Subject
    • defines the common interface for RealSubject and proxy so that a Proxy can be used anywhere a RealSubject is expected.
  • RealSubject
    • defines the real object that proxy represents. 

WorkFlow:

Proxy forwards request to RealSubject when appropriate ,depending on the kind of proxy.

Example:

Let’s see an example.I am taking example of protection proxy. In our example, we have a folder in which you can perform various operation such as copy,paste file or subfolder.In OOP terms,we have IFolder interface and Folder class which provides performOperatons() method, and these are the existing class and interface that we cannot change. We want to further specify that only user with authorization can access it and perform operations such as cut or copy files and sub folder.

UML diagram for example:

Comparing with above generic elements,we have

  • FolderProxy(Proxy)
  • Folder(RealSubject)
  • IFolder(Subject)

Java codes for above classes:

IFolder.java (Subject):

Following class is our realSubject class.Lets say we can not change it but we want to provide authorization on it.
Folder.java (RealSubject):

Following class provides authorization to Folder class.It checks for userName and password and if matched then only it gives access to folder.
FolderProxy.java(Proxy):

User.java:

ProxyDesignPatternMain.java:

Output:

So without changing Folder class,we have provide authentication to folder with the help of FolderProxy class.

Source code:

Was this post helpful?

Related Posts

  • 25 June

    Convert Roman Number to Integer in Python

    Table of ContentsHow to Convert Roman Number to Integer in PythonHow to convert roman number to integer in python?Using the if...else statement to convert roman number to integer in Python.Using classes and a dictionary to convert roman number to integer in Python.Using the roman module to convert roman number to integer in Python.ConclusionWas this post […]

  • 11 June

    Remove Single Quotes from String in Python

    Table of ContentsRemove single quotes from string in Python.How to remove single quotes from string in Python?Using the replace() function to remove single quotes from string in Python.Using a for loop to remove single quotes from string in Python.Using the join() function to remove single quotes from string in Python.Using the re module functions to […]

  • 11 June

    Escape Backslash Character in Python

    Table of ContentsEscape backslash character in python.How to use escape backslash character in python?Using the escape backslash character in python to represent whitespace characters.Using the escape backslash character in python to turn special characters into ordinary characters.Using the escape backslash character in f-strings in Python.Using the escape backslash character in raw strings in Python.Was this […]

  • 11 June

    Remove Parentheses From String in Java

    Table of ContentsJava StringsRemove Parentheses From a String Using the replaceAll() MethodRemove Parentheses From a String by TraversingConclusionWas this post helpful? Java uses the Strings data structure to store the text data. This article discusses methods to remove parentheses from a String in Java. Java Strings Java Strings is a class that stores the text […]

  • 11 June

    Break out of function in Python

    Table of ContentsBreak out of function in PythonHow to break out of function in Python?Using the return statement to break out of function in PythonUsing the try and except block to break out of function in PythonUsing the sys.exit() function to break out of function in PythonConclusionWas this post helpful? In this post, we will […]

  • 11 June

    Check if Object Is Iterable in Python

    Table of ContentsWhat are iterables in Python?How to check if object is iterable in Python?Using the iter() function to check if object is iterable in PythonUsing the for loop to check if object is iterable in PythonUsing the isinstance() function to check if object is iterable in PythonConclusionWas this post helpful? In this post, we […]

Leave a Reply

Your email address will not be published.

Subscribe to our newletter

Get quality tutorials to your inbox. Subscribe now.