This article describes the problem of solving red wine distribution based on Java's proxy model. Share it for your reference, as follows:
1. Pattern definition
In the proxy mode, two objects participate in processing the same request. The received request is entrusted by the proxy object to the real object to process. The proxy object controls the access of the request, which plays a mediating bridge between the client application and the real object. The proxy mode uses object aggregation instead of inheritance, effectively reducing the coupling between software modules.
2. Model examples
1 Pattern Analysis
We borrow wine distribution to illustrate this model.
2 Proxy mode static class diagram
3 Code Examples
3.1 Red Wine Factory Interface 1 IRedWine
package com.demo.real;/** * Red Wine Interface* * @author * */public interface IRedWine { // Method for producing red wine public void product(); // Method for selling wine public void sell();}3.2 Realization of a RealRedWineFactory in the Red Wine Factory
package com.demo.real.impl;import com.demo.real.IRedWine;/** * Real wine production factory* * @author * */public class RealRedWineFactory implements IRedWine { // Method for producing red wine @Override public void product() { System.out.println("Red wine factory produces red wine..."); } // Method for selling wine public void sell() { System.out.println("Red wine factory sells red wine..."); }}3.3 Create a red wine agent - RedWineProxy
package com.demo.proxy;import com.demo.real.IRedWine;/** * Red Wine Agent* * @author * */public class RedWineProxy implements IRedWine { // real red wine manufacturer private final IRedWine redWine; // agent permission to sell red wine private final boolean permission = true; // default construction method public RedWineProxy(IRedWine redWine) { this.redWine = redWine; } // agent method for producing red wine (the agent does not produce red wine, and takes wine from the real factory to sell) @Override public void product() { // Determine whether the agent has the right to be a red wine if (this.permission) { // The agent has the right to sell red wine is a legal System.out.println("[This is a legal red wine agent]"); System.out.println("The agent receives an order and notifies the factory to produce..."); this.redWine.product(); } else { System.out.println("[This is an illegal red wine agent!]"); } } // How to sell red wine for agent @Override public void sell() { if (this.permission) { this.redWine.sell(); System.out.println("The agent gets wholesale red wine from the factory and sells it at a higher price, earning a certain difference from it..."); } else { System.out.println("[This is an illegal red wine agent!]"); } }}3.4 Go to the wine agent to buy red wine client
package com.demo;import com.demo.proxy.RedWineProxy;import com.demo.real.IRedWine;import com.demo.real.impl.RealRedWineFactory;/** * Main application* * @author * */public class Client { /** * @param args */ public static void main(String[] args) { // Create a real red wine factory object instance IRedWine realRedWineFactory = new RealRedWineFactory(); // Get proxy object instance IRedWine redWineProxy = new RedWineProxy(realRedWineFactory); // Agent produces red wine (actually the real production is the factory) redWineProxy.product(); // Agent sells red wine (wholesale price, then sells it at a higher price to earn the difference) redWineProxy.sell(); }}4 Running results
[This is a legal red wine agent]
The agent received the order and notified the factory to produce...
Red wine factory produces red wine...
Red wine factory sells red wine...
The agent gets wholesale red wine from the factory and sells it at a higher price, earning a certain price difference from it...
III. The design principles of this pattern
1 Delay loading to improve system efficiency
2 Single responsibility principle
4. Use occasions
1 Remote proxy: Provides a local proxy for an object in different address spaces.
2 Virtual Agent: If the creation of an object is time-consuming, you can call it through the proxy object. Before the real object is created, a false call is returned. When the real object is created, the corresponding method call of the real object is returned to the client.
3 Protection agent controls access to the original object.
4 Smart guidelines replace simple pointers, which performs some additional operations when accessing objects.
V. Static class diagram of proxy mode
Abstract role: A common interface that declares real objects and proxy objects.
Real Role: The target object that really handles the request.
Agent role: The proxy object role contains references to real objects, so the proxy object can convert the request into real objects for processing. At the same time, the proxy object can also add additional operations before and after performing the real object operation.
For more Java-related content, readers who are interested in this site can view the topics: "Java Data Structure and Algorithm Tutorial", "Summary of Java Operation DOM Node Tips", "Summary of Java File and Directory Operation Tips" and "Summary of Java Cache Operation Tips"
I hope this article will be helpful to everyone's Java programming.