Software Design Patterns and Principles - Part 9 (Proxy Design Pattern)
Saiful Islam Rasel
Senior Engineer, SDE @ bKash | Ex: AsthaIT | Sports Programmer | Problem Solver | FinTech | Microservice | Java | Spring-boot | C# | .NET | PostgreSQL | DynamoDB | JavaScript | TypeScript | React.js | Next.js | Angular
**** Previous Parts ****
Part 1: Introduction
Story:
Samiul and Ruhul are two close friends. Samiul is looks handsome and dashing. On the other hand, Ruhul looks black and fatty. But Ruhul has a online girl friend. He is very romantic, they chat everyday. They have a very good relationship in online. But one day the girl wants to meet face to face. In this scenario, Ruhul is in very deep tension because he hasn't enough confidence due to his look. So he decided to send his close and dashing friend Samiul as his Proxy.
Proxy Design Pattern:
Definition:
Proxy is a structural design pattern that lets you 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.
As the definition stat, Proxy design pattern suggest a middle man approach. Means introduce a substitute of real one and do some sanity, checks, functionality before real one and many more. The main goal is reduce load and secure the main/original object.
Problem:
In the above story, Ruhul don't want to meet directly due his lack of confidence and look. But he wants to maintain the indirect (online relationship). So how can he overcome the meeting day without his direct interaction?
The proxy design pattern is versatile and can be applied in various scenarios to address different concerns. Here are some common scenarios where the proxy design pattern can be used:
Scenario: Loading heavy resources.
Example: Imagine an image gallery where high-resolution images are loaded only when they are viewed. A virtual proxy can be used to represent the high-resolution image, loading it only when the user requests to view it.
Scenario: Controlling access to sensitive functionality or data.
Example: In a system where certain users have administrative privileges, a protection proxy can restrict access to certain features or data, ensuring that only authorized users can perform certain actions.
Scenario: Accessing objects or services over a network.
Example: When working with distributed systems, a remote proxy can represent an object that resides on a remote server. The proxy handles communication details like network calls, serialization, and deserialization, providing a local representation of the remote object.
Scenario: Caching expensive resource accesses.
Example: Consider a web page that fetches data from a remote server. A cache proxy can store the results of expensive queries or network calls, preventing redundant requests by returning the cached data if the same request is made again.
Scenario: Logging method calls or interactions.
领英推荐
Example: When debugging or monitoring an application, a logging proxy can wrap around an object or service, capturing information about method calls, input parameters, and results. This can be useful for performance monitoring, auditing, or debugging.
Scenario: Adding additional behavior or controlling resource allocation.
Example: Imagine a scenario where you want to limit the number of instances of a resource-heavy object in the system. A smart proxy can manage the instantiation of these objects, ensuring that only a certain number are created or that they are created on-demand.
Scenario: Implementing the observer pattern.
Example: In a system where multiple components need to be notified of changes in another component, a subject/observer proxy can manage the subscription and notification process. Components can subscribe to the proxy, and it can broadcast changes to all subscribers.
Scenario: Ensuring immutability of objects.
Example: If you want to ensure that certain objects cannot be modified after creation, an immutable proxy can wrap around the original object, preventing any modifications. This is particularly useful in scenarios where data integrity and consistency are critical.
These scenarios highlight the flexibility and usefulness of the proxy design pattern in solving different problems across various domains in software development.
Solution:
From the above definition of proxy pattern, it gives us a middle man capability. So in the above story, Ruhul sends/sets Samiul as a proxy/middle man and the issue solve, right?
We can also use or implement proxy pattern as a solution of the problems stated earlier sections as well.
UML Diagram:
Here in UML diagram we can see that, Clients interacts with the common interface called Subject. And Proxy and RealSubject implements this common interface. Now when client call for DoAction() instead of direct access of RealSubject it first lands to Proxy and then delegate to real one.
When to use:
Implementation:
// Subject interface
interface Subject {
void request();
}
// RealSubject class
class RealSubject implements Subject {
public void request() {
System.out.println("RealSubject: Handling request.");
}
}
// Proxy class
class Proxy implements Subject {
private RealSubject realSubject;
public void request() {
// Lazy initialization: create the real subject only when necessary
if (realSubject == null) {
realSubject = new RealSubject();
}
realSubject.request();
}
}
// Client class
public class Client {
public static void main(String[] args) {
System.out.println("Client: Executing the client code with a real subject:");
RealSubject realSubject = new RealSubject();
realSubject.request();
System.out.println("\nClient: Executing the same client code with a proxy:");
Proxy proxy = new Proxy();
proxy.request();
}
}
Another C# implementation of Proxy design pattern.
Achieved Design Principles:
The Proxy design pattern helps achieve several design principles, enhancing the overall quality, maintainability, and flexibility of the software system. Here are some design principles facilitated by the Proxy pattern:
By adhering to these design principles, the Proxy pattern helps developers create software systems that are modular, flexible, maintainable, and efficient, contributing to overall software quality and robustness.
Insha Allah, we finish covering the creational and structural design patterns with that. From the next part we start talking about the behavioral design patterns.
Happy Learning !!!
Happy Coding !!!
Happy Programming !!!
Senior Engineer, SDE @ bKash | Ex: AsthaIT | Sports Programmer | Problem Solver | FinTech | Microservice | Java | Spring-boot | C# | .NET | PostgreSQL | DynamoDB | JavaScript | TypeScript | React.js | Next.js | Angular
1 年Previous Parts: Part 1:?https://www.dhirubhai.net/pulse/software-design-patterns-principles-part-1-saiful-islam-rasel-eqfec/ Part 2:?https://www.dhirubhai.net/pulse/software-design-patterns-principles-part-2-list-most-rasel-kepmc/ Part 3:?https://www.dhirubhai.net/pulse/software-design-patterns-principles-part-3-factory-method-rasel-ir4tc/ Part 4:?https://www.dhirubhai.net/pulse/software-design-patterns-principles-part-4-builder-pattern-rasel-41cmc/ Part 5:?https://www.dhirubhai.net/pulse/software-design-patterns-principles-part-5-adapter-pattern-rasel-nfjrc/ Part 6:?https://www.dhirubhai.net/pulse/software-design-patterns-principles-part-6-decorator-rasel-ijxwc/ Part 7:?https://www.dhirubhai.net/pulse/software-design-patterns-principles-part-7-composite-rasel-tuzfc/ Part 8: https://www.dhirubhai.net/pulse/software-design-patterns-principles-part-8-fa%2525C3%2525A7ade-pattern-rasel-6bucc/