Agency is a design mode. Take renting houses as an example. In the past, the landlord would directly contact the tenant and show the tenant the house to rent the house and so on. The landlord just wants to rent the house and nothing else is involved. The agent rents the house for the landlord. In other words: the landlord just needs to focus on his business logic. This is the agent mode in life intuitive embodiment.

Static agent

On the first code

Define the level of abstraction, which I understand as a behavioral standard, and this is the rental housing service

public interface RentService {
    void rentHouse();
}
Copy the code

Define the real role, in this case the landlord, and implement the above interface

Public class Landlord implements RentService {@override public void rentHouse() {system.out.println (" Landlord "); }}Copy the code

Define proxy roles, in this case mediations

public class Proxy implements RentService{ private Landlord landlord; public Proxy(Landlord landlord){ this.landlord = landlord; } @override public void rentHouse() {// system.out.println (); landlord.rentHouse(); // system.out. println(" contract "); }}Copy the code

The application

public static void main(String[] args) {
        Proxy proxy = new Proxy(new Landlord());
        proxy.rentHouse();
    }
Copy the code

Run print: Landlord rents the house

In the application, the landlord does nothing, so we create an agent and represent the landlord. On the surface, the intermediary is renting the house, but in fact the landlord is renting the house, and the agent can do some enhanced processing, such as taking the tenant to see the house to sign a contract and so on.

Advantages of static proxies: The business only needs to focus on the business itself

Disadvantages: 1. If you add a real object, you need to add a proxy class

2. If a method is added to the abstract interface, both real actors and agents have to implement it, which increases the complexity of the code and costs a lot of maintenance

A dynamic proxy

Dynamic proxies are implemented by reflection, as usual, code first

A dynamic proxy

public class DynamicProxy implements InvocationHandler { private Object proxyObject; public Object newInstance(Object proxyObject) { this.proxyObject = proxyObject; return Proxy.newProxyInstance(proxyObject.getClass().getClassLoader(), proxyObject.getClass().getInterfaces(),this); } @Override public Object invoke(Object proxy, Method method, Object[] args) throws Throwable { Object result = method.invoke(proxyObject, args); return result; }}Copy the code

The program

    public static void main(String[] args) {
        DynamicProxy proxy = new DynamicProxy();
        RentService service = (RentService) proxy.newInstance(new Landlord());
        System.out.println(service.getClass());
        service.rentHouse();
    }
Copy the code

Print: class com.sun.proxy.$Proxy0 Landlord rents the house

You can see that RentService service = (RentService) proxy.newInstance(new Landlord()) gets a proxy object, which invokes the corresponding method through the invoke method. Dynamic proxy overcomes the disadvantages of static proxy.