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)?
⚡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?
The proxy design pattern allows us to follow the Design Principle (SOLID).
It allows us to control the service object without the client knowing about it.
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.
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.
Package Delivery interface
We have created subclasses that are implemented by the PackageDelivery interface.
1 →Delivery by bicycle🏍
Delivery By Bicycle
2 →Delivery by Car🚗
Delivery By Car
3 →Delivery by Truck🚚
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.
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 By Car
🚚Delivery Proxy By Truck
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.
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
⚡Where to find the Codebase for Factory Design Pattern?
**On Github**
GitHub - Rohan2596/Design-pattern-examples at 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 and on Twitter at **rohankadam25**
Top comments (0)