前幾天自學了策略模式,因此寫個例子來驗證學習java
例子說明:對於學校的教務系統的登錄,各個高校的狀況不同,爲了實現代碼的可擴展性和維護性,我用策略模式去封裝,也不知道合適不合適的,望大神指點算法
策略接口(Login):定義了學校登錄login抽象方法apache
策略上下文(ContextOfLogin): 策略使用的上下文jsp
具體策略(LoginStrategyOfSISE): 具體策略我只寫了本身學校的,其餘學校沒去調研,因此沒有寫。ide
好了,很少說了,直接上代碼post
login學習
package com.sise.luming; /** * @author USER * 登錄策略的接口 */ public interface Login { public boolean login(String username, String password); }
ContextOfLoginthis
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; } } }
LoginStrategyOfSISEspa
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; } }
Maincode
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