Structural Design Pattern - Proxy Pattern PDF

Summary

This document explains the Proxy design pattern. It details the structure, benefits (such as access control and lazy initialization), and a location-based website access proxy example. The example includes detailed code implementation for various steps in implementing the Proxy with location-based control.

Full Transcript

# Structural Design Pattern - Proxy Pattern ## Introduction to Proxy Pattern ### What is the Proxy Pattern? The Proxy Pattern is a design pattern that provides a surrogate or placeholder for another object to control access to it. ### Real-world Examples Examples of the Proxy Pattern include ac...

# Structural Design Pattern - Proxy Pattern ## Introduction to Proxy Pattern ### What is the Proxy Pattern? The Proxy Pattern is a design pattern that provides a surrogate or placeholder for another object to control access to it. ### Real-world Examples Examples of the Proxy Pattern include access control, logging, caching, and remote object access. ### Purpose of the Proxy Pattern The main purpose of the Proxy Pattern is to add an intermediary layer between a client and the actual object, allowing for additional functionality, security, or performance benefits. ### Proxy Pattern Structure The Proxy Pattern consists of a Proxy class that implements the same interface as the original object, and a Real Subject class that represents the actual object being accessed. ## Proxy Pattern Diagram The diagram shows a Proxy Pattern implemented with a Client, Subject Interface, RealSubject Class, and Proxy Class. * **Subject Interface:** Defines the common methods and properties that the RealSubject and Proxy classes will implement. * **RealSubject Class:** The actual object that the client wants to access. It will implement the methods and properties defined in the Subject Interface. * **Proxy Class:** Implements the same interface as the RealSubject class. It controls access to the RealSubject by intercepting requests, adding additional functionality, or delegating to the RealSubject. * **Client:** Interacts with the Proxy object instead of the RealSubject. It is unaware of the Proxy and believes it is interacting with the RealSubject directly. ## Advantages of Proxy Pattern * **Access Control:** The Proxy Pattern allows controlling access to an object, granting or denying access based on specific criteria. * **Lazy Initialization:** The Proxy Pattern can be used to implement lazy initialization, where the actual object is created only when it is first accessed, improving performance and reducing resource usage. ## Example: Location-Based Website Access Proxy In this scenario, we'll create a website access control proxy that restricts access to a specific website based on the user's location. ### Code Structure * **IWebAccess:** The interface defines a method for accessing websites, taking both the website URL and user location as parameters. * **RealWebAccess:** This class implements the actual logic for accessing websites. * **LocationBasedWebAccessProxy:** This proxy checks the user's location before allowing access. The proxy defines a list of allowed locations (USA, Canada, and UK). If the user's location is in the allowed list, access is granted; otherwise, access is denied. * **Client Code:** The client generates an instance of the proxy and tests access from various locations, demonstrating the access control logic. ### Output The output shows the results of accessing a website from various locations, including successful access from USA and Canada, and access denied from Germany and Australia. ### Code Implementation * **Step 1: Define the Subject Interface** - Creates the interface called `IWebAccess` - Defines a method called `AccessWebsite` which takes the website URL (string) and user location (string) as input. * **Step 2: Implement the Real Subject** - Creates the `RealWebAccess` class which implements the `IWebAccess` interface. - Implements the `AccessWebsite` method by printing a message to the console indicating that the website is being accessed from a specific location. * **Step 3: Implement the Proxy with Location-Based Access Control** - Creates the `LocationBasedWebAccessProxy` class which also implements the `IWebAccess` interface. - Contains a private variable called `_realWebAccess` which holds an instance of the `RealWebAccess` class, representing the actual website access logic. - It defines a list of allowed locations (USA, Canada, UK) and uses the `Array.Exists` method to check if the user's location is included in the list. - If the user's location is allowed, `AccessWebsite` method of `_realWebAccess` is invoked. - If the user's location is not allowed, a message is printed to the console that access is denied. * **Step 4: Client Code** - This code creates a new instance of the `LocationBasedWebAccessProxy` as `webAccess`. - It then tests access from different locations, demonstrating the access control behavior. This is a simplified explanation of the Proxy Pattern and its usage in a real-world example. More complex implementations may involve additional functionality and interactions based on specific design requirements.

Use Quizgecko on...
Browser
Browser