Java Foundation -- Proxy Mode (Static, Dynamic)

Keywords: Java Programming Spring

My own understanding of the agency model, in a colloquial and easy-to-understand way to summarize it.
For example, there are many overseas agents who can act as agents for shoes, cosmetics and so on. After all, they blindly think that overseas agents are good things. Assuming what you need to buy overseas, you need to do some preparatory work in advance, such as investigating the source of the product, or distinguishing the authenticity of the product. After buying, you can also do some satisfaction survey about the product, which is convenient for later recommendation to others to buy, but you can also find out. An agent, let him do these things that have nothing to do with the main purchase, you just need to be responsible for the purchase of things.
First: Static Agent Model
Suppose you need to buy cosmetics now, and then find some overseas agents to help you do some preparatory work in advance, you just need to tell them what you want.
First, we need a Subject class to express what you need the agent to do, or to understand what the agent is doing, such as shopping, financial management, and so on.
Agents and principals are related to this agent, so they establish a relationship through Subject and inherit the Subject interface.
The following examples are mainly about using agents for shopping.

package Test;
//theme
public interface Subject {

    public void shopping();
}

Then there's an agent class, Proxy, to help you prepare for the shopping we mentioned earlier.

package Test;
//Agent
public class Proxy implements Subject{
    private Subject target;//Principal Agent
    public Proxy(Subject target){
        this.target = target;
    }
    //The Method of Implementing Interface
    public void shopping(){
        //What Agents Should Do 
        System.out.println("Do a lot of professional evaluation of commodities");

        target.shopping();//The Real Business of the Agent


        //What to do after buying on behalf of others
        System.out.println("Satisfaction Survey");
    }
}

The agent above is passed in from the main method, followed by the code of the agent SuperMan:

package Test;
//Principal Agent
public class SuperMan implements Subject{
    public void shopping(){
        //There's a lot of evaluation before buying.
        //. . . 

        System.out.println("Purchase of imported cosmetics");


    }
}

Then the main method is:

package Test;

public class Main {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        SuperMan man = new SuperMan();
        Proxy p = new Proxy(man);
        p.shopping();
    }

}

The object man is passed to the proxy class as a target to execute the shopping() method, so the called shopping method is the Shopping method in SuperMan.
The results are as follows:

Maybe many people don't understand why they want to do this, or think they don't need any proxy classes. These tasks can be done by one class. In fact, in some small programs, it can be done by one class as a test. But the idea of Java programming is to interface programming, and another is to reduce coupling. The coupling degree of static proxy mode is very high, because a principal binds a proxy, and the coupling degree is very high. Let's talk about dynamic proxy mode, which is used to reduce the coupling degree. It is also an important idea in programming. The idea of dynamic proxy will also be used in Spring later to implement the code.
II: Dynamic Agent Model
I mentioned the coupling degree of static agent before. Suppose I want not only to hand over some trifles of shopping to the agent, but also to find financial agent, travel agent, or many other agents. What should I do?! If there is an agent like agency, which trains agents in all aspects, I would like to find this agency directly, right, this is the vernacular idea of dynamic agent realization.
First of all, I need an "agency" to produce agent projects - shopping and tourism:

package cn.park;

public interface ProxyMall {
    public void shopping();
    public void trival();
}

Then there is the proxy class, this time the proxy class needs to inherit InvocationHandler and override the methods in it:

package cn.park;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
public class CreatProxy implements InvocationHandler{
    //Declare the proxy object (Object is used because it is a superclass of all classes containing custom classes in Java)
    private Object target;
    //Create proxy objects (based on proxy object requirements)
    public Object bind(Object target){
        this.target = target;
        //Create a dynamic proxy object using the Proxy class
        Object proxy = Proxy.newProxyInstance(target.getClass().getClassLoader(), target.getClass().getInterfaces(), this);
        return proxy;
    }
    //Specific business execution methods
    public Object invoke(Object proxy, Method method, Object[] args)
            throws Throwable {
        //Calling business methods
        method.invoke(target);
        return null;
    }
}

Specific parameters are:
proxy: proxy class object
Method: The method of the agent object, both travel and shopping in the following agents
args: Parameters of the proxy object method
Agent class Man:

package cn.park;

public class Man implements ProxyMall {

    @Override
    public void shopping() {
        System.out.println("I want to buy cosmetics.");
    }

    @Override
    public void trival() {
        System.out.println("I want to travel");
    }

}

In the main method, the code is as follows:

package cn.park;

public class Test {

    public static void main(String[] args) {
        Man man = new Man();
        CreatProxy cp = new CreatProxy();
        ProxyMall lj = (ProxyMall ) cp.bind(man);
        lj.shopping();
        lj.trival();
    }

}

In an agency, by binding multiple agents to the principal (through the bind method in the CreatProxy class), the agent can choose the agent he needs (that is, the method created in Man) according to the type of agent provided by the agency (shopping and tourism).

[********************************************************]
All the above opinions are just my personal understanding, not guaranteeing all the correctness, but also hope that friends who have a deeper and more correct understanding can point out my mistakes. Thank you first!

Posted by Smee on Sun, 30 Jun 2019 18:07:50 -0700