A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

public interface IUser {
//登录
void login(String userId,String password);
//修改昵称
void editNickname(String nickname);
}
public class User implements IUser {
private String nickname;
private String userId;
private String password;
public User(String userId,String password){
this.userId = userId;
this.password = password;
}
@Override
public void login(String userId, String password){
if(this.userId == userId && this.password == password){
System.out.println("用户登录成功");
}
else
System.out.println("用户登录失败");
}
@Override
public void editNickname(String nickname) {
this.nickname = nickname;
System.out.println("修改昵称成功,当前用户的昵称是:"+this.nickname);
}
}
客户端类
public class Client {
public static void main(String[] args) {
//不调用代理模式时
IUser user = new User("firs","123");
user.login("firs", "123");
user.editNickname("大风");
}
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
/**
* 静态代理类必须实现接口,而且需要新创建一个类的代码出来
* @author Administrator
*
*/
public class StaticProxy implements IUser {
private IUser user;
public StaticProxy(String userId,String password){
this.user = new User(userId,password);
}
//登陆前的操作,记录当前登录的时间
void noteLoginInfo(String[] params, String opreate){
Map loginInfo = new HashMap<>();
loginInfo.put("params", params);
loginInfo.put("opreate", opreate);
loginInfo.put("opreateTime", new Date());
System.out.println("记录用户操作成功");
}
@Override
public void login(String userId, String password){
noteLoginInfo(new String[]{userId, password},"login");
user.login(userId, password);
}
@Override
public void editNickname(String nickname) {
noteLoginInfo(new String[]{nickname},"editNickname");
user.editNickname(nickname);
}
}
客户端类:
public class Client {
public static void main(String[] args) {
//不调用代理模式时
IUser user = new User("firs","123");
user.login("firs", "123");
user.editNickname("大风");
System.out.println("");
System.out.println("=============调用静态代理模式后===========");
//需要实现记录用户登录和修改昵称操作的日志功能
//基于“拓展开发,修改关闭”的设计准则,我们可以用静态代理的方式
IUser proxy = new StaticProxy("firs","123");
proxy.login("firs", "123");
proxy.editNickname("我还是大风");
}
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
/**
* 动态代理类不用和主体类继承同一个接口
* @author Administrator
*
*/
public class DynamicProxy implements InvocationHandler {
private Object object;
public DynamicProxy(String userId,String password,Class c){
Object obj = null;
try {
obj = Class.forName(c.getName())
.getConstructor(String.class,String.class)
.newInstance(userId,password);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
this.object = obj;
}
//登陆前的操作,记录当前登录的时间
void noteLoginInfo(String[] params, String opreate){
Map loginInfo = new HashMap<>();
loginInfo.put("params", params);
loginInfo.put("opreate", opreate);
loginInfo.put("opreateTime", new Date());
System.out.println("记录用户操作成功");
}
@Override
public Object invoke(Object proxy, Method method, Object[] args)
throws Throwable {
String[] params = new String[args.length];
for(int i = 0 ;i < args.length ; i++){
params[i] = args[i].toString();
}
noteLoginInfo(params, method.getName());
return method.invoke(object, args);
}
}
最后的客户端类:
package com.test.my;
import java.lang.reflect.Proxy;
public class Client {
public static void main(String[] args) {
//不调用代理模式时
IUser user = new User("firs","123");
user.login("firs", "123");
user.editNickname("大风");
System.out.println("");
System.out.println("=============调用静态代理模式后===========");
//需要实现记录用户登录和修改昵称操作的日志功能
//基于“拓展开发,修改关闭”的设计准则,我们可以用静态代理的方式
IUser proxy = new StaticProxy("firs","123");
proxy.login("firs", "123");
proxy.editNickname("我还是大风");
System.out.println("");
System.out.println("=============调用动态代理模式后===========");
DynamicProxy dynamicProxy = new DynamicProxy("firs","123",Admin.class);
ClassLoader cl = Admin.class.getClassLoader();
IUser iuser = (IUser)Proxy.newProxyInstance(cl,
new Class[]{IUser.class}, dynamicProxy);
iuser.login("firs","123");
iuser.editNickname("使用动态代理后的大风");
}
}
因为需求而增加的Admin类
public class Admin implements IUser {
private String nickname;
private String userId;
private String password;
public Admin(String userId,String password){
this.userId = userId;
this.password = password;
}
@Override
public void login(String userId, String password){
if(this.userId == userId && this.password == password){
System.out.println("用户登录成功");
}
else
System.out.println("用户登录失败");
}
@Override
public void editNickname(String nickname) {
this.nickname = nickname;
System.out.println("修改昵称成功,当前用户的昵称是:"+this.nickname);
}
}

0 个回复

您需要登录后才可以回帖 登录 | 加入黑马