`

class proxy

阅读更多
1.Proxy.newProxyInstance(source.getClass().getClassLoader(), source.getClass().getInterfaces(), myInvoHandler)


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

class MyInvocationHandler implements InvocationHandler{
	
	private Object source ;
	
	public MyInvocationHandler(Object source){
		this.source = source ;
	}
	
	@Override
	public  Object invoke(Object proxy, Method method, Object[] args)
			throws Throwable {
//		System.out.println(proxy);// StackOverflowError course by recurse
		System.out.println(proxy.getClass());//proxy-class,not source-class
		if(null!=args){
			for(Object param : args){
				System.out.println(param);//method-input-param
			}
		}
		System.out.println("u can do special things before method-execute");
		Object obj =  method.invoke(source, args);//Point:source,not proxy. proxy's meaning?
		System.out.println("u can do special things after method-execute");
		return obj ;
	}
	
}


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

public class ClassProxyTest {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		PojoWithoutInterface pojo = new PojoWithoutInterface();
		pojo.setName(1111);
		Object pojoProxy = ClassProxyTest.getProxyInstance(pojo);
//		pojo = (PojoWithoutInterface) pojoProxy ;// class cast exception
		System.out.println(pojoProxy);
		Object pojoProxy1 = ClassProxyTest.getProxyInstance(pojo);// not equals,new instance
		System.out.println(pojoProxy.equals(pojoProxy1));//object 's method should be proxy
		
		ClassImplInterface impl = new ClassImplInterface() ;
		Object implProxy = ClassProxyTest.getProxyInstance(impl) ;
		InterfaceA interA = (InterfaceA) implProxy ;
		interA.methodA() ;
	}

	public static Object getProxyInstance(Object source){
		InvocationHandler myInvoHandler = new MyInvocationHandler(source) ;
		Object proxy = Proxy.newProxyInstance(source.getClass().getClassLoader(), source.getClass().getInterfaces(), myInvoHandler) ;
		Method[] publicMethods = proxy.getClass().getMethods() ;// proxy only have source's interface-method,not all-method
		if(null!=publicMethods){
			for(Method mthd : publicMethods){
				System.out.println(mthd.getName());
			}
		}
		return proxy ;
	}
	
}


class ClassImplInterface implements InterfaceA,InterfaceB{
		@Override
		public void methodA() {
			System.out.println("methodA execute");
		}
		@Override
		public void methodB() {
			System.out.println("methodB execute");
		}
		
		public void methodC(){//proxy can not execute
			System.out.println("self-method execute");
		}
	}


public class PojoWithoutInterface {
	private int name ;
	public int getName() {
		return name;
	}
	public void setName(int name) {
		this.name = name;
	}
}


public interface InterfaceA {

void methodA() ;

}

public interface InterfaceB {

void methodB() ;

}
分享到:
评论

相关推荐

    C++ Proxy模式

    class Proxy : public SubjectInterface { private: std::unique_ptr<SubjectInterface> real_subject; public: Proxy() : real_subject(std::make_unique()) {} void operation() override { // 在调用目标...

    设计模式C++学习之代理模式(Proxy)

    class Proxy : public Subject { private: RealSubject* _realSubject; public: Proxy() : _realSubject(new RealSubject()) {} ~Proxy() { delete _realSubject; } void request() override { // 在调用...

    代理模式(Proxy Pattern)完整示例代码

    class Proxy implements Target { private Target realTarget; public Proxy(Target realTarget) { this.realTarget = realTarget; } @Override public void doSomething() { before(); realTarget....

    代理模式 proxy-learn.rar

    public class ProxyService implements Service { private Service realService; public ProxyService(Service realService) { this.realService = realService; } @Override public void doSomething() { ...

    fluent-hc-4.2.2.jar用于设置代理

    public class proxy { public static void main(String[] args) throws Exception { HttpHost entry = new HttpHost("43.248.99.36", 62084); String resp= Executor.newInstance() .auth(entry, "fd", "123")//...

    JAVA反射 代理 PPT

    class DynamicProxy implements InvocationHandler { private Service target; public DynamicProxy(Service target) { this.target = target; } @Override public Object invoke(Object proxy, Method ...

    类的反射机制 常用的方法(简单明确)

    - **创建代理对象**:使用`ClassProxy`类创建代理对象,这里的`ClassProxy`类需要实现通过反射创建类实例的功能。 - **执行初始化方法**:如果成功创建了类的实例,则调用`init`方法,并传入`HttpServletRequest`...

    实例讲解Java设计模式编程中如何运用代理模式共3页.pd

    class Proxy implements Target { private Target target; public Proxy(Target target) { this.target = target; } @Override public void doSomething() { before(); target.doSomething(); after(); ...

    16-Java代理模式的学习笔记1

    class Proxy implements Programmer { private Programmer programmer; public Proxy(Programmer programmer) { this.programmer = programmer; } @Override public void develop() { bookTicket(); ...

    java静态代理、动态代理、装饰设计模式

    public class ProxyService implements Service { private Service realService; public ProxyService(Service realService) { this.realService = realService; } @Override public void doSomething() { ...

    java 代理demo

    public class ProxyService implements MyService { private MyService realService; public ProxyService(MyService realService) { this.realService = realService; } @Override public void doSomething...

    设计模式专题之(六)代理模式---设计模式代理模式示例代码(python--c++)

    class Proxy: def __init__(self): self._real_subject = RealSubject() def request(self): # 在调用真实对象之前可以进行额外操作 print("Proxy is preprocessing.") self._real_subject.request() # 在...

    ABAP 调用ABAP PROXY

    DATA: lo_proxy TYPE REF TO zcl_my_proxy_class. CREATE OBJECT lo_proxy. lo_proxy->set_parameter( 'PARAM1', 'Value1' ). lo_proxy->set_parameter( 'PARAM2', 'Value2' ). TRY. DATA(lr_result) = lo_proxy-...

    java代理模式

    class ProxyService implements Service { private Service realService; public ProxyService(Service realService) { this.realService = realService; } @Override public void doSomething() { before...

    Java代理示例代码

    public class ProxyService implements Service { private Service realService; public ProxyService(Service realService) { this.realService = realService; } @Override public void doSomething() { ...

    JAVA设计模式之代理模式实例

    class Proxy implements Target { private Target realTarget; public Proxy(Target realTarget) { this.realTarget = realTarget; } @Override public void doSomething() { before(); realTarget....

    委托和代理示例代码-个人独创

    例如,你可能有一个基础类`BaseClass`,然后创建一个代理类`ProxyClass`,该类继承自`BaseClass`并在其上添加额外的功能。 ```csharp public class BaseClass { public virtual void Execute() { Console....

    Java中的代理模式Demo

    class Proxy implements Target { private Target realTarget; public Proxy(Target realTarget) { this.realTarget = realTarget; } @Override public void doSomething() { before(); realTarget....

    Java代理学习

    public class ProxyService implements Service { private Service realService; public ProxyService(Service realService) { this.realService = realService; } @Override public void doSomething() { ...

Global site tag (gtag.js) - Google Analytics