日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

Replace Type Code with State/Strategy(以State/Strategy取代类型码)

發布時間:2024/7/23 编程问答 40 豆豆
生活随笔 收集整理的這篇文章主要介紹了 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取代类型码)的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。