Design Patterns (XI) proxy mode (Proxy)

  Software development industry has a point: Task problems can add an intermediate layer to resolve. Proxy mode is also a product of this idea.

  First, look at the definition of proxy mode: providing a proxy to control access to the object to other objects. It is to delegate class to another class, use this class to control access to the original of that class, such as control of the authority. Class look at FIG.

  Acting divided into static and dynamic proxy agent. Next we look at the static agent.

Static agents

1  // Interface 
2  public  interface Human {
 . 3      public  void EAT ();
 . 4      
. 5      public  void   Breath ();
 . 6  }
 . 7  
. 8  // original object 
. 9  public  class Baby the implements Human {
 10      public  void EAT () {
 . 11          the System. Out.println ( "sucking in" );
 12 is      }
 13 is      
14      public  void   breath () {
 15          System.out.println ( "endlessly breathing!" );
16      }
 17  }
 18 is  
. 19  @ proxy class 
20 is  public  class Mom the implements Human {
 21 is      Private Human Human;
 22 is      public Mom () {
 23 is          the this . Human = new new Baby ();
 24      }
 25      
26 is      public  void EAT () {
 27          the System .out.println ( "feeding the baby to nurse!" );
 28          human.eat ();
 29      }
 30      
31 is      public  void Breath () {
 32         human.breath ();
 33 is      }
 34 is  }
 35  
36  public  class Client {
 37 [      public  static  void main (String [] args) {
 38 is          Human HM = new new Mom ();
 39          hm.eat ();
 40          hm.breath () ;
 41      }
 42  }
 43 is  
44 is ------------------------------------------- ------------------------------
 45  Hey baby nurse!
46  like feeding
 47 and never stops breathing!

Dynamic Proxy

   Example is the use of dynamic proxy example above the human interface and the Baby classes.

//动态代理
public class ProxyMom implements InvocationHandler {

    private Human human = null;
    
    public Human getProxyInterface(Human human1){
        this.human = human1;
        Human hm = (Human)Proxy.newProxyInstance(human.getClass().getClassLoader(), human.getClass().getInterfaces(), this);
        return hm;
    }
    
    @Override
    public Object invoke(Object proxy, Method method, Object[] args)
            throws Throwable {
        // TODO Auto-generated method stub
         
        Object methodObj = null;
        System.out.println("喂婴儿吃奶!");
        methodObj = method.invoke(human, args);
        
        return methodObj;
    }
    
}

public class Client {
    public static void main(String[] args) {
        ProxyMom pm = new ProxyMom();
        Human baby= pm.getProxyInterface(new Baby());
        baby.eat();
        baby.breath();
        
    }
}
-------------------------------------------------- ----------------------- 
hey baby nurse! 
In infants 
fed infant feeding! 
Never stops breathing!

  1, java dynamic proxy inside InvocationHandler need to implement the interface.

  2, and then rewrite the invoke method. The first parameter is: proxy class object. The second is: the need for proxy method, and the third is the method parameters. Further invoke like is automatically adjusted. Users do not need to call display.

      By way of example we saw it, and if it is static agent, then the agent class and native classes are one to one. How many native class methods, the agent class also need to have the appropriate basic approach to agents, so always follow the original proxy class born to change. When the native class has many ways when proxy class also needs a number of ways, very inflexible. The dynamic proxy by Proxy, InvocationHandler and invoke methods on the outcome of this issue. Because the unified process can invoke a method on it. Convenient and flexible.

  java dynamic proxy can only proxy interface, and if you want to proxy class, cglib need to use the library.

Agent typical use case

        1, the remote agent. 2. Alerts. 3, Copy-on-Write agents. 4, protection (Protect or Access) agent. 5, Cache proxy. 6, firewall (Firewall) agent. 7, synchronization (Synchronization) agent. 8, smart referrals (Smart Reference) agent.

  Typical usage is also seen by the nature of the agent model is to control access to class

Difference proxy mode and decorative patterns

  Of course, they are very similar in the realization of. But the agency model to emphasize that access to the kind of control, but mainly for decorative pattern to the class of dynamic enhancements.

Guess you like

Origin www.cnblogs.com/pengweiqiang/p/11219066.html