Java 代理模式详解-2
上一篇 / 下一篇 2012-09-21 13:21:33 / 个人分类:Java
51Testing软件测试网:QvV'f]0v&]~$@4O
@Override
sP qNj8w-`0public void sendMessage(String msg) {
(M)UT*Lq'Sb0// TODO Auto-generated method stub
&k,Ii7Fs%G0System.out.println("SMS Message :" + msg+" sent !");51Testing软件测试网%nKGm yu|QY4[y
}
_Z\1X6L%h0}
;bPl,\M*\2Y*z0//动态代理类51Testing软件测试网;^}#mO| V3d
import java.lang.reflect.InvocationHandler;51Testing软件测试网J V;iY7xy} \]cV
import java.lang.reflect.Method;51Testing软件测试网"^d"F OGQ$r
public class DynamicMessageProxy implements InvocationHandler {51Testing软件测试网0CFk;ySO9v*sq
private static int count;51Testing软件测试网E n.tEzx8^"a.t
private MessageHandler msgHandler;
7l6g,u$E9Kb$zz0public DynamicMessageProxy(MessageHandler handler) {51Testing软件测试网Pq{{4IfO
msgHandler = handler;51Testing软件测试网8E)|y1iM1O |a
}
`/l#T#jym8T0@Override51Testing软件测试网%}sO Ym9A7J[w
public Object invoke(Object proxy, Method method, Object[] args)51Testing软件测试网G;r&jX1kW4\Y}N
throws Throwable {
(XaY }/oa0// TODO Auto-generated method stub51Testing软件测试网o1Y`7R(vL7xV6@
System.out.println("++++++++=============+++++++++");
/kV|8R6w7ok_ p0System.out.println("proxy:" + proxy.getClass());51Testing软件测试网7Jt$Bj @7g}{
System.out.println("method:" + method);
"I1h[W V#@[E0System.out.println("++++++++=============+++++++++");51Testing软件测试网?};JU,^
if (args != null && args.length == 1 && checkMessage((String) args[0])) {
K2}~(^%~_o0vU%R0count++;51Testing软件测试网,pw ZYiO
System.out.println("Message sent:" + count);
sG~(DCPZ5O0return method.invoke(msgHandler, args);51Testing软件测试网n K Ih5_4g\
}51Testing软件测试网.? ]K&@4}?7z,A
return null;
a/p.XZp5a9|@A0}51Testing软件测试网HnR D-jc!a\:o
private boolean checkMessage(String msg) {51Testing软件测试网'K M5q"QPBp^
return msg != null && msg.length() > 10;
5j&k2p.]0Yl0Ik l(jp0}51Testing软件测试网Eq'hK s/h
}
/_-}6j+O?sC h8]m&N
在上面的例子基础上,我们先添加一个通过短信来发送消息的处理类:51Testing软件测试网*W(`K4|5tG rd.r
^ly8?x_~+R5u051Testing软件测试网jb#R[ \(V7T.U q:J
public class SmsMessage implements MessageHandler {51Testing软件测试网EJgF:{/p O@Override
sP qNj8w-`0public void sendMessage(String msg) {
(M)UT*Lq'Sb0// TODO Auto-generated method stub
&k,Ii7Fs%G0System.out.println("SMS Message :" + msg+" sent !");51Testing软件测试网%nKGm yu|QY4[y
}
_Z\1X6L%h0}
;bPl,\M*\2Y*z0//动态代理类51Testing软件测试网;^}#mO| V3d
import java.lang.reflect.InvocationHandler;51Testing软件测试网J V;iY7xy} \]cV
import java.lang.reflect.Method;51Testing软件测试网"^d"F OGQ$r
public class DynamicMessageProxy implements InvocationHandler {51Testing软件测试网0CFk;ySO9v*sq
private static int count;51Testing软件测试网E n.tEzx8^"a.t
private MessageHandler msgHandler;
7l6g,u$E9Kb$zz0public DynamicMessageProxy(MessageHandler handler) {51Testing软件测试网Pq{{4IfO
msgHandler = handler;51Testing软件测试网8E)|y1iM1O |a
}
`/l#T#jym8T0@Override51Testing软件测试网%}sO Ym9A7J[w
public Object invoke(Object proxy, Method method, Object[] args)51Testing软件测试网G;r&jX1kW4\Y}N
throws Throwable {
(XaY }/oa0// TODO Auto-generated method stub51Testing软件测试网o1Y`7R(vL7xV6@
System.out.println("++++++++=============+++++++++");
/kV|8R6w7ok_ p0System.out.println("proxy:" + proxy.getClass());51Testing软件测试网7Jt$Bj @7g}{
System.out.println("method:" + method);
"I1h[W V#@[E0System.out.println("++++++++=============+++++++++");51Testing软件测试网?};JU,^
if (args != null && args.length == 1 && checkMessage((String) args[0])) {
K2}~(^%~_o0vU%R0count++;51Testing软件测试网,pw ZYiO
System.out.println("Message sent:" + count);
sG~(DCPZ5O0return method.invoke(msgHandler, args);51Testing软件测试网n K Ih5_4g\
}51Testing软件测试网.? ]K&@4}?7z,A
return null;
a/p.XZp5a9|@A0}51Testing软件测试网HnR D-jc!a\:o
private boolean checkMessage(String msg) {51Testing软件测试网'K M5q"QPBp^
return msg != null && msg.length() > 10;
5j&k2p.]0Yl0Ik l(jp0}51Testing软件测试网Eq'hK s/h
}
/_-}6j+O?sC h8]m&N