JavaSE-static proxy

Proxy pattern classification :

  • Static proxy
  • Dynamic proxy

1.1 Static proxy

Static agent role analysis

  • Abstract role: It is generally implemented using interfaces or abstract classes.
  • Real role: The role being proxied.
  • Acting role: Acting for the real role; after acting for the real role, some subsidiary operations are usually done.
  • Client: Use the agent role to perform some operations.

1.2 Code example

Rent interface (abstract role)

package cn.guardwhy.domain;
// 抽象角色:租房
public interface Rent {
    
    
    public void rent();
}

Landlord

package cn.guardwhy.domain;
// 真实角色: 房东,要出租房子
public class Landlord implements Rent {
    
    
    @Override
    public void rent() {
    
    
        System.out.println("房屋出租!!!");
    }
}

Proxy is the proxy role

package cn.guardwhy.proxy;

import cn.guardwhy.domain.Landlord;
import cn.guardwhy.domain.Rent;

public class Proxy implements Rent {
    
    
    private Landlord landlord;

    public Proxy() {
    
    

    }

    public Proxy(Landlord landlord) {
    
    
        this.landlord = landlord;
    }

    @Override
    // 租房
    public void rent() {
    
    
        // 调用方法
        seeHouse();
        landlord.rent();
        agreement();
        fare();
    }

    // 看房
    public void seeHouse(){
    
    
        System.out.println("带房客看房!!!");
    }
    // 合同
    public void agreement(){
    
    
        System.out.println("签租赁合同...");
    }
    // 收中介费
    public void fare(){
    
    
        System.out.println("收中介费");
    }
}

Client (tenant)

package cn.guardwhy.client;

import cn.guardwhy.domain.Landlord;
import cn.guardwhy.proxy.Proxy;

public class Client {
    
    
    public static void main(String[] args) {
    
    
        // 1.房东租房子
        Landlord landlord = new Landlord();
        // 2.代理,中介帮房东租房子
        Proxy proxy = new Proxy(landlord);
        // 3.不需要直接面对房东,直接找中介租房即可
        proxy.rent();
    }
}

Results of the

Analysis result

In this process, you are directly in contact with the intermediary, just like in real life, you cannot see the landlord, but you still rent the landlord’s house through an agency, which is the so-called agency model.

1.3 Features of static proxy

advantage:

  • Can make our real role more pure. No longer pay attention to some public things.
  • The public business is completed by the agent. The division of labor is realized.
  • As public services expand, they become more centralized and convenient.

Disadvantages:

There are more classes and more agent classes, and the workload becomes larger. Development efficiency is reduced.

2.1 Re-understanding of static proxy

1. Abstract role

package cn.guardwhy.domain;
// 抽象角色,增删改查业务
public interface UserService {
    
    
    void add();
    void delete();
    void update();
    void query();
}

2. Real objects to complete the addition, deletion, modification, and inspection operations

package cn.guardwhy.domain;
// 真实对象,完成增删改查的操作
public class UserServiceImpl implements UserService{
    
    
    @Override
    public void add() {
    
    
        System.out.println("增加了一个用户");
    }

    @Override
    public void delete() {
    
    
        System.out.println("删除了一个用户");
    }

    @Override
    public void update() {
    
    
        System.out.println("更新了一个用户");
    }

    @Override
    public void query() {
    
    
        System.out.println("查询一个用户");
    }
}

3. Set up a proxy class to process logs

package cn.guardwhy.proxy;

import cn.guardwhy.domain.UserService;
import cn.guardwhy.domain.UserServiceImpl;

public class UserServiceProxy implements UserService {
    
    
    private UserServiceImpl userService;

    public UserServiceImpl getUserService() {
    
    
        return userService;
    }

    public void setUserService(UserServiceImpl userService) {
    
    
        this.userService = userService;
    }

    @Override
    public void add() {
    
    
        message("添加方法");
        userService.add();
    }

    @Override
    public void delete() {
    
    
        message("删除方法");
        userService.delete();
    }

    @Override
    public void update() {
    
    
        message("更新方法");
        userService.update();
    }

    @Override
    public void query() {
    
    
        message("查询方法");
        userService.query();
    }

    public void message(String msg){
    
    
        System.out.println("执行了" + msg + "方法!!");
    }
}

4. Test code

package cn.guardwhy.client;

import cn.guardwhy.domain.UserServiceImpl;
import cn.guardwhy.proxy.UserServiceProxy;

public class Client2 {
    
    
    public static void main(String[] args) {
    
    
        // 1.真实业务
        UserServiceImpl userService = new UserServiceImpl();
        // 2.代理类
        UserServiceProxy proxy = new UserServiceProxy();
        // 3.使用代理类实现日志功能
        proxy.setUserService(userService);
        // 调用方法
        proxy.add();
    }
}

Guess you like

Origin blog.csdn.net/hxy1625309592/article/details/115031693