Java proxy mode - static agents (1)

Disclaimer: This article is a blogger original article, shall not be reproduced without the bloggers allowed. https://blog.csdn.net/zhangyong01245/article/details/90519769

Proxy mode

  1. In the proxy mode (Proxy Pattern), the class represents a function of another class. This type of design pattern belongs structural model.
  2. In proxy mode, we create an object with an existing object to provide a functional interface to the outside world.
  3. Agent proxy mode is divided into static, dynamic proxy, this post mainly on the static agent

Static agents

  1. Static Agent: Create or specific tool automatically generates the source code, i.e. has an interface, the proxy class is, agents etc. finalized by the programmer at compile time. Before the program is running, the proxy class has been generated .class file.
  2. Simple implementation Example: Calculate the total time a Sql call.

Design ideas and code examples:

  1. Create an interface class SqlService
public interface SqlService {
    void executeSql() throws InterruptedException;
}
  1. Creating achieve SqlService of:
public class SqlServiceImpl implements SqlService {
	// 真正执行业务逻辑的地方
    public void executeSql() throws InterruptedException {
        System.out.println("Sql 开始执行.....");
        Thread.sleep(1000);
        System.out.println("Sql 执行结束.....");
    }
}
  1. Create a proxy class SqlProxy
public class SqlProxy implements SqlService{

    // 需要代理的对象
    private SqlService target;
    
    // 接受需要代理对象
    public SqlProxy(SqlService target) {
        this.target = target;
    }
    // 执行共有逻辑,并调用业务逻辑代码
    public void executeSql() throws InterruptedException {
        // 开始执行时间
        Long startTime = System.currentTimeMillis();
        // 真正执行的方法 (此处可以设计的更公有化,暂不讨论,后面会讲解动态代理)
        target.executeSql();
        // 执行结束
        Long endTime = System.currentTimeMillis();
        System.out.println(target.getClass().getName()+"执行executeSql耗时"+(endTime-startTime)+"ms");
    }
}
  1. Test categories:
public class Test {

    public static void main(String[] args) throws InterruptedException {
        // 真正使用的对象
        SqlService target = new SqlServiceImpl();
        // 代理对象
        SqlProxy sqlServiceProxy = new SqlProxy(target);
        // 执行代码
        sqlServiceProxy.executeSql();
    }
}
  1. Results of the:
Sql 开始执行.....
Sql 执行结束.....
com.lot.zy.test.SqlServiceImpl执行executeSql耗时1001ms

analysis:

1, first, program designers to implement SqlService interface methods;
2, when the Test call SqlServiceImpl.executeSql () method, is performed through a proxy SqlProxy, and perform other related public logic
3, in general, begin - >> proxy class -> execution of business class - >> proxy class - >> end.

Follow-up:

1, the static agent: https://blog.csdn.net/zhangyong01245/article/details/90519769
2, the Java dynamic proxy implementation: https://blog.csdn.net/zhangyong01245/article/details/90598309
3, dynamic CGLIB Acting and logic: https://blog.csdn.net/zhangyong01245/article/details/90644933

Guess you like

Origin blog.csdn.net/zhangyong01245/article/details/90519769