Proxy Design Pattern
Start course
3h 59m

In this course, we will learn the concepts of microservice and spring framework with a focus on design patterns.

Learning Objectives

  • Learn about various design patterns

Intended Audience

  • Beginner Java developers
  • Java developers interested in learning how to Build and Deploy RESTful Web Services
  • Java Developers who want to develop web applications using the Spring framework
  • Java Developers who want to develop web applications with microservices
  • Java Developers who wish to develop Spring Boot Microservices with Spring Cloud


  • Basic Java knowledge

Hello, dear friends. In this video, we will examine Proxy Design Pattern. So, let's begin. The proxy design pattern is a design pattern belonging to the set of structural patterns. Structural patterns are a category of design patterns used to simplify the design of a program on its structural level. As its name suggests, the proxy pattern means using a proxy for some other entity. In other words, a proxy is used as an intermediary in front of or wrapped around an existing object. 

Proxy design pattern is used to control access to another object which may be secure or expensive to create. According to the GoF definition of proxy design pattern, a proxy object provides a surrogate or placeholder for another object to control access to it. A proxy is basically a substitute for an intended object which we create due to many reasons. For example, security reasons or costs associated with creating a fully initialized original object. So, the famous use case of this is with your Internet usage. The student is not permitted to access any restricted sites while on school grounds and computer labs. When he tries to access that site, another page appears informing him that it is restricted. If the user is authorized, the site will be opened or access to a secure object will be granted. Otherwise, an error message will be displayed.

So, let's examine the UML of the proxy pattern. Subject is an interface which exposes the functionality available to be used by the clients. RealSubject is a class implementing subject and it is a concrete implementation which needs to be hidden behind a proxy. Proxy hides the real object by extending it and clients communicate to the real object via this proxy object. Usually, frameworks create this proxy object when clients request for the real object. Of course this pattern provides us some advantages like the remote proxy can shield the fact that the implementation resides in another address space. Basically, it provides the protection to the original object from the outside world. The virtual proxy can perform optimizations such as creating objects on demand. So, let's ask another question. When should we use this pattern? The proxy pattern is appropriate when a more versatile or sophisticated reference to an object rather than a simple pointer is needed. So, I think it's that time, time to create a project using the proxy pattern. Let's create a new project and name it Proxy.

As we spoke about Internet usage, let's use a school as an example. First, create Internet interface. We need a method as Connect. Let's define RealInternet class. Now, also we need a ProxyInternet. First define RealInternet with Internet interface. We need list, so first, let's import list libraries. Now, define a string list as bannedLists. Let's add some sites to the list. Now, we have to override Connect method. We need a condition to determine, is site denied or not. Now, we can write client codes. First, define a ProxyInternet. Try to connect four sites. Now, if we run the project, we will see three connections and one access denied. So, let's try. As you can see, we have three successful connections and one access denied. Great. So, we finished the proxy design pattern. We will look at our final structural design pattern called Facade. So, I'll see you in the following video my friends.


About the Author
Learning Paths

OAK Academy is made up of tech experts who have been in the sector for years and years and are deeply rooted in the tech world. They specialize in critical areas like cybersecurity, coding, IT, game development, app monetization, and mobile development.