How to implement Proxy Design Pattern using Java?
Tags: design, beginners, programming, opensource
Published: 2022-01-14 05:05:14


## Design Pattern

 

Hello👋 Its Rohan Kadam😊

Maybe you are a newbie to coding👩‍💻 or programming, maybe experienced👴 or maybe a FrontEnd Developer or BackEnd Developer you all have may have come across the word Design Pattern as a principle or as a concept. The design pattern is a way or approach to writing code or developing an application. In this article, we try to answer certain questions What is Design Pattern? Why implement a design pattern? How to implement a Design pattern (**Proxy**)?

![How to implement Proxy Design Pattern?](https://cdn-images-1.medium.com/max/3840/1*5h04pUXMLXPdJpOWcBxVNA.png)

### ⚡What is Design Pattern?

**Design patterns** are the solutions to commonly occurring problems in software design. They are like pre-fabricated blueprints that we can customize to solve a recurring design problem in your code.

They are not pieces of code or libraries that can be imported and used in the development of software or solving a particular problem. We can follow the pattern details and implement a solution that suits the realities of your own program.
>  **We often confused ourselves between design patterns and algorithms. While an algorithm always defines a clear set of actions that can achieve some goal, a pattern is a more upper-level description of a solution.**

An analogy to an algorithm is a cooking dish: both have clear steps to achieve a goal. They’re not libraries or modules; they’re guidelines you integrate into the core of your designs, giving you a leg up in creating flexible and maintainable object-oriented systems.

### ⚡Why implement a Design Pattern?

We have encountered design patterns only in a nutshell, they’re general object-oriented solutions that you can use in your own designs. Crafted by experienced 👴object-oriented practitioners, design patterns can make your designs more flexible, more resilient to change, and easier to maintain. So, if design patterns still aren’t part of your development toolbelt, here are five reasons you should add them.

1 → Make our life easier by not reinventing the wheel

2 → Improve our object-oriented Skills

3 → Recognize Patterns in libraries and languages

4 → Use the power of a shared vocabulary

5 → Find Truth and beauty

### ⚡How to implement a Proxy Design Pattern?

Before jumping directly into the implementation of the design pattern we need to answer certain questions such as What is a Proxy design pattern? Why incorporate the design pattern? and finally How to implement a Proxy Design Pattern?

**1 →What is Proxy Design Pattern?**

The Proxy design pattern is part of the Structural Design Pattern Family, it let us create a placeholder or substitute for a real object. A real-life example will be, **Imagine you have run a big delivery business🛒, you got to deliver the package to a consumer now the question arises what should use you used delivery the particular car🚗 , truck 🚚or bicycle🛵**. **So to solution could be we give input as the size of a package based on that we decide which would be the best possible mode of transportation but if the package info doesn’t match with the condition we through default message for it**. That’s how the Proxy pattern works in real life.
>  **Definition:** 
*The Proxy pattern is a design pattern that *lets us **provide a substitute or placeholder for another object. A proxy controls access to the original object, allowing you to perform something either before or after the request gets through to the original object.**

**2 → Why implement Proxy Design Pattern?**

 1. The proxy design pattern allows us to follow the Design Principle (**SOLID**).

 2. It allows us to control the service object without the client knowing about it.

 3. It allows us to introduce new code without affecting the existing code structure like **Plug and Play Model supporting our Open-closed principle.**

**3 → How to Implement Proxy Design Pattern?**

### In section, we try to implement Proxy Design Pattern using Java. For example, we use the delivery manager example for selecting the mode of transportation.

**Step 1 → What input should the delivery manager give?**

As a Delivery Manager, he/she should give info about the package like **size** and **weight**, in our example, we are using **PackageInfo** to the job.

![Package Info](https://cdn-images-1.medium.com/max/2000/0*b6Yg19-Tjw4L-pMZ.png)

**Step 2 → What will be various modes for making a delivery?**

The various modes of delivering packages are car🚗, bicycle🏍 and truck🚚. We have created three-class defining the modes for delivery.

![img0](https://cdn-images-1.medium.com/max/2000/0*92XOgHaC63UBOdUH.png)

Package Delivery interface

We have created subclasses that are implemented by the PackageDelivery interface.

**1 →Delivery by bicycle🏍**

![imag1](https://cdn-images-1.medium.com/max/2000/0*Q1dr58mLipwPQOij.png)

Delivery By Bicycle

**2 →Delivery by Car**🚗

![imag2](https://cdn-images-1.medium.com/max/2000/0*jV3qRMb2Eq0yneKv.png)

Delivery By Car

**3 →Delivery by Truck**🚚

![Delivery By Truck](https://cdn-images-1.medium.com/max/2000/0*ldq3FtMTIFoZqTXI.png)

**Step 3→ How will be the proxies managed?**

In Example, the proxies will be managed by the Delivery Manager Class we can also be called as Factory Manager Class. Further down the line proxies classes decide based on the weight of the package that will be delivered or rejected for delivery.

![Delivery Manager](https://cdn-images-1.medium.com/max/3068/1*a2DCzFoneCKqvkCIlS5EqQ.png)

**Step 4→ How to provide a substitute or placeholder for another object?**

According to definitions of proxy pattern, we need to **provide a substitute or placeholder for another object so in our examples**, we are using delivery proxy class for are various modes. In our case, we are checking the weight limit for the Package if it fails it through an overweight message. Further understanding the examples in deep-dive a **proxy controls access to the original object, allowing you to perform something either before or after the request gets through to the original object in our example proxies class are checking the package object based on weight before its goes to delivery method.**

### 🛵Delivery Proxy For Bicycle

![Delivery Proxy For Bicycle](https://cdn-images-1.medium.com/max/3236/1*Pve5NaOVAe-vF8HrbGX_wA.png)

### 🚗Delivery Proxy By Car

![Delivery By Car](https://cdn-images-1.medium.com/max/3236/1*mbveX97_XQXwRv0OtDwRzQ.png)

### 🚚Delivery Proxy By Truck

![DeliveryProxy By Truck](https://cdn-images-1.medium.com/max/3236/1*OWShNGsFFzVMT5AjuP5Vzg.png)

**Step 5→ How to test the Factory Design Pattern?**

To test our Factory Design Pattern we are giving 3 different packages with different 3 different Packages sizes like **SMALL, LARGE and MEDIUM & also the weight of Package Info.**

![Design Pattern](https://cdn-images-1.medium.com/max/3180/1*ezyXmK5_bN8Cf51cKl9E7w.png)

**Step 6→What will be the Output for Factory Pattern?**

Below console output helps to understand how the Manager(Factory Class) is selecting the mode of transportation based on Package Info

![Output For Factory Pattern](https://cdn-images-1.medium.com/max/3524/1*s0ofehOMfO0uvbmShnpE9g.png)

### ⚡Where to find the Codebase for Factory Design Pattern?

[**On Github](https://github.com/Rohan2596)**
[**GitHub - Rohan2596/Design-pattern-examples at pattern/proxy**](https://github.com/Rohan2596/Design-pattern-examples/tree/pattern/proxy)

### 📑Conclusion:-

In the article, we tried to answer some questions related to Design Pattern and especially **Proxy Design Pattern** how it helps developers around the world to write better code and build faster applications. **Proxy Design Pattern** allows us to implement the **SOLID **principle effectively may it is **loose-coupling or Single-responsibility.**

Please do share and like💖 if you find the article useful. Follow me on medium [Rohan Ravindra Kadam](undefined) and on Twitter at [**rohankadam25](https://twitter.com/rohankadam25)**

### 📚Bibliography:-
[**Proxy**](https://refactoring.guru/design-patterns/proxy)
[**Proxy pattern - Wikipedia**](https://en.wikipedia.org/wiki/Proxy_pattern)

![](https://cdn-images-1.medium.com/max/2800/1*0cxzfmp5UUo7O2CYgyPs4A.png)