用类,子类 ,State/Strategy代替类型码

来源:互联网 发布:it设备资产管理 编辑:程序博客网 时间:2024/06/03 09:15

1.用来代替类型码

动机:便于复用,充分利用系统的类型检查器等设施,更加安全

【转换前】

public class Person {public static final int O = 0;public static final int A = 1;public static final int B = 2;public static final int AB = 3;private int bloodType;public Person(int type){bloodType = type;}public void setBloodType(int arg){bloodType = arg;}public int getBloodType(){return bloodType;}}

【转化后】

public class Person {private static final int O = BloodGroup.O.getCode();private static final int A = BloodGroup.A.getCode();private static final int B = BloodGroup.B.getCode();private static final int AB = BloodGroup.AB.getCode();private BloodGroup bloodGroup;public Person01(int bloodGroup) {this.bloodGroup =BloodGroup.code(bloodGroup);}public void setBloodGroup(BloodGroup arg) {bloodGroup = arg;}public int getBloodGroupCode() {return bloodGroup.getCode();}public BloodGroup getBloodGroup(){return bloodGroup;}}class BloodGroup {public static final BloodGroup O = new BloodGroup(0);public static final BloodGroup A = new BloodGroup(1);public static final BloodGroup B = new BloodGroup(2);public static final BloodGroup AB = new BloodGroup(3);private static final BloodGroup[] values = {O,A,B,AB};public BloodGroup(int arg) {code = arg;}private int code;public int getCode(){return code;}public static BloodGroup code(int arg){return values[arg];}}
优点:任何用到血型码作为参数的方法,不必用数字表示对应的类型,增加的可读性,减少的出错的可能。例如,在Person总增加一个判断是否是A血型的方法-

public boolean isBloodType_A(){return getBloodGroup()==BloodGroup.A;}

而不是用

public boolean isBloodType_A(){return getBloodType()==0;}


2.用子类代替类型码

【转化前】


public class Employee {private int type;static final int ENGINEER = 0;static final int SALESMAN = 1;static final int MANAGER = 2;Employee(int type){this.type = type;}}
适用条件:有些类会用一些enum来改变自己的行为,比如下面的create(int type)方法,如果不子类化,在用create(int type)就需要返回int值代表职员的类型,当然没有用标示符表示的意思清楚。


【转化后】

public abstract class Employee {private int type;static final int ENGINEER = 0;static final int SALESMAN = 1;static final int MANAGER = 2;static Employee create(int type) {switch (type) {case ENGINEER:return new Engineer();case SALESMAN:return new salesMan();case MANAGER:return new Manager();default:throw new IllegalArgumentException();}}abstract int getType();}class Engineer extends Employee {int getType() {return Employee.ENGINEER;}}class salesMan extends Employee {int getType() {return Employee.SALESMAN;}}class Manager extends Employee {int getType() {return Employee.MANAGER;}}
3.State/Strategy代替类型码

【转化钱】

public class Employee0 {private int type;static final int ENGINEER = 0;static final int SALESMAN = 1;static final int MANAGER = 2; Employee0(int type) {this.type = type;} int payAmount(){ switch(type){ case ENGINEER: return monthlySalary; case SALESMAN: return monthlySalary+commission; case MANAGER: return monthlySalary+bonus; default: throw new RuntimeException("Incorrect Employee"); }  }  private int monthlySalary; private int commission; private int bonus;}


适用条件:类型码是可变的情况,例如,允许SALESMAN升职到MANAGER,那么对应的类型吗也要改变。

【百变后】

public class Employee0 {private EmployeeType type; Employee0(EmployeeType type) {this.type = type;} int payAmount(){ switch(getType()){//变化处 case EmployeeType.ENGINEER: return monthlySalary; case EmployeeType.SALESMAN: return monthlySalary+commission; case EmployeeType.MANAGER: return monthlySalary+bonus; default: throw new RuntimeException("iNCORRECT eMPLOYEE"); }  } int getType(){ return type.getTypeCode(); } //这里就是需要子类的地方void setType(int type){this.type = EmployeeType.newType(type); } private int monthlySalary; private int commission; private int bonus;}
abstract class EmployeeType{abstract int getTypeCode();static EmployeeType newType(int type){switch (type) {case ENGINEER:return new Engineer();case SALESMAN:return new SalesMan();case MANAGER:return new Manager();default:throw new IllegalArgumentException("Incorrect Employee Code");}}static final int ENGINEER = 0;static final int SALESMAN = 1;static final int MANAGER = 2;}
class Engineer extends EmployeeType{int getTypeCode(){return Employee.ENGINEER;}}
class SalesMan extends EmployeeType{int getTypeCode(){return Employee.SALESMAN;}}
class Manager extends EmployeeType{int getTypeCode(){return Employee.MANAGER;}}



原创粉丝点击