• 状态模式详解


    解决的问题:无限的if elseif else

    基本版:

    1. package state;
    2. public class V1 {
    3. public static void main(String[] args) {
    4. SayHi sayhi = new SayHi();
    5. sayhi.setTime(1);
    6. sayhi.doAction();
    7. sayhi.setTime(9);
    8. sayhi.doAction();
    9. sayhi.setTime(16);
    10. sayhi.doAction();
    11. sayhi.setTime(20);
    12. sayhi.doAction();
    13. }
    14. }
    15. //打招呼的类
    16. class SayHi {
    17. Integer time;
    18. public Integer getTime() {
    19. return time;
    20. }
    21. public void setTime(Integer time) {
    22. this.time = time;
    23. }
    24. public void doAction() {
    25. if((time >= 0 && time < 4) || (time >= 19 && time <= 24)) {
    26. System.out.println("晚上好!");
    27. }else if(time >= 4 && time < 12) {
    28. System.out.println("上午好!");
    29. }else if(time >= 12 && time < 19) {
    30. System.out.println("下午好!");
    31. }
    32. }
    33. }

    状态模式版本:

    1. package state;
    2. public class V2 {
    3. public static void main(String[] args) {
    4. SayHi2 sayhi = new SayHi2();
    5. sayhi.setTime(1);
    6. sayhi.doAction();
    7. sayhi.setTime(9);
    8. sayhi.doAction();
    9. sayhi.setTime(16);
    10. sayhi.doAction();
    11. sayhi.setTime(20);
    12. sayhi.doAction();
    13. }
    14. }
    15. //环境类 也就是存放状态的类
    16. class SayHi2 {
    17. private Integer time;
    18. private State state;
    19. public SayHi2() {
    20. state = new EveningState();
    21. }
    22. public State getState() {
    23. return state;
    24. }
    25. public void setState(State state) {
    26. this.state = state;
    27. }
    28. public Integer getTime() {
    29. return time;
    30. }
    31. public void setTime(Integer time) {
    32. this.time = time;
    33. this.setState(new EveningState());
    34. }
    35. public void doAction() {
    36. state.doAction(this);
    37. }
    38. }
    39. interface State {
    40. void doAction(SayHi2 sayHi2);
    41. }
    42. class EveningState implements State {
    43. @Override
    44. public void doAction(SayHi2 sayHi2) {
    45. if((sayHi2.getTime() >= 0 && sayHi2.getTime() < 4) || (sayHi2.getTime() >= 19 && sayHi2.getTime() <= 24)) {
    46. System.out.println("晚上好!");
    47. }else {
    48. sayHi2.setState(new MorningState());
    49. sayHi2.doAction();
    50. }
    51. }
    52. }
    53. class MorningState implements State {
    54. @Override
    55. public void doAction(SayHi2 sayHi2) {
    56. if(sayHi2.getTime() >= 4 && sayHi2.getTime() < 12) {
    57. System.out.println("上午好!");
    58. }else {
    59. sayHi2.setState(new AfternoonState());
    60. sayHi2.doAction();
    61. }
    62. }
    63. }
    64. class AfternoonState implements State {
    65. @Override
    66. public void doAction(SayHi2 sayHi2) {
    67. if(sayHi2.getTime() >= 12 && sayHi2.getTime() < 19) {
    68. System.out.println("下午好!");
    69. }else {
    70. System.out.println("我与您有时间差,不知道怎么向你打招呼");
    71. }
    72. }
    73. }

    状态接口uml图:

     

  • 相关阅读:
    Python期末复习题:字符串与产生随机数
    11月9日,每日信息差
    一文读懂 Linux 网络 IO 模型
    ORB-SLAM2从理论到代码实现(九):LocalMapping程序详解
    Java POI操作excel
    【Springboot】微服务中使用腾讯云T-Sec天御对文本及图片内容进行安全检测
    变频器调试工具:ABB Drive Composer
    软件测试之TCP、HTTP协议必知必会,面试必备
    [附源码]计算机毕业设计springboot高校学生宿舍管理系统
    外汇天眼:外汇中的做空和做多是什么意思?有什么区别?
  • 原文地址:https://blog.csdn.net/xf552527/article/details/127106683