Design Patterns (10) - Proxy Pattern

proxy mode

1. Definition

         Provides a proxy for other objects to control access to this object.

2. Sample code

   

/*Abstract target interface*/
public interface Subject{
    // abstract request interface
    public void request();  
}

/*Specific target object*/
public class RealSubject implements Subject{
   public void request(){
       System.out.println("Processing proxy object request...");
   }
}

   

/*Proxy object*/
public class Proxy implements Subject{
    /* Holds the proxied target object */
    public RealSubject realSubject = null;
    public Proxy(RealSubject realSubject){
       this.realSubject  = realSubject ;
    }
    public void request(){
       //1. Before transposing the target object, some functional processing can be performed
       System.out.pringln("Proxy object processing");
       //2. Call the specific object
       realSubject.request();
    }
}

  

3. Practical application

       The proxy mode introduces a certain degree of indirection between the client and the object accessed by the client. The client uses the proxy directly, allowing the proxy to directly interact with the object being accessed. This additional indirection has different uses, such as Remote proxy, virtual proxy, protection proxy, smart reference.

The Essence of the Proxy Pattern: Controlling Object Access

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326387491&siteId=291194637