Skip to content
APPCITORThe Tech Blog
Esc

Structural – Proxy Design Pattern

August 4, 2020 1 min read

Structural – Proxy Design Pattern

Proxy design pattern is a Structural design pattern that provides a class which will limit the access to another class. This is something that authorized to handle the requests on behalf of the original object.

Proxy pattern is applicable in four common situations.

  1. Virtual proxy – The real object is only created when a client first access the object.
  2. Remote proxy – Provides a local representative for an object that resides in a different address space.
  3. Protective proxy – Controls access to a sensitive master object.
  4. Smart proxy – Interposes additional actions when an object is accessed.

Let’s discuss about proxy pattern with an example in java.

Assume that in your country, few sites are banned. And I am going to create a program that can access other sites and cannot access the banned sites.

Here I created an interface called “Internet” which has the major function that is to access the site you want. And then you can see I implemented the interface and created the class called “RealInternet” to connect to the host that is not banned.

This is the class I created to identify the banned sites and block the access to those sites. You can see I listed the banned sites and access denied in the overridden method.

At last in this main method you can see how we can access the banned sites and the sites that not banned. The proxy first checks the host you are connecting to, if it is not a banned site from the banned sites list, then it connects to the real internet. Otherwise blocks the access. Here is the output.

Proxies are also called surrogates, handles and wrappers.

You can visit the below given articles to learn about creational design patterns.

Thank you.

Creational Design Patterns

Singleton Design Pattern

Factory Design Pattern

Abstract Factory Design Pattern

Prototype Design Pattern

Builder Design Pattern

Share this article

Keep reading