荣新IT培训中心
全国咨询热线:400-1335-066
您现在的位置:首页>行业新闻 > 正文

java设计模式-代理模式

时间:2017-09-28 10:27:36 来源:荣新IT教育培训 作者:荣新科技

近几年选择参加java培训的人越来越多,荣新教育小编带你学java,每天学一点,一年成为java编程高手。


java培训

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<String,Object> 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<String,Object> 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);
    }

}

荣新科技十年沉淀提供java培训,行业独家领先的课程,高薪就业不是梦!