- 浏览: 532999 次
- 性别:
- 来自: 上海
文章分类
- 全部博客 (231)
- 一个操作系统的实现 (20)
- 汇编(NASM) (12)
- Linux编程 (11)
- 项目管理 (4)
- 计算机网络 (8)
- 设计模式(抽象&封装) (17)
- 数据结构和算法 (32)
- java基础 (6)
- UML细节 (2)
- C/C++ (31)
- Windows (2)
- 乱七八糟 (13)
- MyLaB (6)
- 系统程序员-成长计划 (8)
- POJ部分题目 (10)
- 数学 (6)
- 分布式 & 云计算 (2)
- python (13)
- 面试 (1)
- 链接、装载与库 (11)
- java并行编程 (3)
- 数据库 (0)
- 体系结构 (3)
- C++ template / STL (4)
- Linux环境和脚本 (6)
最新评论
-
chuanwang66:
默默水塘 写道typedef void(*Fun)(void) ...
C++虚函数表(转) -
默默水塘:
typedef void(*Fun)(void);
C++虚函数表(转) -
lishaoqingmn:
写的很好,例子简单明了,将观察者模式都表达了出来。
这里是ja ...
观察者模式——Observer
一、概念和注意点:
Once you write a dynamic proxy class, you can use it to wrap any object, so
long as the object is an instance of a class that implements an interface that
declares the behavior you want to intercept.
动态代理可以用来包裹任何类,拦截这个类实现的接口中的某些方法,在执行这些方法
的前后加入适当动作。
Dynamic proxies in Java let you wrap an object with a proxy that intercepts
the object's calls and that can add behavior before or after passing the call
along.This lets you create reusable behavior that you can drop in on an arbitrary
object, in a fashion similar to aspect-oriented programming.
在被拦截的方法前后加入适当操作——提高了“可重用性”,类似AOP的做法。
In AOP, an aspect is a combination of aspect——code that you want to drop in
——and point cuts——definitions of execution points in your code where you want
the drop-in code to run.
理解AOP中的两个概念:
1)aspect——可重用代码
2)point cuts——执行点
Sam的总结: Dynamic Proxies就是一个拦截器,在某些操作的前后加上可重用代码。记住
Java中实现动态代理需要实现的接口和方法。
二、代码
动态代理的使用ShowDynamicProxy.java
原来的对象HashSet s 被代理后,可以转化为HashSet的(任意)父接口Set,在执行Set中声明的任意方法时,会在外围包裹拦截动作^_^
package app.proxy.dynamic; import java.util.HashSet; import java.util.Set; import com.oozinoz.firework.Firecracker; import com.oozinoz.firework.Sparkler; import com.oozinoz.utility.Dollars; /** * Show an example of using a dynamic proxy to add behavior to an object. In * this example, we add an element of impatience, complaining if any method * takes too long to execute. */ public class ShowDynamicProxy { public static void main(String[] args) { Set s = new HashSet(); s = (Set) ImpatientProxy.newInstance(s); /* 代理后可以强制转化为原来类实现的任何接口类型 */ s.add(new Sparkler("Mr. Twinkle", new Dollars(0.05))); s.add(new BadApple("Lemon")); s.add(new Firecracker("Mr. Boomy", new Dollars(0.25))); System.out.println("The set contains " + s.size() + " things."); } }
动态代理类ImpatientProxy.java
1)动态代理类必须实现InvocationHandler接口,以及该接口中的invoke(...)方法;
2)背下来:静态实例化方法newInstance(...)的实现。
package app.proxy.dynamic; import java.lang.reflect.*; /** * This class is an example of a dynamic proxy. Instances of this class wrap a * proxied object. This class simply forwards calls to the object it wraps. * However, if any method takes a long time to execute, this class will print a * warning message. */ public class ImpatientProxy implements InvocationHandler { private Object obj; /** * Construct a dynamic proxy around the given object. * @param obj the object to wrap * @return the proxy */ public static Object newInstance(Object obj) { ClassLoader loader = obj.getClass().getClassLoader(); /* 获取“被代理类”的加载器 */ Class[] classes = obj.getClass().getInterfaces(); /* 获取“被代理类”实现的接口 */ return Proxy.newProxyInstance(loader, classes, new ImpatientProxy(obj)); /* 返回Object类型的代理 */ /* * 注意:The returned object will implement all the interfaces that the wrapped * object's class implements. We can cast the returned object to any of these * interfaces. */ } private ImpatientProxy(Object obj) { this.obj = obj; } /** * The method that all dynamic proxies must implement. This dynamic proxy * complains when a method takes a long time to return. */ public Object invoke(Object proxy, Method m, Object[] args) throws Throwable { Object result; long t1 = System.currentTimeMillis(); result = m.invoke(obj, args); long t2 = System.currentTimeMillis(); if (t2 - t1 > 10) { System.out.println("> It takes " + (t2 - t1) + " millis to invoke " + m.getName() + "() with"); for (int i = 0; i < args.length; i++) System.out.println("> arg[" + i + "]: " + args[i]); } return result; } }
发表评论
-
(第十章)一个xml解析器和构造器
2013-03-10 16:40 897本章的前两节“10.1 状态机”、“10.2 ... -
享元模式——Flyweight
2012-02-17 13:10 1014享元模式——Flyweig ... -
工厂方法和抽象工厂——Factory Method & Abstract Factory
2012-01-04 17:14 2049一、使用抽象工厂和工厂方法 Factory Me ... -
单例模式——Singleton
2012-01-04 17:08 975public class Singleton { ... -
观察者模式——Observer
2012-01-04 16:25 1280观察者模式—— Observer ... -
适配器模式——Adaptor(Adapter)
2012-01-01 18:23 1367适配器模式 —— Adapto ... -
状态模式——State (更好的实现状态机)
2011-12-28 14:10 655801. 概述 The intent o ... -
装饰者模式——Decorator
2011-12-25 11:11 1140装饰者模式—— Decorator ... -
组合模式——Composite
2011-12-24 14:27 9731. Composite 定义 : ... -
构造者模式——Builder
2011-08-10 13:59 1030构造者模式——Builder 本文是《Java设计模 ... -
责任链模式——Chain of Responsibility
2011-08-10 11:26 892一、总结《Java设计模式》Chapter12 Chain o ... -
代理模式Remote Proxies(三、RMI)
2011-08-01 09:51 1665因为我本科毕业设计中大量采用RMI实现分布式,且使用了Ecli ... -
代理模式Image Proxies(二、最朴素实现)
2011-07-31 11:55 955在前面《 代理模式Image Proxies(一、最朴素实现) ... -
命令模式——Command
2011-06-10 10:31 912偷懒一下,直接用JavaEye上chjavach老兄的文章了, ... -
代理模式Image Proxies(一、最朴素实现)
2011-06-03 09:27 1022A Classic Example: Image Prox ... -
策略模式——strategy
2011-06-02 12:36 847Strategy Pattern ...
相关推荐
NULL 博文链接:https://chuanwang66.iteye.com/blog/1069268
NULL 博文链接:https://chuanwang66.iteye.com/blog/1137037
NULL 博文链接:https://chuanwang66.iteye.com/blog/1136589
学习网络爬虫难免遇到使用代理的情况,下面介绍一下如何使用requests设置代理: 如果需要使用代理,你可以通过为任意请求方法提供 proxies 参数来配置单个请求: import requests proxies = { ...
Laravel开发-laravel-valid-proxies Laravel有效代理
代理ip池(自动采集ip地址 从网站获取ip地址,检测是否可用,并实时更新,存入数据库中) 代理ip池(自动采集ip地址 从网站获取ip地址,检测是否可用,并实时更新,存入数据库中)
资源分类:Python库 所属语言:Python 资源全名:proxies_l-0.0.3.tar.gz 资源来源:官方 安装方法:https://lanzao.blog.csdn.net/article/details/101784059
Koa代理 middlware HTTP代理 由提供支持。 安装 $ npm install koa-proxies --save 选件 http-proxy活动 options . events = { error ( err , req , res ) { } , proxyReq ( proxyReq , req , res ) { } , ...
* TCP: tunnelling of connections through HTTP proxies * TCP: speed limiting, data logging, etc * TCP: passwording of tunnels * SMTP: antispam (DNS/RBL & greylisting) * SMTP: antivirus (using ...
代理搜寻器 在互联网上抓取免费的HTTP和SOCKS代理编译sbt clean assembly用法crawl [pluginClassName]* OutputFilecheck proxies.json valid_proxies.jsonfilter valid_proxies.json <HTTP> output.json例如: 搜寻...
代理人 Tokio堆栈的代理协议。 用法 查看服务器和客户端示例的examples/目录。 地位 http 服务器端 客户端 袜子5 服务器端 客户端
设置可信代理允许在负载均衡器或缓存等反向代理后面时正确生成 URL、重定向、会话处理和登录 Laravel。 Laravel 5.5+ 自带这个包。 如果您使用的是 Laravel 5.5 或更高版本,则无需单独将其添加到您的项目中。 设置...
Trusted Proxies GeoSet工具栏允许您快速连接到Trusted Proxies帐户上的任何代理服务器。 使用“受信任的代理” GeoSet工具栏,您可以在“受信任的代理”帐户上的所有地理位置代理之间无缝切换。 还没有Trusted ...
AspectSharp是DotNet下的免费AOP框架,它以Dynamic Proxies和XML作为配置文 件。
'''01_普通代理示例.py''' import requests url = "http://www.baidu.com/" proxies = {"http":"http://183.129.207.82:11597"} headers = {"User-Agent":"Mozilla/5.0"} res = requests.get(url,proxies=proxies,...
'''02_私密代理示例.py''' import requests url = "http://httpbin.org/get" headers = {"User-Agent":"Mozilla/5.0"} proxies = {"http":"http://309435365:szayclhp@123.206.119.108:16817"} res = requests.get...
A simple source code to explain how to scrape proxies in Delphi.
java动态代理,j-dynproxies-source
weird_proxies:反向代理备忘单