描述:根据不同的标识,获得不同的user对象
方法一:使用if else进行判断:
一旦分支多太多,逻辑复杂,会导致代码十分冗长,增加阅读难度;第二,违背了开闭原则。如果需要增加或减少分支,需要改动if…else if,增大因代码改动而出错的风险。
if("A".equals(type)){
//AAA
}else if("B".equals(type)){
//BBB
}else{
//CCC
}
方法二:用枚举和反射
创建一个枚举类
package com.example.usersport.demofanshe;
public enum UserEnum {
AUSER("A","com.example.usersport.demofanshe.AUser"),
BUSER("B","com.example.usersport.demofanshe.BUser"),
CUSER("C","com.example.usersport.demofanshe.CUser");
//User类对应的type
private String type;
//User类的类名
private String UserClassName;
private UserEnum(String type,String UserClassName){
this.type=type;
this.UserClassName=UserClassName;
}
public String getType() {
return type;
}
public String getUserClassName() {
return UserClassName;
}
public static String getUserClassName(String type) {
for(UserEnum user:UserEnum.values()){
if(user.getType().equals(type)){
return user.getUserClassName();
}
}
return null;
}
}
创建一个包含用户信息的类
package com.example.usersport.demofanshe;
/**
* @Description:
* @Author:
* @Date: 2021/8/12
*/
public class SendMessage {
private String desc;
private String name;
private String address;
private int age;
public String getDesc() {
return desc;
}
public void setDesc(String desc) {
this.desc = desc;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
@Override
public String toString() {
return "SendMessage{" +
"desc='" + desc + '\'' +
", name='" + name + '\'' +
", address='" + address + '\'' +
", age=" + age +
'}';
}
}
定义一个接口类User
package com.example.usersport.demofanshe;
public interface User {
SendMessage descMethod();
}
定义一个接口实现类AUser
package com.example.usersport.demofanshe;
/**
* @Description:
* @Author:
* @Date: 2021/8/12
*/
public class AUser implements User {
@Override
public SendMessage descMethod() {
SendMessage send = new SendMessage();
System.out.println("进入了AUser");
send.setAddress("北京北京");
send.setName("张三");
send.setAge(15);
return send;
}
}
定义一个接口实现类BUser
package com.example.usersport.demofanshe;
/**
* @Description:
* @Author:
* @Date: 2021/8/12
*/
public class BUser implements User {
@Override
public SendMessage descMethod() {
SendMessage send = new SendMessage();
System.out.println("进入了BUser");
send.setAddress("上海");
send.setName("李四");
send.setAge(18);
return send;
}
}
定义一个接口实现类CUser
package com.example.usersport.demofanshe;
/**
* @Description:
* @Author:
* @Date: 2021/8/12
*/
public class CUser implements User {
@Override
public SendMessage descMethod() {
SendMessage send = new SendMessage();
System.out.println("进入了CUser,默认");
send.setAddress("杭州");
send.setName("旺旺");
send.setAge(18);
return send;
}
}
测试类
package com.example.usersport.demofanshe;
/**
* @Description:
* @Author:
* @Date: 2021/8/12
*/
public class CreateUser {
public static void main(String[] args) throws Exception {
User userField = createUser("B");
SendMessage sendField = userField.descMethod();
System.out.println(sendField.toString());
}
public static User createUser(String type) throws Exception{
String userClassName= UserEnum.getUserClassName(type);
if(userClassName == null){
//将C类型设置成默认类型
userClassName=UserEnum.getUserClassName("C");
}
Class clazz = Class.forName(userClassName);
return (User)clazz.newInstance();
}
}