• 设计模式之状态模式


    设计模式之状态模式

    跟着大鸟的节奏学学状态模式

    题目:每天的不同时段,会产生不同的工作状态。要求用代码实现:

    说来确实也很是惭愧,学了很久的OOP,却还在用面向过程的方法来写代码。代码实在太low,就不贴了。

    下面开始用面向对象的方式开始编写

    首先建立一个Work类

    package com.zww.state.test;
    
    public class Work {
        private boolean workFinish;
        private int hour;
    
        public boolean isWorkFinish() {
            return workFinish;
        }
    
        public void setWorkFinish(boolean workFinish) {
            this.workFinish = workFinish;
        }
    
        public int getHour() {
            return hour;
        }
    
        public void setHour(int hour) {
            this.hour = hour;
        }
    
        public void writeProgram() {
            if (hour < 12) {
                System.out.println("hour:" + hour);
            } else if (hour < 13) {
                System.out.println("hour:" + hour);
            } else if (hour < 17) {
                System.out.println("hour:" + hour);
            } else {
                if (workFinish) {
                    System.err.println("下班" + hour);
                } else {
                    if (hour < 21) {
                        System.out.println("hour:" + hour);
                    } else {
                        System.out.println("sleep hour:" + hour);
                    }
                }
            }
        }
    
    }

    其次建立Test测试类

    package com.zww.state.test;
    
    public class Test {
    
        public static void main(String[] args) {
            // TODO Auto-generated method stub
            Work work=new Work();
            work.setHour(9);
            work.writeProgram();
            work.setHour(13);
            work.writeProgram();
            work.setHour(23);
            work.setWorkFinish(false);
            work.writeProgram();
        
        }
    
    }

    到这里差不多也能看出来有点怪怪的样子了。对头,就是Work类里面的writeProgram方法过长。当我们想要再添加判断的时候,不得不再在if分支里面添加,这很明显是违背了“开放--封闭原则”的。所以我们应该将分支变成一个个类,增加时就不会再影响到其他类了。

    下面 状态模式 便可以登场了!

    什么是状态模式?

    状态模式,当一个对象的内在状态改变时允许改变其行为,这个对象看起来像是改变了其类。

    状态模式主要解决的是当控制一个对象状态的条件表达式过于复杂的情况,把状态的判断逻辑转移到表示不同状态的一系列类当中,可以把复杂的判断逻辑简化。

    UML

    下面便通过状态模式重新实现功能

    首先建立一个Work类

    package com.zww.state.test2;
    
    public class Work {
        private boolean workFinish;
        private int hour;
        private State state;
        
        
        public Work() {
            super();
            state = new ForenoonState();
        }
    
        public State getState() {
            return state;
        }
    
        public void setState(State state) {
            this.state = state;
        }
    
        public boolean isWorkFinish() {
            return workFinish;
        }
    
        public void setWorkFinish(boolean workFinish) {
            this.workFinish = workFinish;
        }
    
        public int getHour() {
            return hour;
        }
    
        public void setHour(int hour) {
            this.hour = hour;
        }
    
        public void writeProgram() {
            state.writeProgram(this);
        }
    
    }

    其次再建立一个State接口

    package com.zww.state.test2;
    
    public interface State {
        void writeProgram(Work work);
    }

    下面分别实现各种状态类

    package com.zww.state.test2;
    
    public class ForenoonState implements State {
    
        @Override
        public void writeProgram(Work work) {
            // TODO Auto-generated method stub
            if (work.getHour()<12) {
                System.out.println("hour:"+work.getHour());
            } else {
                work.setState(new NoonState()); //转入中午工作状态
                work.writeProgram();
            }
        }
    
    }
    package com.zww.state.test2;
    
    public class NoonState implements State {
    
        @Override
        public void writeProgram(Work work) {
            // TODO Auto-generated method stub
            if (work.getHour()<13) {
                System.out.println("hour:"+work.getHour());
            }else {
                work.setState(new AfternoonState()); //转入下午工作状态
                work.writeProgram();
            }
        }
    
    }
    package com.zww.state.test2;
    
    public class AfternoonState implements State {
    
        public void writeProgram(Work work) {
            // TODO Auto-generated method stub
            if (work.getHour()<17) {
                System.out.println("hour:"+work.getHour());
            }else {
                work.setState(new EveningState());
                work.writeProgram();
            }
        }
    
    }
    package com.zww.state.test2;
    
    public class EveningState implements State {
    
        @Override
        public void writeProgram(Work work) {
            // TODO Auto-generated method stub
            if (work.isWorkFinish()) {
                work.setState(new RestState());
                work.writeProgram();
            }else {
                if (work.getHour()<21) {
                    System.out.println("hour:"+work.getHour());
                }else {
                    work.setState(new SleepingState());
                    work.writeProgram();
                }
            }
        }
    
    }
    package com.zww.state.test2;
    
    public class RestState implements State {
    
        @Override
        public void writeProgram(Work work) {
            // TODO Auto-generated method stub
            System.out.println("hour:"+work.getHour());
        }
    
    }
    package com.zww.state.test2;
    
    public class SleepingState implements State {
    
        @Override
        public void writeProgram(Work work) {
            // TODO Auto-generated method stub
            System.out.println("hour:"+work.getHour());
        }
    
    }

    最后建立测试类

    package com.zww.state.test2;
    
    public class Test {
    
        public static void main(String[] args) {
            // TODO Auto-generated method stub
            Work work=new Work();
            work.setHour(9);
            work.writeProgram();
            work.setHour(13);
            work.writeProgram();
            work.setHour(23);
            work.setWorkFinish(false);
            work.writeProgram();
        
        }
    
    }

    如果此时要增加一个分支要求,“员工必须要在20点之前离开公司”,那么只需要再添加一个强制类,并改动一下“傍晚工作状态类”的判断分支即可

    《初学大话设计模式》

  • 相关阅读:
    STM32学习笔记1(ADC多通道采样)
    SQL Server索引管理之六大铁律
    如何激励员工?
    第一周学习计划
    山寨STL实现之traits,construct&destruct
    山寨STL实现之内存池
    山寨STL实现之vector
    山寨STL实现之allocator
    Python 开发环境搭建
    Java | Python 流程控制对比
  • 原文地址:https://www.cnblogs.com/zhaoww/p/5088472.html
Copyright © 2020-2023  润新知