全文一共1543字,預計閱讀時間10分鐘程序員
** * 程序員類. * * @author jialin.li * @date 2019-12-30 17:38 */ public class Programmer { private int empiricalValue; public void setEmpiricalValue(int empiricalValue) { this.empiricalValue = empiricalValue; } public void evaluate() { if(empiricalValue < 100){ System.out.println("MT"); }else if(empiricalValue < 200){ System.out.println("開發助理"); }else if(empiricalValue < 300){ System.out.println("初級程序員"); }else if(empiricalValue < 400){ System.out.println("中級程序員"); }else if(empiricalValue < 500){ System.out.println("高級程序員"); }else if(empiricalValue < 600){ System.out.println("技術專家"); } } }
/** * 客戶端. * * @author jialin.li * @date 2019-12-30 18:28 */ public class Main { public static void main(String[] args) { Programmer programmer = new Programmer(); programmer.setEmpiricalValue(50); programmer.evaluate(); programmer.setEmpiricalValue(150); programmer.evaluate(); programmer.setEmpiricalValue(250); programmer.evaluate(); programmer.setEmpiricalValue(350); programmer.evaluate(); programmer.setEmpiricalValue(450); programmer.evaluate(); programmer.setEmpiricalValue(550); programmer.evaluate(); } }
MT
開發助理
初級程序員
中級程序員
高級程序員
技術專家
/** * 狀態處理接口. * * @author jialin.li * @date 2019-12-30 19:23 */ public interface State { void handle(Programmer programmer); }
/** * 程序員類. * * @author jialin.li * @date 2019-12-30 17:38 */ public class Programmer { /** * 經驗值 */ private int empiricalValue; /** * 當前狀態 */ private State state = new MTState(); public void setEmpiricalValue(int empiricalValue) { this.empiricalValue = empiricalValue; } public int getEmpiricalValue() { return empiricalValue; } public void setState(State state) { this.state = state; } public void handle() { state.handle(this); } }
/** * MT. * * @author jialin.li * @date 2019-12-30 19:30 */ public class MTState implements State { @Override public void handle(Programmer programmer) { int empiricalValue = programmer.getEmpiricalValue(); if (empiricalValue < 100) { System.out.println("MT"); } else { State juniorProgrammer = new JuniorProgrammer(); programmer.setState(juniorProgrammer); juniorProgrammer.handle(programmer); } } }
/** * 助理程序員. * * @author jialin.li * @date 2019-12-30 19:33 */ public class ProgrammerAssistant implements State{ @Override public void handle(Programmer programmer) { int empiricalValue = programmer.getEmpiricalValue(); if(empiricalValue < 200){ System.out.println("開發助理"); }else{ JuniorProgrammer juniorProgrammer = new JuniorProgrammer(); programmer.setState(juniorProgrammer); juniorProgrammer.handle(programmer); } } }
/** * 初級程序員. * * @author jialin.li * @date 2019-12-30 19:31 */ public class JuniorProgrammer implements State { @Override public void handle(Programmer programmer) { int empiricalValue = programmer.getEmpiricalValue(); if (empiricalValue < 300) { System.out.println("初級程序員"); } else { State middleProgrammer = new MiddleProgrammer(); programmer.setState(middleProgrammer); middleProgrammer.handle(programmer); } } }
/** * 中級程序員. * * @author jialin.li * @date 2019-12-30 19:32 */ public class MiddleProgrammer implements State { @Override public void handle(Programmer programmer) { int empiricalValue = programmer.getEmpiricalValue(); if (empiricalValue < 400) { System.out.println("中級程序員"); } else { SeniorProgrammer seniorProgrammer = new SeniorProgrammer(); programmer.setState(seniorProgrammer); seniorProgrammer.handle(programmer); } } }
/** * 高級程序員. * * @author jialin.li * @date 2019-12-30 19:34 */ public class SeniorProgrammer implements State { @Override public void handle(Programmer programmer) { int empiricalValue = programmer.getEmpiricalValue(); if (empiricalValue < 500) { System.out.println("高級程序員"); } else { Professor professor = new Professor(); programmer.setState(professor); professor.handle(programmer); } } }
/** * @author jialin.li * @date 2019-12-30 19:35 */ public class Professor implements State { @Override public void handle(Programmer programmer) { System.out.println("技術專家"); } }
/** * @author jialin.li * @date 2019-12-30 19:35 */ public class Professor implements State { @Override public void handle(Programmer programmer) { System.out.println("技術專家"); } }
/** * 客戶端. * * @author jialin.li * @date 2019-12-30 19:36 */ public class Main { public static void main(String[] args) { Programmer programmer = new Programmer(); programmer.setEmpiricalValue(50); programmer.handle(); programmer.setEmpiricalValue(150); programmer.handle(); programmer.setEmpiricalValue(250); programmer.handle(); programmer.setEmpiricalValue(350); programmer.handle(); programmer.setEmpiricalValue(450); programmer.handle(); programmer.setEmpiricalValue(550); programmer.handle(); } }
MT
初級程序員
初級程序員
中級程序員
高級程序員
技術專家
@Override public final synchronized void init() throws LifecycleException { if (!state.equals(LifecycleState.NEW)) { invalidTransition(Lifecycle.BEFORE_INIT_EVENT); } try { setStateInternal(LifecycleState.INITIALIZING, null, false); initInternal(); setStateInternal(LifecycleState.INITIALIZED, null, false); } catch (Throwable t) { handleSubClassException(t, "lifecycleBase.initFail", toString()); } }
private void invalidTransition(String type) throws LifecycleException { String msg = sm.getString("lifecycleBase.invalidTransition", type, toString(), state); throw new LifecycleException(msg); }
期待您的關注、推薦、收藏,同時也期待您的糾錯和批評。編程