`

代理模式

阅读更多
代理模式三大要素:抽象角色(真实角色和代理角色关系),代理角色(中间人),真实角色(供应方)



一、静态代理

package org.mars.design.proxy.staticMode;



/**

* Title: base<br>

* Description: It's an abstract role,just like a deal between the proxy role and real role.<br>

* Copyright: Copyright (c) 2011 <br>

* Create DateTime: Jun 24, 2011 6:10:23 PM <br>

* @author wangmeng

*/

public abstract class AbstractRole {



/***

* An action, just like a request.

*/

abstract void response();

}
---------------------------------------------------------------
package org.mars.design.proxy.staticMode;

/**
* Title: base<br>
* Description: It's a proxy role.<br>
* Copyright: Copyright (c) 2011 <br>
* Create DateTime: Jun 24, 2011 6:30:00 PM <br>
* @author wangmeng
*/
public class ProxyRole extends AbstractRole{
/**
* Members who need agent.
*/
private RealRole realRole;
public ProxyRole(){}

/**
* It's a action of proxy's own, just a member method.
*/
private void declare(){
System.out.println("If you want build a family, just tell me, since I'm a matchmaker.");
}
@Override
void response() {
if(null == realRole){
realRole = new  RealRole();
}
this.declare();
//Deal the handler of the real role.
realRole.response();
}
}
-------------------------------------------------------------
package org.mars.design.proxy.staticMode;

/**
* Title: base<br>
* Description: It's a real role.<br>
* Copyright: Copyright (c) 2011 <br>
* Create DateTime: Jun 24, 2011 6:31:58 PM <br>
* @author wangmeng
*/
public class RealRole extends AbstractRole{

@Override
void response() {
System.out.println("When a man loves me forever, I will get married with him.");
}
}
------------------------------------------------
需求方样例:
package org.mars.design.proxy.staticMode;

/**
* Title: base<br>
* Description: It's demander, which maybe need something to be solved with someone's help.<br>
* Copyright: Copyright (c) 2011 <br>
* Create DateTime: Jun 24, 2011 6:43:39 PM <br>
* @author wangmeng
*/
public class Demander {

public static void main(String[] args){
AbstractRole role = new ProxyRole();
/**
* Coming up with a demand.
*/
System.out.println("I want to marry a girl and build a warm family with her.");
/**
* Let agent do it.
*/
role.response();
}
}





二、动态代理

package org.mars.design.proxy.dynamicMode;



/**

* Title: base<br>

* Description: In the dynamic mode, the abstract role is a interface.<br>

* Copyright: Copyright (c) 2011 <br>

* Create DateTime: Jun 24, 2011 7:21:35 PM <br>

* @author wangmeng

*/

public interface AbstractRole {



/**

* Actions need to be done.

*/

void response();

}
----------------------------------------------------------
关键点:动态代理动态调用
package org.mars.design.proxy.dynamicMode;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;

/**
* Title: base<br>
* Description: Dynamic Proxy.<br>
* Copyright: Copyright (c) 2011 <br>
* Create DateTime: Jun 24, 2011 7:29:27 PM <br>
* @author wangmeng
*/
public class ProxyRole implements InvocationHandler{

/**
* The object which would be agent.
*/
private Object realRole;
public ProxyRole(){}
/**
* Rebuilding the constructor in order to pass the object into the dynamic agent.
* @param obj
*/
public ProxyRole(Object object){
this.realRole = object;
}
/**
* That'll invoked by the JVM automatically.
*/
public Object invoke(Object proxy, Method method, Object[] args)
throws Throwable {
/**Other methods passed in can be here.*/
/**Do the method of the real role.*/
method.invoke(this.realRole, args);

/**Other methods passed in can be here too.*/
return null;
}

}
------------------------------------------------------------
package org.mars.design.proxy.dynamicMode;

/**
* Title: base<br>
* Description: It's also a real role.<br>
* Copyright: Copyright (c) 2011 <br>
* Create DateTime: Jun 24, 2011 7:25:15 PM <br>
* @author wangmeng
*/
public class RealRole implements AbstractRole {

public void response() {
System.out.println("When a man loves me forever, I will get married with him.");
}

}
---------------------------------------------------------------------
需求方样例:
package org.mars.design.proxy.dynamicMode;

import java.lang.reflect.Proxy;

/**
* Title: base<br>
* Description: It's a demander.<br>
* Copyright: Copyright (c) 2011 <br>
* Create DateTime: Jun 24, 2011 8:00:05 PM <br>
* @author wangmeng
*/
public class Demander {

public static void main(String[] args){
RealRole realRole = new RealRole();
ProxyRole handler = new ProxyRole(realRole);
Class<?> cls = realRole.getClass();
AbstractRole role = (AbstractRole) Proxy.newProxyInstance(cls.getClassLoader(),cls.getInterfaces(),handler);
role.response();
}
}

注意动态代理模式和静态代理的区别:原理一样,实现方式不同,动态代理模式关键在于动态代理根据类加载器和接口组来动态实现所传入对象的方法调用。
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics