Static agent
Role analysis:
- Abstract roles: Interfaces or abstract classes are usually used to solve this problem
- True Role: The role of the agent
- Proxy role: Proxy for real roles. After proxy for real roles, we usually do some ancillary operations.
- Client: The person who accesses the proxy object
Benefits of the proxy model:
- Can make the real character’s operation more pure! You don’t have to pay attention to some public business
- The public is left to the agent! Realized the division of labor of business!
- When the common service happens to expand, convenient centralized management!
Disadvantages:
- A real character leads to a surrogate character; The amount of code will double and the development will be less efficient
Code steps:
- interface
- True role
- The agent role
- The client accesses the proxy role
The sample
interface
public interface Rent {
void rent(a);
}
Copy the code
True role
public class Host implements Rent{// Real role implementation interface
@Override
public void rent(a) {
System.out.println("The landlord wants to let the house..."); }}Copy the code
The agent role
public class Proxy implements Rent{
private Host host;
public Proxy(Host host) {
this.host = host;
}
public Proxy(a) {}@Override
public void rent(a) {/ / proxy Host
host.rent();
}
public void extend01(a){
System.out.println("Extend the business based on the real role of agent... 01");
}
public void extend02(a){
System.out.println("Scaling up the business based on the real role of agent... 02");
}
public void extend03(a){
System.out.println("Scaling up the business based on the real role of agent... 03"); }}Copy the code
The client accesses the proxy role
public class Client {
public static void main(String[] args) {
Proxy proxy=new Proxy(new Host());
proxy.rent();// Use the Host method through the proxyproxy.extend01(); proxy.extend02(); proxy.extend03(); }}Copy the code
A dynamic proxy
According to the above introduction, you will find that each proxy class can only serve one interface, so the program development will inevitably produce many proxy classes, so we will try to use a proxy class to complete all the proxy function, then we need to use dynamic proxy
In the example above, an agent can only delegate one type, and the proxied object is determined at the compiler. Dynamic proxy is realized at run time through reflection mechanism, and can proxy various types of objects
In Java to realize the dynamic Proxy mechanism, need Java. Lang. Reflect. InvocationHandler interface and Java. Lang. Reflect. The Proxy class support
- Dynamic proxy roles are the same as static proxy roles
- Dynamic proxy proxy classes are dynamically generated, not written directly!
- Dynamic proxies fall into two categories: interface-based dynamic proxies and class-based dynamic proxies
- Interface based –JDK dynamic Proxy
- Based on class: cglib
- Java bytecode implementation: javasist
JDK dynamic proxy
-
The representation of dynamic proxy is that the same proxy object can represent multiple proxy objects, unlike the previous static proxy only bound to one proxy object.
-
In Java, jdK-based proxy classes and proxied classes must implement the same interface.
The sample
- interface
public interface Business {
void doBusiness(a);
}
Copy the code
- Real objects (multiple classes implementing the same interface)
public class Boss01 implements Business{
public void doBusiness(a) {
System.out.println("Boss One is talking business.");
}
public void aVoid(a){
System.out.println("dfgdgssdfg"); }}public class Boss02 implements Business{
public void doBusiness(a) {
System.out.println("Boss two is talking business."); }}public class Boss03 implements Business{
@Override
public void doBusiness(a) {
System.out.println("Boss Number three is talking business."); }}Copy the code
- implementation
InvocationHandler
interface
public class ProxyIH implements InvocationHandler {//
private Object target;// Real object (proxied object)
public void setTarget(Object target) {// Set the real object
this.target = target;
}
public Object getTarget(a){// Get the real object to be proxied
return Proxy.newProxyInstance(this.getClass().getClassLoader(), target.getClass().getInterfaces(),this);
}
@Override
public Object invoke(Object proxy, Method method, Object[] args)
throws Throwable {// This method can implement business extensions and execute methods on proxied objects
extend();
Object result = method.invoke(target,args);
log(target.getClass().getName());
return result;
}
public void extend(a){
System.out.println("Agent expansion: Show boss dynamics");
}
public void log(String msg){
System.out.println("Agent."+msg+"Business"); }}Copy the code
- test(Client)
Dynamic proxy, proxy is the interface
public class Client {
public static void main(String[] args) {
ProxyIH proxyIH = new ProxyIH();
proxyIH.setTarget(new Boss01());
// proxyIH.setTarget(new Boss02());
// proxyIH.setTarget(new Boss03());
Business business = (Business) proxyIH.getTarget();// Dynamic proxy, proxy is the interfacebusiness.doBusiness(); }}Copy the code