Design pattern of agency mode (dynamic agency)

The last article introduced the static proxy of the proxy mode of the design pattern . In actual programming, the static proxy model is well understood. However, dynamic agents are usually used more often. The main reason is that it doesn't matter which agent is in the implementation phase, but which agent is specified in the operation phase. The idea of ​​AOP-oriented programming in the famous spring framework in java is based on dynamic proxy.
Speaking of dynamic proxy, I have to say an interface InvocationHandler, there is an introduction in the java API documentation.
Of InvocationHandler IS The interface Implemented by The Invocation Handler of A Proxy instance.
Each Proxy instance has AN Associated Invocation Handler. The When A Method IS the Invoked ON A Proxy instance, The Method Invocation IS encoded and dispatched to The Invoke Method of ITS Invocation Handler.
Substantially Meaning: each instance of the proxy class is associated with a handler. When we call a method through the proxy object, the call of this method will be forwarded to the invoke method of the InvocationHandler interface.

Specific code implementation: Like the static proxy method, we created an interface:

public interface ICompany {
	public void signContract(String companyName);
}

This interface will be implemented by the real implementation class Company:

public class Company implements ICompany {

	@Override
	public void signContract(String companyName) {
		// TODO Auto-generated method stub
		System.out.println("我们签约了"+companyName);
	}
}

As mentioned above, every dynamic proxy object must be associated with a handler, and this handler is the implementation class of InvocationHandler:

public class ProxyCompanyHandler implements InvocationHandler {
	private ICompany company;
	
	public ProxyCompanyHandler(ICompany company){
		this.company = company;
	}
	
	@Override
	public Object invoke(Object proxy, Method method, Object[] arg2) throws Throwable {
		// TODO Auto-generated method stub
		//从侧面切入从而达到扩展的效果的编程,就是面向切面编程(AOP Aspect Oriented Programming)。
	  //AOP并不是新技术,而是相对于面向对象编程的一种新的编程思想。在日志,事务,权限等方面使用较多
		if(method.getName().equals("signContract")){
			System.out.println("这里我们可以拦截方法的执行,我可以动态代理这个对象");
			method.invoke(company, arg2);
		}
		return method.invoke(company, arg2);
	}
}
}

How to create a dynamic proxy instance? Create a dynamic proxy instance through the Proxy.newProxyInstance method.

public class ProxyMain {

	public static void main(String[] args) {
		ICompany tencent = new Company();
		ProxyCompanyHandler handler = new ProxyCompanyHandler(tencent);
		ICompany tencentProxy = (ICompany) Proxy.newProxyInstance(tencent.getClass().getClassLoader(),tencent.getClass().getInterfaces(), handler);
		//此处便实现了对 Company的动态代理。并且生成了代理对象 tencentProxy。如果配合java反射的思想。可以实现很多有意思的代理
		tencentProxy.signContract("阿里巴巴");
	}
}

The final execution result is:
Dynamic proxy graph

Actually: If you cooperate with java reflection, you can proxy some private objects. For example, in android, you can intercept the startup of android and activity through a proxy. It is possible to start activities that are not registered in the list by means of [deception]. This is also an important idea of ​​the plug-in framework. All in all, dynamic agents have been widely used in some excellent frameworks. This design pattern also brings us great convenience in implementing certain functions.

Guess you like

Origin blog.csdn.net/xu_coding/article/details/84204582