Design Patterns (6) - Detailed Explanation of Proxy Patterns (Easy to Understand)

Proxy Pattern Definition

Definition: Provides a proxy for other objects to control access to this object. In some cases, an object is inappropriate or cannot directly reference another object, and the proxy object can act as an intermediary between the client and the target object.

  • Subject: abstract role, extracted public methods, can be an interface or abstract class

  • RealSubject: Real role, implementing specific business logic

  • Proxy: Proxy role, the logical processing of the real role, the method of implementing the abstract role, and can add its own operation logic.

Example description

An agent is also a delegation, and the first thing that comes to my mind is game boosting. Everyone is familiar with this, let's take the game power leveling to illustrate the agent mode:

1. Abstract roles: that is, public methods - play games to level up

public interface AbstractPlayer {
     //Play game - leveling
   public void playGame () ;
}

 

2. Real characters: here refers to the people who play the game

public class GamePlayer implements  AbstractPlayer {
   private String name="";
   public  GamePlayer(String name){
       this.name=name;

   }
   @Override
   public void playGame() {
       Log.e("qzs","玩游戏-刷级");
   }
}

 

3. Agent, find a game booster here to help him level up

public class Proxy implements AbstractPlayer {
   private  GamePlayer gamePlayer= null ;

   public Proxy (GamePlayer gamePlayer) {
       this .gamePlayer=gamePlayer;

   }
   @Override
   public void playGame () {
       this .gamePlayer.playGame();

   }
}

 

4. Call:

       //Create a new player
         GamePlayer gamePlayer= new GamePlayer( "Qin Zishuai" );
       //Define a booster
         Proxy proxy= new Proxy (gamePlayer);
       //Play game refresh
         proxy.playGame();

 

模式结构

代理模式分为静态代理、动态代理。

静态代理是由程序员创建或工具生成代理类的源码,再编译代理类。所谓静态也就是在程序运行前就已经存在代理类的字节码文件,代理类和委托类的关系在运行前就确定了。

动态代理是在实现阶段不用关心代理类,而在运行阶段才指定哪一个对象。

静态代理最基本的就是上面的实例了,下面主要讲解一下动态代理,这很重要。

动态代理

动态代理是在实现阶段不用关心代理类,而在运行阶段才指定哪一个对象。

首先先了解一下动态代理类

1.Interface InvocationHandler:

该接口中仅定义了一个方法Object:invoke(Object obj,Method method,Object[] args)。这个抽象方法在代理类中动态实现。

我们来看一下invoke中的三个参数:

  • obj一般是指代理类

  • method是被代理的方法

  • args为该方法的参数数组。

 

2.Proxy:

动态代理类,提供了getProxyClass (ClassLoader loader,Class[] interfaces)和newProxyInstance(ClassLoader loader,Class[] interfaces,InvocationHandler h)两种静态方法。

我们来看一下newProxyInstance中的三个参数:

  • CLassLoader loader:类的加载器 

  • Class<?> interfaces:指定动态代理类需要实现的所有接口

  • InvocationHandler h:得到InvocationHandler接口的子类的实例

还拿上面的实例说:

1.建立动态代理类:

 

public class DynamicProxy implements InvocationHandler {
   //对真实对象的引用-被代理的实例对象
   private Object obj;

   public DynamicProxy(Object obj){
       this.obj=obj;
   }
   @Override
   public Object invoke(Object o, Method method, Object[] args) throws Throwable {
       Object result;
       //可以加上调用方法之前的操作
       result= method.invoke(this.obj,args);
       //可以加上调用方法之后的操作
       return result;
   }
}

 

2.调用:

       //新建一个游戏者
       AbstractPlayer abstractPlayer=new GamePlayer("秦子帅");

       InvocationHandler invocationHandler=new DynamicProxy(abstractPlayer);
        //类加载器
       ClassLoader classLoader=abstractPlayer.getClass().getClassLoader();
       //动态产生一个代理者
       AbstractPlayer proxy= (AbstractPlayer) Proxy.newProxyInstance(classLoader,new Class[]{AbstractPlayer.class},invocationHandler);

       proxy.playGame();

 

注意:实现动态代理的首要条件:被代理类必须实现接口。

还有一些没有提出来,比如强制代理等等,这些大家可以自己去写一写...

文章学习参考了《设计模式之禅》与网上博文

 

另外可以加入我的Android技术交流群:458739310
大家可以关注我的微信公众号:「安卓干货铺」一个有质量、有态度的公众号!

 

Guess you like

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