Home  >  Article  >  Java  >  Examples to explain how to use the proxy pattern in Java design pattern programming

Examples to explain how to use the proxy pattern in Java design pattern programming

高洛峰
高洛峰Original
2017-02-07 13:33:131230browse

Definition:

Provide a surrogate or placeholder for another object to control access to it.

Provide a surrogate or placeholder for another object to control access to this object.

General description:

Generally includes three roles: abstract theme, concrete theme, and agent theme.

Abstract theme: It is an abstract class or interface and a common business type definition.

Specific theme: the specific executor of business logic

Agent role: Responsible for the application of real roles, and delegates all method restrictions defined by abstract theme classes to real theme roles for implementation.

General class diagram:

Examples to explain how to use the proxy pattern in Java design pattern programming

General code:

package Proxy;
//抽象主题类:
public interface Subject {
  public void doSomething();
}
 
package Proxy;
//具体主题类
public class RealSubject implements Subject{
  @Override
  public void doSomething() {
    System.out.println("业务逻辑...");
  }
}
 
package Proxy;
//代理主题类
public class Proxy implements Subject{
    
  private Subject sub = null; 
    
  @Override
  public void doSomething() {
    this.sub.doSomething();   
  }
    
  public Proxy(Subject sub){
    this.sub = sub;
  }
    
    
}
 
package Proxy;
//客户端
public class Client {
  public static void main(String[] args) {
      
    Subject realsub = new RealSubject();
    Subject proxy = new Proxy(realsub);
      
    proxy.doSomething();
  }
}

Advantages:

1. Clear responsibilities

2. High scalability

Extension of proxy mode:

Ordinary proxy:

The specific topic class is transparent to the high-level, and the specific topic class is constructed in the proxy topic class

Code implementation:

package GeneralProxy;
  
public interface Subject {
  public void doSomething();
}
 
package GeneralProxy;
  
public class RealSubject implements Subject{
    
  private String name = null;
    
  @Override
  public void doSomething() {
    System.out.println(this.name + "被代理,正在执行业务逻辑...");
  }
    
  public RealSubject(Subject proxy,String name) throws Exception{
    if(proxy == null){
      throw new Exception("无法创建被代理对象");
    }else{
      this.name = name;
    }
        
  }
}
 
package GeneralProxy;
  
public class Proxy implements Subject{
  
  private Subject realsub = null;
      
  public Proxy(String name) {
    try {
      realsub = new RealSubject(this, name);
    } catch (Exception e) {
      e.printStackTrace();
    }
  }
    
  public void doSomething() {
    realsub.doSomething();
  }
}
 
package GeneralProxy;
  
public class Client {
  public static void main(String[] args) {
    //普通代理
    Subject proxy = new Proxy("张三");        
    proxy.doSomethaing();
  }
    
}

Forced proxy:

You must obtain the object of the proxy theme class by accessing the specific theme class, and then use the proxy theme class to control access

Code implementation:

package MustProxy;
  
public interface Subject {
  public void doSomething();
    
  public Subject getProxy();
}
 
package MustProxy;
  
public class RealSubject implements Subject{
    
  private String name = null;
  private Subject proxy = null;
    
  @Override
  public void doSomething() {
    if(isProxy()){
      System.out.println(this.name + "被代理,正在执行业务逻辑...");
    }else{
      System.out.println("请先访问代理...");
    }
      
  }
    
  public RealSubject(String name) {
    this.name = name;
  }
    
  public Subject getProxy() {
  
    proxy = new Proxy(this);
    return this.proxy;
  }
    
  private boolean isProxy(){
    if(this.proxy == null){
      return false;
    }else{
      return true;
    }
        
  }
}
 
package MustProxy;
  
public class Proxy implements Subject{
  
  private Subject realSub = null;
    
  public Proxy(Subject realSub) {
    this.realSub = realSub;
  }
    
  public void doSomething() {   
    this.realSub.doSomething();
  }
    
  
  public Subject getProxy() {
  
    return this;
  }
}
 
package MustProxy;
  
public class Client {
  public static void main(String[] args) {
    Subject realSub = new RealSubject("张三");
    realSub.doSomething();
      
    Subject proxy = realSub.getProxy();
    proxy.doSomething();
  }
}

Application scenario
In the real world, a secretary is equivalent to an agent. When the boss has a meeting, then the meeting-related work such as notifying employees of the meeting time, arranging the meeting venue, arranging the meeting venue after the meeting, etc. can be handed over. If it is done for the secretary, the boss only needs to hold meetings and does not need to do those things himself. In the same way, the proxy mode can also be used in our programming to decouple codes that are combined with a series of unrelated logic. For example, the logging code in the business code can be performed in the proxy. Spring's AOP is a typical dynamic proxy application.


Application form of proxy mode
(1) Remote Proxy (Remote Proxy) - can hide the fact that an object exists in a different address space. It also allows the client to access objects on the remote machine. The remote machine may have better computing performance and processing speed and can quickly respond to and process client requests.
(2)Virtual Proxy – Allows objects with large memory overhead to be created when needed. Only create this object when we really need it.
(3) Copy-On-Write Proxy - Used to control the copying of objects by delaying the copying of objects until the client really needs it. Is a variant of virtual agent.
(4)Protection (Access)Proxy) – Provides different levels of access to target objects for different customers
(5)Cache Proxy – Provides temporary storage for expensive calculation results , which allows multiple clients to share results to reduce computational or network latency.
(6) Firewall Proxy – Controls access to network resources and protects themes from malicious clients.
(7)SynchronizationProxy – Provides safe access to topics in multi-threaded situations.
(8) Smart ReferenceProxy - When an object is referenced, it provides some additional operations, such as recording the number of calls to this object.
(9) Complexity HidingProxy – Used to hide the complexity of a complex collection of a class and perform access control. Sometimes it is also called Façade Proxy, which is not difficult to understand. The complex hidden proxy is different from the facade pattern because the proxy controls access, while the facade pattern is different because the proxy controls access, while the facade pattern just provides another set of interfaces.

For more examples to explain how to use the proxy pattern in Java design pattern programming, please pay attention to the PHP Chinese website for related articles!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn