代理模式(接口和子类)
来源:互联网 发布:淘宝网手机网页版 编辑:程序博客网 时间:2024/06/05 01:02
接口:
package com.alice.Proxy;
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
public class Client{
public static void main(String[] args) {
//2.改final
final Springbrothder sb=new Springbrothder();
/* Proxy proxy=(Proxy) Proxy.newProxyInstance(sb.getClass().getClassLoader(), sb.getClass().getInterfaces(), new InvocationHandler() {
//1.必须是Hummer 类
@Override
public Object invoke(Object proxy, Method method, Object[] args)
throws Throwable {
// TODO Auto-generated method stub
return null;
}
});
*/
Hummer proxy=(Hummer) Proxy.newProxyInstance(sb.getClass().getClassLoader(), sb.getClass().getInterfaces(), new InvocationHandler() {
//必须是Hummer 类
@Override
public Object invoke(Object proxy, Method method, Object[] args)
throws Throwable {
if("sing".equals(method.getName())){
double money=(Double)args[0];
if(money>=10000)
return method.invoke(sb, money/2);
}
if("dance".equals(method.getName())){
method.invoke(sb, 20000);
}
return null;
}
});
proxy.sing(10); //3.调用 proxy
proxy.sing(10000);
}
}
子类:
package com.alice.Proxy2;
import java.lang.reflect.Method;
import net.sf.cglib.proxy.Enhancer;
import net.sf.cglib.proxy.InvocationHandler;
public class Client {
public static void main(String[] args) {
final Springbrothder sb=new Springbrothder();
Springbrothder client=(Springbrothder)Enhancer.create(Springbrothder.class,new InvocationHandler() {
@Override
public Object invoke(Object proxy, Method method, Object[] args)
throws Throwable {
if("sing".equals(method.getName())){
double money=(Double)args[0];
if(money>=10000)
return method.invoke(sb, money/2);
}
if("dance".equals(method.getName())){
method.invoke(sb, 20000);
}
return null;
}
});
client.sing(10); //3.调用 proxy
client.sing(10000);
}
}
- 代理模式(接口和子类)
- 代理模式(基于接口)
- 接口(工厂模式&代理模式)
- 静态代理模式(java),接口练习
- 代理模式(接口隔离)
- 接口代理模式
- 接口--代理设计模式
- JAVA-三种代理模式(静态、动态、子类/Cglib代理)
- 设计模式- 代理模式 (静态代理 和 动态代理)
- 接口-工厂模式-代理模式
- 抽象类和接口的区别(里面有适配器设计、工厂设计、代理设计模式)
- 抽象类和接口的区别(里面有适配器设计、工厂设计、代理设计模式)
- iOS设计模式学习 (二) OC协议之接口继承和代理设计
- 代理模式和代理模式
- 代理模式(静态代理和动态代理)
- Spring的代理模式(静态代理和动态代理)
- Spring的代理模式(静态代理和动态代理)
- java的代理模式(动态代理和静态代理)
- canvas的translate、scale、rotate等方法
- 自旋锁的应用
- cookie学习笔记2
- 电脑族最容易得什么疾病?
- 【第二章】证券交易程序
- 代理模式(接口和子类)
- 如何mount ext4 分区
- androd之绘制文本(FontMetrics)
- 显式转换 id和void *
- 【第二章】证券交易程序_整理
- Python字符与数字的相互转换
- HDU 2281 Square Number (Pell方程&数论)
- 【第二章 第一节】
- 1066. Root of AVL Tree (25)