应用场景:不改变被代理对象的代码,新建动态代理类,并在代理类中增加相应的处理代码。
在目前的java开发包中包含了对动态代理的支持,但是其实现只支持对接口的的实现。
其实现主要通过是java.lang.reflect.proxy类和java.lang.reflect.invocationhandler接口。
import java.lang.reflect.invocationhandler;
import java.lang.reflect.method;
import java.lang.reflect.proxy;
//动态代理设计模式: invocationhandler
//dobefore(): 在代理调用被代理方法前执行
//doafter(): 在代理类调用被代理方法后执行
public class dyn {
/** 获取代理对象 */
public static ihelloworld gethellowoldproxy() {
ihelloworld hello = new helloworld();
invocationhandler handler = new dynhandler(hello);
classloader cl = hello.getclass().getclassloader();
ihelloworld helloproxy = (ihelloworld)proxy.newproxyinstance(cl, new class[]{ihelloworld.class}, handler);
return helloproxy;
}
public static void main(string[] args) {
ihelloworld proxy = gethellowoldproxy();
//调用hi()之前,调用dobefore(),hi()运行之后,调用doafter()
string c = proxy.hi("dog");
system.out.println(c);
}
}
class dynhandler implements invocationhandler{
//被代理者
private class cls = null;
//被代理的实例
private ihelloworld obj = null;
//我要代理谁
public dynhandler(ihelloworld obj){
this.obj = obj;
}
//调用被代理的方法
/**
* args: proxy.hi传进来的参数
*/
public object invoke(object proxy, method method, object[] args) throws throwable {
system.out.println("invoke() ..");
dobefore(args);
object result = method.invoke(this.obj, args);
doafter(args);
return result; //返回代理对象
}
public void dobefore(object[] args){
system.out.println("dobefore() .." (string)args[0]);
}
public void doafter(object[] args){
system.out.println("doafter() ..");
}
}
//被代理类
class helloworld implements ihelloworld{
public string hi(string name) {
system.out.println("hi() ..");
return "hello " name;
}
}
//exception in thread "main" java.lang.illegalargumentexception: cn.util.helloworld is not an interface
interface ihelloworld{
public string hi(string name);
}
//2.代理模式
public class dynamicpattern {
private hello h;
public dynamicpattern(hello h){
this.h=h;
}
public string sayhi(string name){
string str = h.hi(name);
system.out.println("sayhi() ..");
return str;
}
public static void main(string args[]){
dynamicpattern d = new dynamicpattern(new hello());
string s = d.sayhi("jake");
system.out.println(s);
}
}
class hello{
public string hi(string name) {
system.out.println("hi() ..");
return "hello " name;
}
}
相关推荐
java设计模式——代理设计模式(静态代理和动态代理) 各种情况例子源码
详细而又简单的讲述了java动态代理设计模式
java设计模式之代理模式(结构)java设计模式之代理模式(结构)java设计模式之代理模式(结构)java设计模式之代理模式(结构)java设计模式之代理模式(结构)
java设计模式之代理模式实例
java常用设计模式及jdk与cglib实现动态代理区别(源码) /** * 使用cglib动态代理 * @author * */ public class bookfacadeproxy implements methodinterceptor{ private object target; @override public...
null 博文链接:https://871656094.iteye.com/blog/2355335
java 经典设计模式讲解以及项目实战 设计模式简介:主要介绍各种设计模式的概念和运用场景等 设计模式综合运用:主要是笔者在实际工作中运用到的一些设计模式综合运用事例的提炼 spring设计模式简介:主要是讲述...
java 设计模式 代理模式介绍,含源码
总体来说设计模式分为三大类: 创建型模式,共五种:工厂方法模式、抽象工厂模式、单例模式、建造者模式、原型模式。 结构型模式,共七种:适配器模式、装饰器模式、代理模式、外观模式、桥接模式、组合模式、享元...
用myeclipse开发的java设计模式课程设计,课题内容为西瓜工厂,包含四个设计模式,单例模式、代理模式、建造者模式和抽象工厂模式,有较好的图形界面,文档内附有完整代码,适用于新手学习。
设计模式主要分为三大类: 1.创建型模式:工厂模式、抽象工厂模式、单例模式、建造者模式、原型模式。 2.结构型模式:适配器模式、桥接模式、装饰模式、组合模式、外观模式、享元模式、代理模式。 4.行为型模式:...
java动态代理设计模式中融入抗疫元素的课程思政教学
java设计模式(工厂模式,动态代理,责任链模式……)实例源码
java设计模式视频教程-代理模式, 深层了解java的设计模式
设计模式之proxy(代理) 设计模式之adapter(适配器) 设计模式之composite(组合) 设计模式之decorator(油漆工) 设计模式之bridge 设计模式之flyweight(享元) 行为模式: 设计模式之template 设计模式之memento(备忘机制...
java代码积累:并发 设计模式 数据结构 使用容器 实用 类 基础知识 并发性 演示线程的生命周期 生产者-消费者 设计模式参考《大话设计模式》 工厂简单模式 创造型模式 工厂方法模式 抽象工厂模式 原型模式 建造者...
java设计模式【之】jdk动态代理【源码】【场景:帮爸爸买菜】.rar /** * 代理模式 * 在开发者的角度来看,创建一个代理对象,提供给用户使用,避免用户直接访问真正的对象 * 在用户角度来看,就是普通的类方法...
java模式设计-代理模式之动态代理.ppt
目录: 前 言 第一部分 大旗不挥,谁敢冲锋——热身篇 第1章 单一职责原则 1.1 我是“牛”类,我可以担任多职吗 1.2 绝杀技,打破你的传统思维 1.3 我单纯,所以我快乐 1.4 最佳实践 ...附录:23个设计模式
设计模式之代理模式java实现和类设计图,包括静态代理和动态代理