本文承接上文,使用面向对象重构之-从过程式设计到面向对象。
新需求来了,Manager申请Leave的过程和普通员工不一样,这个时候就需要修改几处地方:
1. 把ApplyLeave的这个函数virtual一下,abstract awaypublic class Employee { ... public virtual bool ApplyLeave(Leave leave) { ... } ... }
public class Manager : Employee { ... public override bool ApplyLeave(Leave leave) { //... manager apply leave logic } ... }
public class Employee { ... private void ApplyLeaveStep1() { ... } private void ApplyLeaveStep2() { ... } protected virtual void ExtraSteps() { // do nothing } public virtual bool ApplyLeave(Leave leave) { Step1(); Step2(); ExtraSteps(); } ... } public class Manager : Employee { ... public override bool ExtraSteps() { //... manager apply leave logic } ... }