美文网首页
<<设计模式之禅(第二版)>>——第十二

<<设计模式之禅(第二版)>>——第十二

作者: leiiiooo | 来源:发表于2016-10-14 21:07 被阅读17次
    定义:
    • 为其他对象提供一种代理以控制对这个对象的访问。
    通用类图:
    代理模式通用类图表示
    /*
     * 定义一个抽象的主题类
     * */
    public interface Subject {
      // 定义一个方法
      public void request();
    }
    public class RealSubject implements Subject {
    
      @Override
      public void request() {
        // 定义具体的业务逻辑
    
      }
    
    }
    
    /*
     * 实现相关的代理类
     * */
    public class Proxy implements Subject {
    
      private Subject subject;
    
      public Proxy(Subject subject) {
        this.subject = subject;
      }
    
      @Override
      public void request() {
        // TODO Auto-generated method stub
        doBefore();
        subject.request();
        doAfter();
      }
    
      private void doBefore() {
    
      }
    
      private void doAfter() {
    
      }
    
    }
    
    代理模式的优点:
    • 职责清晰
    • 搞拓展性
    • 智能化(动态代理)
    代理模式的拓展:
    • 普通代理模式(调用者知道代理的存在,同时知道代理的名字,但是不知道代理的是谁):
    普通代理模式类图
    public interface IGamePlayer {
      void login(String user, String password);
    
      void killBoss();
    
      void upgrade();
    }
    public class GamerProxy implements IGamePlayer {
      private IGamePlayer gamePlayer = null;
    
      public GamerProxy(String name) {
        // TODO Auto-generated constructor stub
        try {
            gamePlayer = new GamerPlayer(this, name);
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
      }
    
      @Override
      public void login(String user, String password) {
        // TODO Auto-generated method stub
        this.gamePlayer.login(user, password);
      }
    
      @Override
      public void killBoss() {
        // TODO Auto-generated method stub
        this.gamePlayer.killBoss();
      }
    
      @Override
      public void upgrade() {
        // TODO Auto-generated method stub
        this.gamePlayer.upgrade();
      }
    
    }
    
    public class GamerPlayer implements IGamePlayer {
    
      private String name = "";
    
      public <T extends GamerProxy> GamerPlayer(T t, String name) throws Exception {
        // TODO Auto-generated constructor stub
        if (t == null) {
            throw new Exception("不能创建对应的用户!");
        } else {
    
            this.name = name;
        }
    
      }
    
      @Override
      public void login(String user, String password) {
        // TODO Auto-generated method stub
        System.out.println(user + "" + name + "" + password);
      }
    
      @Override
      public void killBoss() {
        // TODO Auto-generated method stub
        System.out.println(name + "killBoss");
      }
    
      @Override
      public void upgrade() {
        // TODO Auto-generated method stub
        System.out.println(name + "upgrade");
      }
    
    }
    
    • 强制代理模式(只能通过真实角色指定的代理类才能访问,也就是说由真实角色管理代理角色,即高层模块new了一个真实的对象,但是返回的确是代理角色)
    强制代理模式类图
    public interface IGamePlayer {
      void login(String user, String password);
    
      void killBoss();
    
      void upgrade();
    
      IGamePlayer getProxy();
    }
    public class GamerProxy implements IGamePlayer {
      private IGamePlayer gamePlayer = null;
    
      public <T extends GamerProxy> GamerProxy(GamerPlayer gamerPlayer) {
        // TODO Auto-generated constructor stub
        this.gamePlayer = gamePlayer;
      }
    
      @Override
      public void login(String user, String password) {
        // TODO Auto-generated method stub
        this.gamePlayer.login(user, password);
      }
    
      @Override
      public void killBoss() {
        // TODO Auto-generated method stub
        this.gamePlayer.killBoss();
      }
    
      @Override
      public void upgrade() {
        // TODO Auto-generated method stub
        this.gamePlayer.upgrade();
      }
    
      @Override
      public IGamePlayer getProxy() {
        // TODO Auto-generated method stub
        return this;
      }
    
    }
    
    public class GamerPlayer implements IGamePlayer {
    
      private String name = "";
      private IGamePlayer proxy;
    
      public GamerPlayer(String name)  {
        // TODO Auto-generated constructor stub
        this.name = name;
      }
    
      @Override
      public void login(String user, String password) {
        // TODO Auto-generated method stub
        if (isProxy()) {
            System.out.println("login");
        } else {
            System.out.println("failed");
        }
      }
    
      @Override
      public void killBoss() {
    
      }
    
      @Override
      public void upgrade() {
    
      }
    
      @Override
      public IGamePlayer getProxy() {
        this.proxy = new GamerProxy(this);
        return this.proxy;
      }
    
      private boolean isProxy() {
        if (this.proxy == null) {
            return false;
        } else {
            return true;
        }
      }
    }
    
    public class Client {
      public static void main(String[] args) {
        IGamePlayer gamer = new GamerPlayer("zhangsan");
        IGamePlayer gamerProxy = gamer.getProxy();
        gamerProxy.login("", "");
      }
    }
    
    • 增加接口整合不同的任务(图片少一个getProxy( )方法,参照上图)
    多任务接口代理类整合
    public interface IProxy {
      // 计算费用
      public void count();
    }
    public class GamerProxy implements IGamePlayer,IProxy {
      private IGamePlayer gamePlayer = null;
    
      public <T extends GamerProxy> GamerProxy(GamerPlayer gamerPlayer) {
        // TODO Auto-generated constructor stub
        this.gamePlayer = gamePlayer;
      }
    
      @Override
      public void login(String user, String password) {
        // TODO Auto-generated method stub
        this.gamePlayer.login(user, password);
      }
    
      @Override
      public void killBoss() {
        // TODO Auto-generated method stub
        this.gamePlayer.killBoss();
      }
    
      @Override
      public void upgrade() {
        // TODO Auto-generated method stub
        this.gamePlayer.upgrade();
        this.count();
      }
    
      @Override
      public IGamePlayer getProxy() {
        // TODO Auto-generated method stub
        return this;
      }
      @Override
      public void count() {
        // TODO Auto-generated method stub
        System.out.println("总费用");
      }
    }
    
    • 动态代理:实现阶段不用关心代理谁,而在运行阶段才指定代理哪一个对象。(面向截面编程AOP的核心,被代理类实现必须实现)
    动态代理模式通用类图
    public interface IAdvice {
      // 通知方法
      public void exec();
    }
    public class BeforeAdvice implements IAdvice {
    
      @Override
      public void exec() {
        // TODO Auto-generated method stub
        System.out.println("前置通知");
      }
    
    }
    
    public interface Subject {
      // 业务操作
      public void doSomething(String str);
    }
    
    public class ResultSubject implements Subject {
    
      @Override
      public void doSomething(String str) {
        // TODO Auto-generated method stub
        System.out.println("具体实际的操作方法");
      }
    
    }
    
    public class DynamicProxy<T> {
      public static <T> T newProxyInstance(ClassLoader loader,Class<?>[] interfaces,InvocationHandler h){
        //寻找JoinPoint链接点,AOP框架使用元数据定义
        if(true){
            //执行前置通知
            (new BeforeAdvice()).exec();
        }
        //执行目标,并返回结果
        return (T)Proxy.newProxyInstance(loader, interfaces, h);
      }
    }
    public class SubjectDynamicProxy extends DynamicProxy {//根据不同的主题进行二次封装
      public static <T> T newProxyInstance(Subject subject){
        //获取ClassLoader
        ClassLoader loader = subject.getClass().getClassLoader();
        //获取接口数据
        Class<?>[] classes = subject.getClass().getInterfaces();
        //获取handler
        DemoInvocationHandler demoIH = new DemoInvocationHandler(subject);
        
        return newProxyInstance(loader, classes, demoIH);
      }
    }
    public class DemoInvocationHandler implements InvocationHandler {
      // 被代理的对象
      private Object target = null;
    
      // 通过构造函数传递一个对象
      public DemoInvocationHandler(Object object) {
        // TODO Auto-generated constructor stub
        this.target = object;
      }
    
      // 代理方法
      @Override
      public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
        // TODO Auto-generated method stub
        // 被代理的方法
        return method.invoke(this.target, args);
      }
    
    }
    
    public class Client {//两种方式
      public static void main(String[] args) {
        // 定义主题
        Subject subject = new ResultSubject();
        // 定义主题代理类
        Subject proxy = SubjectDynamicProxy.newProxyInstance(subject);
        proxy.doSomething("");
    
        ///////////////////////////////////////
    
        // 定义主题
        Subject subjectTwo = new ResultSubject();
        // 定义handler
        InvocationHandler iH = new DemoInvocationHandler(subjectTwo);
        // 定义主题代理类
        Subject proxyTwo = DynamicProxy.newProxyInstance(subjectTwo.getClass().getClassLoader(),
                subjectTwo.getClass().getInterfaces(), iH);
        proxyTwo.doSomething("");
      }
    }
    

    相关文章

      网友评论

          本文标题:<<设计模式之禅(第二版)>>——第十二

          本文链接:https://www.haomeiwen.com/subject/puxnyttx.html