Moe's Tech Blog
[Design Pattern] Proxy Pattern 본문
Motivation
- There are times when it's easier, safer, or more convinent to use a placeholder to represent something or something else
- Example
- 1. Individual representatives may be sent to sepak on behalf of company
- 2. Crash test dummies are used instead of real human beings
- 3. Credit card is used as a method of payment instead of cash
- All of the objects in the example are represented by a proxy object
- Example
Why do we want to use Proxy Class
- 1. Act as a virtual proxy where the proxy class is used in place of a real subject class that is resource intensive to instantiate
- Used commonly on images, and web pages, or graphic editors
- A signle high definition image can be extremely large
- Reloading all of the images at once can be very time and resource intensive
- 2. Act as a protection proxy in order to control access to the real subject class
- a protection proxy can be used in learning management system that cheks the credential of a user
- So the different users, like students and instructors can only access the appropriate functions permitted by their role
- a protection proxy can be used in learning management system that cheks the credential of a user
- 3. Act as a remote proxy where the proxy class is local and the real subject class exists remotely
- this can be thought as working on a google document
- The web browser has all the object it needs locally
- Also exists on a Google server somewhere else
- this can be thought as working on a google document
What is Proxy Design Pattern
- Acts as a simplified or lightweight version of the original object
- Is still able to accomplish the same tasks, but may delete requests to the original object to achieve them
- in software world, there are some cases where the use of proxy object is better than the original
- Allows a proxy class to represent a real subject class
- wraps the real subject class, that is hides a reference to an instance of the real subject class
- the real subject class may contain sensitive information or would be resource intensive to instantiate
- since it acts as a wrapper, the client class will interact with it instead of the real subject class
- What calls need to be supported in the proxy class?
- The proxy class is meant to stand in for the real subject class, it must offer the same methods
- This can be ensured by having both these classes implement a common subject interface
- allows polymorphism
- Since the proxy and real subject classes are both sub-types of subject, a client class can interact with the proxy
- The proxy class is meant to stand in for the real subject class, it must offer the same methods
Where do we want to use Proxy Design Pattern
- 1. Online retail store with global distribution and warehousing
- how would you know which warehouse to route orders to fulfill customer orders?
- You may run into an issue where you route orders to a warehouse that doesn't have stock for an item in a customer worder
- You need some way to determine which warehouse to send orders to
- A system that routes the order's full fulfillment to an appropriate warehouse will prevent your warehouse from receiving orders they cannot fill
- How is this system implemented?
- Use proxy design pattern to have a proxy protect your real subject (warehouse) from receiving orders if the warehouse don't have enough stock to fulfill an order
- 1. The order fulfillment class acts as a proxy for all your warehouses
- It has list of warehouses that it can delegate parts of customer oders depending on whether or not a warehouse is capable of fulfilling a part of the order
- 2. The warehouse class is the real subject class and will be responsible for actually processing and fulfilling parts of customer orders
- it will report it's inventory to the proxy that asks for it
- 3. The client class or client will interact with your system by using the interface
- 1. The order fulfillment class acts as a proxy for all your warehouses
- Use proxy design pattern to have a proxy protect your real subject (warehouse) from receiving orders if the warehouse don't have enough stock to fulfill an order
- how would you know which warehouse to route orders to fulfill customer orders?
How to Implement Proxy Pattern
- Step 1: Design the subject interface
- This will be used by the client to interact with the system
- i.e. this will be used by OrderFulfillment and Warehouse classes
public interface IOrder {
public void fulfillOrder(Order);
}
- Step 2: Implement the real subject class
- The warehouse class knows how to process an order for fulfullment
- also, the warehouse class knows how to report the current stock of items
- Note it doesn't need to check if it has enough stock to fulfill an order
- This is because an order should only be sent to warehouse if it can be fulfilled
public class Warehouse implements IOrder {
private Hashtable<String, Integer> stock;
private String address;
/*Constructor and other attributes would go here*/
public void fulfillOrder(Order order) {
for (Item item : order.itemList) {
this.stock.replace(item.sku, stock.get(item) - 1);
/*Process the order for shipment and delivery*/
}
}
public int currentINventory(Item item) {
if (stock.containsKey(item.sku)) {
return stock.get(item.sku).intValue();
}
return 0;
}
}
- Step 3: Implement the proxy class
- The OrderFulfillment class does all the work of checking the warehouse inventory and ensuring that an order can be successfully completed before sending the request to the warehouse
- The OrderFulfillment class asks each warehouse about whether it has enough stock of a particular item
- If it has enough item --> the item gets added to a new order object that will be sent to the warehouse
public class OrderFulfillment implements IOrder {
private List<Warehouse> warehouses;
/*Constructors and other attributes would go here*/
public void fulfillOrder(Order order) {
/*For each item in a custom order, check each warehouse to see if it is in stock */
/*If it is then create a new Order for that warehouse. Else, check the next warehouse*/
/*Send all the orders to the warehouses(s) after you finish iterating over all items in the original order*/
for (Item item: order.itemList) {
for (Warehouse warehouse : warehouses) {
...
}
}
return;
}
}
Conclusion
- The proxy class ensures that an order can be fulfilled before sending the request to the warehouses
- This protects your warehose system from receiving orders it cannot fulfill
- The OrderFulfillment class also lets you seperate order validation from the order fulfillment by separating them into two pieces
- This will improve the overall rate of processing an order because the warehouse doesn't have to worry about the validation process
- Each individual warehouses doesn't need to know how to reroute an order if the warehouse cannot fulfill the order
- The OrderFulfillment class can be improved with other functionalities such as:
- 1. priortizing sending orders to warehouses based on proximity to the customer to ensure the order arrives as fast as possible
- The proxy design pattern is useful when you need to
- 1. Defer creating resource intensive objects until needed
- 2. Control access to specific objects
- 3. When you need something to act as a local representation of a remote system
- To summarize, the main features of the proxy design pattern are:
- 1. To use the proxy class to wrap the real subject class
- 2. To have a polymorphic design so that the client class can expect the same interface for the proxy and real subject classes
- 3. To use a lightweight proxy in place of a resource intensive object until it is actually needed
- 4. To implement some form of intelligent verification of requests from client code in order to determine if, how, and to whom the requests should be forwarded to
- 5. To represent a local representation of a system that is not in the same physical or virtual space
- It provides a powerful means of indirection
- it is robust and can let you build systems that are more secure and less resource-intensive
'Software Design Pattern > Notes' 카테고리의 다른 글
[Design Pattern] Decorator Pattern (0) | 2022.08.22 |
---|---|
Summary of Design Patterns (0) | 2022.08.22 |
[Design Pattern] Composite Pattern (0) | 2022.08.15 |
[Design Pattern] Adapter Pattern (0) | 2022.08.15 |
[Design Pattern] Facade Pattern (0) | 2022.08.13 |