学校登陆验证之策略模式封装

前几天自学了策略模式,所以写个例子来验证学习

例子说明:对于学校的教务系统的登陆,各个高校的情况不一样,为了实现代码的可扩展性和维护性,我用策略模式去封装,也不知道合适不合适的,望大神指点

策略接口(Login):定义了学校登陆login抽象方法

策略上下文(ContextOfLogin): 策略使用的上下文

具体策略(LoginStrategyOfSISE): 具体策略我只写了自己学校的,其他学校没去调研,所以没有写。

好了,不多说了,直接上代码

login

package com.sise.luming;

/**
 * @author USER
 *    登陆策略的接口
 */
public interface Login {

    public boolean login(String username, String password);
}

ContextOfLogin

package com.sise.luming;

/**
 * @author USER
 *    策略上下文
 */
public class ContextOfLogin {

    private Login login;
    public void setLogin(Login login) {
        this.login = login;
    }
    
    public boolean getLoginResult(String username, String password) {
        if(login!=null) {
            System.out.println("具体策略为:"+login.getClass());
            return login.login(username,password);
        }else{
            System.out.println("具体策略为null");
            return false;
        }
    }
}

LoginStrategyOfSISE

package com.sise.luming;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import org.apache.http.Consts;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;


/**
 * @author USER
 *    实现策略Login接口,实现具体的算法,SISE登陆的具体策略
 */
public class LoginStrategyOfSISE implements Login{

    @Override
    public boolean login(String username,String password) {
        CloseableHttpClient httpclient = HttpClients.createDefault();
        HttpPost post = new HttpPost("http://class.sise.com.cn:7001/sise/login_check_login.jsp");
        //开始登陆
        //封装post请求参数
        List<NameValuePair> formparams = new ArrayList<NameValuePair>();
        formparams.add(new BasicNameValuePair("08e7a22bca3fd77141e6faa335c5d402", "4756a1c591ef421a90785fec028e98cf"));
        formparams.add(new BasicNameValuePair("username", username));
        formparams.add(new BasicNameValuePair("password", password));
        UrlEncodedFormEntity entity = new UrlEncodedFormEntity(formparams,Consts.UTF_8);
        post.setEntity(entity);
        //获取响应结果
        try {
            HttpResponse response = httpclient.execute(post);
            HttpEntity entity1 = response.getEntity();
            if (entity!=null) {
                if(EntityUtils.toString(entity1).contains("<script>top.location.href='/sise/index.jsp'</script>")) {
                    System.out.println("登陆成功");
                    return true;
                }
            }
        } catch (ClientProtocolException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }finally{
            post.releaseConnection();
        }
        System.out.println("登陆失败");
        return false;
    }

}

Main

package com.sise.luming;

public class Main {

    public static void main(String[] args) {
        String username = "yourusername";    //登陆账号
        String password = "yourpassword";    //登陆密码
        ContextOfLogin login = new ContextOfLogin();    
        login.setLogin(new LoginStrategyOfSISE());    //设置SISE登陆策略
        boolean flag = login.getLoginResult(username,password);    //获取登陆结果
        System.out.println("登陆的结果是:"+flag);
    }
}

运行结果

具体策略为:class com.sise.luming.LoginStrategyOfSISE
登陆失败
登陆的结果是:false


你可能感兴趣的:(策略模式,SISE,教务系统登陆)