Chapter 1, why should we increase agent?

Copyright: Reprinted please note the name of the source https://blog.csdn.net/meism5/article/details/90486684

Consider, if you want to perform two functions in Java Web project:

1, each print request from the beginning to the end of time-consuming

2, check the current user is logged on certain requests

 

According to conventional thinking, the solution is

1, at the beginning and end of each requested controller codes are recorded time, and finally print it this time difference

2, in each of the request code to be verified, add the code to verify the current user is logged

 

such:

  • Modify the original code logic
  • Each point involves the need to change, a large amount of change

There is no way to deal with this uniform requirements without changes to the original code so that the original code is still only interested in the core business logic?

There, using proxy mode. Let broker to deal with a unified demand, is simply to achieve agency's core business logic.

 

The original analog business code:

package constxiong.cxproxy.chapter1;

import constxiong.cxproxy.chapter1.service.Service;
import constxiong.cxproxy.chapter1.service.ServiceImpl;

/**
 * 测试类
 * @author ConstXiong
 * @date 2019-05-29 11:01:30
 */
public class Test {

	public static void main(String[] args) {
		Service service = new ServiceImpl();
		service.login("ConstXiong", "123456");
		service.getUserInfo("ConstXiong");
	}
	
}
package constxiong.cxproxy.chapter1.service;

import java.util.Map;

/**
 * 服务接口
 * @author ConstXiong
 * @date 2019-05-29 11:02:02
 */
public interface Service {

	boolean login(String username, String password);

	Map<String, Object> getUserInfo(String username);

}
package constxiong.cxproxy.chapter1.service;

import java.util.HashMap;
import java.util.Map;

/**
 * 服务接口实现
 * @author ConstXiong
 * @date 2019-05-29 11:02:15
 */
public class ServiceImpl implements Service {

	@Override
	public boolean login(String username, String password) {
		simulateDaOperation(100);
		System.out.println("用户名:" + username + ", 密码:" + password + "  登录成功");
		return true;
	}

	@Override
	public Map<String, Object> getUserInfo(String username) {
		Map<String, Object> userInfo = new HashMap<String, Object>();
		simulateDaOperation(100);
		userInfo.put("username", username);
		userInfo.put("sex", "男");
		userInfo.put("age", 18);
		System.out.println("用户名:" + username + ", 获取用户信息:" + userInfo);
		return userInfo;
	}
	
	/**
	 * 模拟数据库操作,休眠
	 * @param millis 毫秒数
	 */
	private void simulateDaOperation(long millis) {
		try {
			Thread.sleep(millis);
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
	}

}

 The results of the test class:

用户名:ConstXiong, 密码:123456  登录成功
用户名:ConstXiong, 获取用户信息:{sex=男, age=18, username=ConstXiong}

 

Complete source: https://github.com/ConstXiong/xtools     cxproxy project chapter1

 

Here we do not use a proxy, complete functions.

Guess you like

Origin blog.csdn.net/meism5/article/details/90486684