标签:动态代理 sign 接口 this public lib col 扩展 family
package com.design.design.proxy.staticproxy; /** * @author: GuanBin * @date: Created in 下午11:52 2019/7/20 */ public interface ITeacherDao { /** * 授课的方法 */ void teach(); }
package com.design.design.proxy.staticproxy; /** * @author: GuanBin * @date: Created in 下午11:56 2019/7/20 */ public class TeacherDao implements ITeacherDao { @Override public void teach() { System.out.println("*****老是正在授课*****"); } }
3.被代理对象传入代理类的构造方法中,在代理类中实现的teach方法中,除了执行被代理类的teach方法外,还可以执行其他的业务逻辑操作
package com.design.design.proxy.staticproxy; /** * @author: GuanBin * @date: Created in 下午11:58 2019/7/20 */ public class TeacherDaoProxy implements ITeacherDao { private ITeacherDao target; public TeacherDaoProxy (ITeacherDao target) { this.target=target; } @Override public void teach() { System.out.println("开始代理"); target.teach(); System.out.println("提交代理"); } }
4.Client中获取代理对象,并执行被代理类的teach方法
package com.design.design.proxy.staticproxy; /** * @author: GuanBin * @date: Created in 上午12:05 2019/7/21 */ public class Client { public static void main(String[] args) { //创建目标对象(被代理对象) TeacherDao teacherDao = new TeacherDao(); //创建代理对象,同时将被代理对象传递给代理对象 TeacherDaoProxy teacherDaoProxy = new TeacherDaoProxy(teacherDao); //通过代理对象,调用到被代理对象的方法 //即:执行的是代理对象的方法,代理对象再去调用目标对象的方法 teacherDaoProxy.teach(); } }
标签:动态代理 sign 接口 this public lib col 扩展 family
原文地址:https://www.cnblogs.com/guanbin-529/p/11219944.html