Copyright: Reprinted please note the name of the source https://blog.csdn.net/meism5/article/details/90735087
In order not to modify the original ServiceImpl business code, and its expansion (statistics, printing and time-consuming) and interception (to determine whether login), you can use a proxy.
code show as below
1, let ServiceImpl original core business processing logic
package constxiong.cxproxy.chapter3.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(150);
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();
}
}
}
2, a new class of agents, agent Service interface.
- Proxy holders of class interface to implement Service
- Agent login method, add the code printing capabilities and time-consuming
- Acting getUserinfo method, add a print time-consuming function code verify login
package constxiong.cxproxy.chapter3.proxy;
import java.util.Map;
import java.util.Random;
import constxiong.cxproxy.chapter3.service.Service;
import constxiong.cxproxy.chapter3.service.ServiceImpl;
/**
* Service 接口的静态代理类
* @author ConstXiong
* @date 2019-06-02 00:10:02
*/
public class StaticServiceProxy implements Service {
//持有Serrvice接口的实现类
private Service service;
public StaticServiceProxy() {
service = new ServiceImpl();
}
@Override
public boolean login(String username, String password) {
long start = System.currentTimeMillis();//计时开始
//调用 ServiceImpl 的登录方法
boolean result = service.login(username, password);
long end = System.currentTimeMillis();//计时结束
System.out.println("耗时:" + (end - start) + "毫秒");//打印耗时
return result;
}
@Override
public Map<String, Object> getUserInfo(String username) {
long start = System.currentTimeMillis();//计时开始
Map<String, Object> result = null;
if (checkIsLogined()) {//校验登录
result = service.getUserInfo(username);//调用 ServiceImpl 的获取用户信息方法
}
long end = System.currentTimeMillis();//计时结束
System.out.println("耗时:" + (end - start) + "毫秒");//打印耗时
return result;
}
/**
* 模拟 当前用户是否登录
*/
private boolean checkIsLogined() {
Random r = new Random();
int i = r.nextInt(10);
if (i % 2 == 0) {
System.out.println("已登录");
return true;
}
System.out.println("未登录");
return false;
}
}
3, calling the test code
package constxiong.cxproxy.chapter3;
import constxiong.cxproxy.chapter3.proxy.StaticServiceProxy;
import constxiong.cxproxy.chapter3.service.Service;
/**
* 测试类
* @author ConstXiong
* @date 2019-06-02 00:16:30
*/
public class Test {
public static void main(String[] args) {
Service service = new StaticServiceProxy();
service.login("ConstXiong", "123456");
service.getUserInfo("ConstXiong");
}
}
The results of printing with in Chapter 2, do not use a proxy consistent results.
advantage:
- May not modify the original target class (refer to the ServiceImpl) code
- Can function to intercept and expansion in the proxy class
Disadvantages:
- Acting class needs to implement the same interface as the target class, will lead to a larger number of the proxy class, easy to maintain
- Once the interface method of increasing the target class and the need to maintain the proxy class
In view of these shortcomings, JDK dynamic proxies are available.