Replace Type Code with State/Strategy(以State/Strategy取代类型码)
生活随笔
收集整理的這篇文章主要介紹了
Replace Type Code with State/Strategy(以State/Strategy取代类型码)
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
有一個類型碼,它會影響類的行為,但你無法通過繼承消除它
?
public class Employee {static final int ENGINNER = 0;static final int SALESMAN = 1;static final int MANAGER = 2;private int type;// 月薪.private int montylySalary;// 傭金.private int commission;// 獎金.private int bonus;public Employee(int type) {this.type = type;}public int payAmount() {switch(getType()) {case ENGINNER: return montylySalary;case SALESMAN: return montylySalary + commission;case MANAGER: return montylySalary + bonus;default:throw new IllegalArgumentException("Incorrect Employee.");}}public int getType() {return type;} }重構:以狀態對象取代類型碼
public abstract class EmpoyeeType {static final int ENGINNER = 0;static final int SALESMAN = 1;static final int MANAGER = 2;public abstract int getTypeCode();public static EmpoyeeType newType(int code) {switch (code) {case ENGINNER: return new Engineer();case SALESMAN: return new Salesman();case MANAGER: return new Manager();default:throw new IllegalArgumentException("Incorrect Employee Code.");}} }public class Engineer extends EmpoyeeType {public int getTypeCode() {return Employee.ENGINNER;} }public class Manager extends EmpoyeeType {public int getTypeCode() {return Employee.MANAGER;} }public class Salesman extends EmpoyeeType {public int getTypeCode() {return Employee.SALESMAN;} }public class Employee {private EmpoyeeType type;// 月薪.private int montylySalary;// 傭金.private int commission;// 獎金.private int bonus;public Employee(int type) {this.type = type;}public int payAmount() {switch(getType()) {case EmpoyeeType.ENGINNER: return montylySalary;case EmpoyeeType.SALESMAN: return montylySalary + commission;case EmpoyeeType.MANAGER: return montylySalary + bonus;default:throw new RuntimeException("Incorrect Employee.");}}public EmpoyeeType getType() {return type;} }再繼續Replace Conditional with Polymorphism(以多態取代條件表達式)
public abstract class EmpoyeeType {static final int ENGINNER = 0;static final int SALESMAN = 1;static final int MANAGER = 2;public abstract int getTypeCode();public abstract int payAmount(Employee employee);public static EmpoyeeType newType(int code) {switch (code) {case ENGINNER: return new Engineer();case SALESMAN: return new Salesman();case MANAGER: return new Manager();default:throw new IllegalArgumentException("Incorrect Employee Code.");}} }public class Engineer extends EmpoyeeType {public int getTypeCode() {return Employee.ENGINNER;}public int payAmount(Employee employee) {return employee.getMontylySalary();} }public class Manager extends EmpoyeeType {public int getTypeCode() {return Employee.MANAGER;}public int payAmount(Employee employee) {return employee.getMontylySalary() + employee.getBonus();} }public class Salesman extends EmpoyeeType {public int getTypeCode() {return Employee.SALESMAN;}public int payAmount(Employee employee) {return employee.getMontylySalary() + employee.getCommission();} }public class Employee {private EmpoyeeType type;// 月薪.private int montylySalary;// 傭金.private int commission;// 獎金.private int bonus;public Employee(int type) {this.type = type;}public int payAmount() {return getType().payAmount(this);}public EmpoyeeType getType() {return type;} }總結
以上是生活随笔為你收集整理的Replace Type Code with State/Strategy(以State/Strategy取代类型码)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: html盒子嵌套居中,css在盒子中垂直
- 下一篇: Consolidate Conditio