1、需要解决的问题: a、避免出现坏味道,比如过多的if,else;b、增加新的登陆方式可以减少修改成本
2、为了解决以上问题,将引入以下几个关键点:a、引入Login视图对象。b、使用枚举型。
3、具体实现如下。
/**登陆值对象
* Date: 2010-11-24
* Time: 21:30:26
*/
public class Login {
private LoginModel model;
private LoginService service;
private String password;
private String email;
private String cellphone;
private String username;
private String qq;
public boolean doLogin(){
return model.doLogin(this,service);
}
public void setModel(LoginModel model) {
this.model = model;
}
public void setEmail(String email) {
this.email = email;
}
public void setCellphone(String cellphone) {
this.cellphone = cellphone;
}
public void setUsername(String username) {
this.username = username;
}
public void setQq(String qq) {
this.qq = qq;
}
public LoginModel getModel() {
return model;
}
public LoginService getService() {
return service;
}
public String getEmail() {
return email;
}
public String getCellphone() {
return cellphone;
}
public String getUsername() {
return username;
}
public String getQq() {
return qq;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
}
/**
* Date: 2010-11-24
* Time: 21:28:11
*/
/**
* 登陆方式
*/
public enum LoginModel {
Email{
public boolean doLogin(Login login ,LoginService service){
return service.checkByEamil(login.getEmail(),login.getPassword());
}
},
Username{
public boolean doLogin(Login login ,LoginService service){
return service.checkByUsername(login.getUsername(),login.getPassword());
}
},
Cellphone{
public boolean doLogin(Login login ,LoginService service){
return service.checkByCellphone(login.getCellphone(),login.getPassword());
}
},
QQ{
public boolean doLogin(Login login ,LoginService service){
return service.checkByQQ(login.getQq(),login.getPassword());
}
} ;
abstract boolean doLogin(Login login ,LoginService service);
}
package com.ming.login;
/**
* Date: 2010-11-24
* Time: 21:40:09
*/
/**
* Login业务逻辑层
*/
public class LoginService {
public boolean checkByEamil(String email, String password) {
return true;
}
public boolean checkByUsername(String username, String password) {
return true;
}
public boolean checkByCellphone(String cellphone, String password) {
return true;
}
public boolean checkByQQ(String qq, String password) {
return true;
}
}
4、实际应用中如何调用
Login login = new Login();
login.setEmail("xxx@xx.com");
login.setPassword("xxxxxxxxx");
login.setModel(LoginModel.Email);
login.doLogin();
5、增加身份证方式,需改动哪些些东西。
a、login 增加idCard,并增加getter/setter方式
b、LoginModel,增加idCard枚举
IdCard{
public boolean doLogin(Login login ,LoginService service){
return service.checkByIdCard(login.getIdCard(),login.getPassword());
}
}
c、在LoginSevice,增加 checkByIdCard方法。