用类,子类 ,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;}}
- 用类,子类 ,State/Strategy代替类型码
- 8.15 replace type code with state/strategy (以state/strategy取代类型码)
- 重构之4.Replace Type Code with State/Strategy(以State/Strategy取代类型码)
- state代替if-else
- State模式与Strategy模式的区别
- 设计模式之 strategy和state
- Refactoring - replace Switch statement with state/strategy
- Bridge、Visitor、State和Strategy的理解
- State模式与Strategy模式的区别
- 设计模式学习笔记--Strategy、State
- Replace Type Code with State/Strategy(以State/Strategy取代型别码)
- 子类代替父类进行参数传递
- design pattern notes [7] - state,strategy,memento,mediator,and iterator
- the difference between state and strategy design pattern
- Strategy策略模式和State/Bridge模式的区别
- State模式及与Strategy/Commmand/Chain of Resposibility区别
- 设计模式(12)--State状态模式&Strategy策略模式
- 比较策略(strategy)模式和状态(state)模式
- 代码块、内部类
- struts2 标签中传值的问题
- MySQL Desc指令相关
- ABAP中的F4帮助怎么用
- 现实迷途 第十五章 损兵折将
- 用类,子类 ,State/Strategy代替类型码
- 桶排序(Bucket Sort),基数排序(Radix_Sort)
- DSP----------绘图
- Openstack中社区项目简介
- POJ 2785:4 Values whose Sum is 0
- SQL语句:查询多表更新数据
- 有目标就不会迷茫
- 通过淘宝IP地址库获取IP位置
- 获取更大的利润在很短的时间周期