《Android 源码设计模式解析与实战》笔记之代理模式

前言

此笔记摘抄于《Android源码设计模式解析与实战》一书

代理模式的定义

为其他对象提供一种代理以控制对这个对象的访问。

代理模式的使用场景

当无法或不想直接访问某个对象或访问某个对象时存在困难时可以通过一个代理对象来间接访问,为了保证客户端的使用的透明性,委托对象与代理对象需要实现相同的接口

代理模式的简单实现

  • 静态代理
    //诉讼类接口
    public interface ILawsuit{
        //提交申请
        void submit();
        //进行举证
        void burden();
        //开始辩护
        void defend();
        //诉讼完成
        void finish();
    }
    //具体诉讼人(委托对象)
    public class XiaoMin implements ILawsuit{
        @override
        public void submit(){
            System.out.println("老板拖欠工资,特此申请仲裁");
        }
        @override
        public void burden(){
            System.out.println("这是合同书和过去一年的银行工资流水");
        }
        @override
        public void defend(){
            System.out.println("证据确凿,不需要再说什么了");
        }
        @override
        public void finish(){
            System.out.println("诉讼成功,判决老板即日起七天内结算工资");
        }
    }
    //代理律师(代理对象)
    public class Lawyer implements ILawsuit{
        private ILawsuit mLawsuit;//持有一个具体被代理者的引用

        public Lawyer(ILawsuit lawsuit){
            this.mLawsuit = lawsuit;
        }
        @override
        public void submit(){
            mLawsuit.submit();
        }
        @override
        public void burden(){
            mLawsuit.burden();
        }
        @override
        public void defend(){
            mLawsuit.defend();
        }
        @override
        public void finish(){
            mLawsuit.finish();
        }
    }
    //客户类
    public class Client {
        public static void main(Sting[] args) {
            ILawsuit xiaomin = new XiaoMin();
            ILawsuit lawyer = new Lawyer(xiaomin);

            lawyer.sunbit();
            lawyer.burden();
            lawyer.defend();
            lawyer.finish();
        }
    }
  • 动态代理
    //动态代理类
    public class DynamicProxy implements InvocationHandler {
        private Object obj;//被代理的引用

        pubic DynamicProxy(Object obj){
            this.obj = obj;
        }

        @override
        public Object invoke(Object proxy, Method method, Object[] args) throws Throwable{
            //调用被代理对象的方法
            Object result = method.invoke(obj,args);
            return result;
        }
    }
    //修改后的客户类
    public static void main(String[] args){
        Ilawsuit xiaomin = new XiaoMin();
        DynamicProxy proxy = new DynamicProxy(xiaomin);

        //获取被代理类XiaoMin的ClassLoader
        ClassLoader loader = xiaomin.getClass().getClassLoader();
        //动态构造一个代理律师
        ILawsuit  lawyer = (ILawsuit)Proxy.newProxyInstance(loader,new Class[] {ILawsuit.class},proxy);
        lawyer.sunbit();
        lawyer.burden();
        lawyer.defend();
        lawyer.finish();
    }

Android源码中的代理模式以及涉及到的重要概念

  • ActivityManagerProxy
  • ActivityManagerNative

Android中的Binder跨进程通信机制与AIDL

  • 四大模块:Binder Client、Binder server、Server Manager、Binder Driver
    原文作者:算法小白
    原文地址: https://juejin.im/entry/58eaf5a6ac502e006c439cf3
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞