/**
* 模拟用户业务功能
*/
public interface UserService {
String login(String loginName,String password);
void selectUsers();
boolean deleteUsers();
}
public class UserServicelmpl implements UserService{
@Override
public String login(String loginName, String password) {
long startTimer = System.currentTimeMillis();
try {
Thread.sleep(1000);
if ("admin".equals(loginName) && "6666".equals(password)){
return "登陆成功!";
}
return "您的用户名或密码错误!!!";
} catch (Exception e) {
e.printStackTrace();
return "error";
} finally {
long endTimer = System.currentTimeMillis();
System.out.println("login方法耗时:" + (endTimer - startTimer) /1000.0 + 's');
}
}
@Override
public void selectUsers() {
long startTimer = System.currentTimeMillis();
System.out.println("已查询了100个用户数据");
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
e.printStackTrace();
}
long endTimer = System.currentTimeMillis();
System.out.println("selectUsers方法耗时:" + (endTimer - startTimer) /1000.0 + 's');
}
@Override
public boolean deleteUsers() {
long startTimer = System.currentTimeMillis();
try {
System.out.println("已删除了100个用户数据");
Thread.sleep(500);
return true;
} catch (InterruptedException e) {
e.printStackTrace();
return false;
}finally {
long endTimer = System.currentTimeMillis();
System.out.println("deleteUsers方法耗时:" + (endTimer - startTimer) /1000.0 + 's');
}
}
}
public class Test {
public static void main(String[] args) {
UserService userService = new UserServicelmpl();
System.out.println(userService.login("admin", "6666"));
System.out.println(userService.deleteUsers());
userService.selectUsers();
}
}
①必须有接口,实现类要实现接口(代理通常是基于接口实现的)
②创建一个实现类的对象,该对象为业务对象,紧接着为业务对象做一个代理对象