Two cases take you to understand the static proxy model

What is a static proxy?

Agents in life are very common, such as purchasing, lawyers, intermediaries, etc. They all have one thing in common is to help the agent deal with some things before and after. The agent only needs to focus on the part of the thing he wants to do.
The agent in Java is similar. The agent model can help the agent to complete some preliminary preparations and later work, but the core business logic is still completed by the agent.

Proxy mode

Case number one

Rental
1. Interface

//租房
public interface Rent {
    public void rent();
}

2. Real characters

//房东
public class Host implements Rent {
    public void rent(){
        System.out.println("房东出租房子!");
    }
}

3. Agent Role

public class Proxy implements Rent {
    private Host host;

    public Proxy() {
    }
    public Proxy(Host host) {
        this.host = host;
    }

    public void rent(){
        seeHouse();
        host.rent();
        hetong();
        fee();
    }
    //看房
    public void seeHouse(){
        System.out.println("中介带你看房");
    }
    //签合同
    public void hetong(){
        System.out.println("签合同");
    }
    //收费
    public void fee(){
        System.out.println("收取中介费用");
    }
}

4. Customer access

public class Client {
    public static void main(String[] args) {
        //房东出租房子
        Host host = new Host();
        //代理,中介帮房东租房子,但是代理角色一般会有一些附属操作!
        Proxy proxy = new Proxy(host);
        proxy.rent();
    }
}

Case 2

1. Interface

public interface UserService{
    void add();
    void delete();
    void update();
    void query();
}

2. Real characters

public class UserServiceImpl implements UserService {
    public void add() {
        System.out.println("增加一个用户");
    }
    public void delete() {
        System.out.println("删除一个用户");
    }

    public void update() {
        System.out.println("修改一个用户");
    }
    public void query() {
        System.out.println("查询一个用户");
    }
}

3. Agent Role

public class UserServiceProxy implements UserService {

    private UserServiceImpl userService;

    public void setUserService(UserServiceImpl userService) {
        this.userService = userService;
    }
    @Override
    public void add() {
        log("add");
        userService.add();
    }
    @Override
    public void delete() {
        log("delete");
        userService.delete();
    }
    @Override
    public void update() {
        log("update");
        userService.update();
    }
    @Override
    public void query() {
        log("query");
        userService.query();
    }
    //日志方法
    public void log(String msg) {
        System.out.println("使用了" + msg + "方法");
    }
}

4. Client access

public class Client {
    public static void main(String[] args) {
        UserServiceImpl userService = new UserServiceImpl();
        UserServiceProxy proxy = new UserServiceProxy();
        proxy.setUserService(userService);
        proxy.query();
    }
}

to sum up:

The benefits of the proxy model:

  • Can make the operation of real characters more pure, without having to pay attention to some public business
  • The public will be handed over to the role of agent, realizing the division of business
  • Convenient centralized management when public services are expanded

Disadvantages of proxy mode:

  • A real role will produce an agent role, the amount of code will double, and the development efficiency will become lower
Published 51 original articles · Likes 73 · Visits 3700

Guess you like

Origin blog.csdn.net/qq_41256881/article/details/105430183