文档章节

设计模式-状态模式

月夜筱风
 月夜筱风
发布于 2017/09/01 18:02
字数 1672
阅读 11
收藏 0
点赞 0
评论 0

一,定义

输入图片说明

1.1. 分析

首先,有很多具体的状态处理类

就是用传入的参数去判断用哪种具体的状态处理类。在具体的状态处理类中来层层判断最后是哪个具体状态类去处理,不会出现很多if else在一个类的情况了(其实是把if else分散了)。最后,提供了统一的入口!!!

1.2. 适用

对于参数是那种时间或者即使以后增加条件思路也清晰明了的适用

输入图片说明

二,代码

  • 2.1目录

输入图片说明

  • 2.2,具体代码
package com.boot.lijunhao.zhaungtai;

/**
 * 共有的方法抽出来
 * Created by lijunhao on 2017/9/1.
 */
public abstract class State {

    public abstract void doSomething(InClass content);
}

package com.boot.lijunhao.zhaungtai;

import lombok.extern.slf4j.Slf4j;

/**
 * int < 0 --->>>  0-10
 * Created by lijunhao on 2017/9/1.
 */
@Slf4j
public class DoSomethingFromState1 extends State {

    @Override
    public void doSomething(InClass inClass) {
        if(inClass.getInteger()<0){
            log.info("doSomething在类(DoSomethingFromState1.java:14)行:{}","这是小于0的判断"+inClass.getInteger());
        }else {
            //转移到0-10的判断
            inClass.setState(new DoSomethingFromState2());
        }
    }
}

package com.boot.lijunhao.zhaungtai;

import lombok.extern.slf4j.Slf4j;

/**
 * int 1-10  --->>> 11-20
 * Created by lijunhao on 2017/9/1.
 */
@Slf4j
public class DoSomethingFromState2 extends State {

    @Override
    public void doSomething(InClass inClass) {
        if(inClass.getInteger()>=0&&inClass.getInteger()<=10){
            log.info("doSomething在类(DoSomethingFromState2.java:15)行:{}","这是0-10的判断"+inClass.getInteger());
        }else {
            //转移到0-10的判断
            inClass.setState(new DoSomethingFromState3());
        }
    }
}

package com.boot.lijunhao.zhaungtai;

import lombok.extern.slf4j.Slf4j;

/**
 * int 11-20  --->>> 21-30
 * Created by lijunhao on 2017/9/1.
 */
@Slf4j
public class DoSomethingFromState3 extends State {

    @Override
    public void doSomething(InClass inClass) {
        if(inClass.getInteger()>=11&&inClass.getInteger()<=20){
            log.info("doSomething在类(DoSomethingFromState3.java:15)行:{}","这是11-20的判断"+inClass.getInteger());
        }else {
            //转移到11-20的判断
            inClass.setState(new DoSomethingFromState4());
        }
    }
}

package com.boot.lijunhao.zhaungtai;

import lombok.extern.slf4j.Slf4j;

/**
 * int 21-50  --->>> >50
 * Created by lijunhao on 2017/9/1.
 */
@Slf4j
public class DoSomethingFromState4 extends State {

    @Override
    public void doSomething(InClass inClass) {
        if(inClass.getInteger()>=21&&inClass.getInteger()<=50){
            log.info("doSomething在类(DoSomethingFromState4.java:15)行:{}","这是21-50的判断"+inClass.getInteger());
        }else {
            //转移到31-50的判断
            inClass.setState(new DoSomethingFromState5());
        }
    }
}

package com.boot.lijunhao.zhaungtai;

import lombok.extern.slf4j.Slf4j;

/**
 * int 31-50  --->>> 50-无穷大
 * Created by lijunhao on 2017/9/1.
 */
@Slf4j
public class DoSomethingFromState5 extends State {

    @Override
    public void doSomething(InClass inClass) {
        if(inClass.getInteger()>=51){
            log.info("doSomething在类(DoSomethingFromState5.java:15)行:{}","这是大于50的判断"+inClass.getInteger());
        }
    }
}

package com.boot.lijunhao.zhaungtai;

/**
 * Created by lijunhao on 2017/9/1.
 */
public class InClass {

    private State state;

    private Integer integer;

    public Integer getInteger() {
        return integer;
    }

    public void setInteger(Integer integer) {
        this.integer = integer;
    }

    //初始的具体状态类,一般是逻辑的起点
    public InClass() {
        state = new DoSomethingFromState1();
    }

    public State getState() {
        return state;
    }

    public void setState(State state) {
        this.state = state;
    }

    public void getResout(){
        state.doSomething(this);
    }
}

package com.boot.lijunhao.zhaungtai;

/**
 * Created by lijunhao on 2017/9/1.
 */
public class Start {

    public static void main(String[] args) {
        InClass inClass = new InClass();
        for (int i = -1; i < 55; i++) {
            inClass.setInteger(i);
            inClass.getResout();
        }
    }
}

  • 2.3结果
17:54:46.495 [main] INFO com.boot.lijunhao.zhaungtai.DoSomethingFromState1 - doSomething在类(DoSomethingFromState1.java:14)行:这是小于0的判断-1
17:54:46.495 [main] INFO com.boot.lijunhao.zhaungtai.DoSomethingFromState2 - doSomething在类(DoSomethingFromState2.java:15)行:这是0-10的判断1
17:54:46.495 [main] INFO com.boot.lijunhao.zhaungtai.DoSomethingFromState2 - doSomething在类(DoSomethingFromState2.java:15)行:这是0-10的判断2
17:54:46.495 [main] INFO com.boot.lijunhao.zhaungtai.DoSomethingFromState2 - doSomething在类(DoSomethingFromState2.java:15)行:这是0-10的判断3
17:54:46.495 [main] INFO com.boot.lijunhao.zhaungtai.DoSomethingFromState2 - doSomething在类(DoSomethingFromState2.java:15)行:这是0-10的判断4
17:54:46.495 [main] INFO com.boot.lijunhao.zhaungtai.DoSomethingFromState2 - doSomething在类(DoSomethingFromState2.java:15)行:这是0-10的判断5
17:54:46.495 [main] INFO com.boot.lijunhao.zhaungtai.DoSomethingFromState2 - doSomething在类(DoSomethingFromState2.java:15)行:这是0-10的判断6
17:54:46.495 [main] INFO com.boot.lijunhao.zhaungtai.DoSomethingFromState2 - doSomething在类(DoSomethingFromState2.java:15)行:这是0-10的判断7
17:54:46.495 [main] INFO com.boot.lijunhao.zhaungtai.DoSomethingFromState2 - doSomething在类(DoSomethingFromState2.java:15)行:这是0-10的判断8
17:54:46.495 [main] INFO com.boot.lijunhao.zhaungtai.DoSomethingFromState2 - doSomething在类(DoSomethingFromState2.java:15)行:这是0-10的判断9
17:54:46.495 [main] INFO com.boot.lijunhao.zhaungtai.DoSomethingFromState2 - doSomething在类(DoSomethingFromState2.java:15)行:这是0-10的判断10
17:54:46.495 [main] INFO com.boot.lijunhao.zhaungtai.DoSomethingFromState3 - doSomething在类(DoSomethingFromState3.java:15)行:这是11-20的判断12
17:54:46.495 [main] INFO com.boot.lijunhao.zhaungtai.DoSomethingFromState3 - doSomething在类(DoSomethingFromState3.java:15)行:这是11-20的判断13
17:54:46.495 [main] INFO com.boot.lijunhao.zhaungtai.DoSomethingFromState3 - doSomething在类(DoSomethingFromState3.java:15)行:这是11-20的判断14
17:54:46.495 [main] INFO com.boot.lijunhao.zhaungtai.DoSomethingFromState3 - doSomething在类(DoSomethingFromState3.java:15)行:这是11-20的判断15
17:54:46.495 [main] INFO com.boot.lijunhao.zhaungtai.DoSomethingFromState3 - doSomething在类(DoSomethingFromState3.java:15)行:这是11-20的判断16
17:54:46.495 [main] INFO com.boot.lijunhao.zhaungtai.DoSomethingFromState3 - doSomething在类(DoSomethingFromState3.java:15)行:这是11-20的判断17
17:54:46.495 [main] INFO com.boot.lijunhao.zhaungtai.DoSomethingFromState3 - doSomething在类(DoSomethingFromState3.java:15)行:这是11-20的判断18
17:54:46.495 [main] INFO com.boot.lijunhao.zhaungtai.DoSomethingFromState3 - doSomething在类(DoSomethingFromState3.java:15)行:这是11-20的判断19
17:54:46.495 [main] INFO com.boot.lijunhao.zhaungtai.DoSomethingFromState3 - doSomething在类(DoSomethingFromState3.java:15)行:这是11-20的判断20
17:54:46.495 [main] INFO com.boot.lijunhao.zhaungtai.DoSomethingFromState4 - doSomething在类(DoSomethingFromState4.java:15)行:这是21-50的判断22
17:54:46.495 [main] INFO com.boot.lijunhao.zhaungtai.DoSomethingFromState4 - doSomething在类(DoSomethingFromState4.java:15)行:这是21-50的判断23
17:54:46.495 [main] INFO com.boot.lijunhao.zhaungtai.DoSomethingFromState4 - doSomething在类(DoSomethingFromState4.java:15)行:这是21-50的判断24
17:54:46.495 [main] INFO com.boot.lijunhao.zhaungtai.DoSomethingFromState4 - doSomething在类(DoSomethingFromState4.java:15)行:这是21-50的判断25
17:54:46.495 [main] INFO com.boot.lijunhao.zhaungtai.DoSomethingFromState4 - doSomething在类(DoSomethingFromState4.java:15)行:这是21-50的判断26
17:54:46.495 [main] INFO com.boot.lijunhao.zhaungtai.DoSomethingFromState4 - doSomething在类(DoSomethingFromState4.java:15)行:这是21-50的判断27
17:54:46.495 [main] INFO com.boot.lijunhao.zhaungtai.DoSomethingFromState4 - doSomething在类(DoSomethingFromState4.java:15)行:这是21-50的判断28
17:54:46.495 [main] INFO com.boot.lijunhao.zhaungtai.DoSomethingFromState4 - doSomething在类(DoSomethingFromState4.java:15)行:这是21-50的判断29
17:54:46.510 [main] INFO com.boot.lijunhao.zhaungtai.DoSomethingFromState4 - doSomething在类(DoSomethingFromState4.java:15)行:这是21-50的判断30
17:54:46.510 [main] INFO com.boot.lijunhao.zhaungtai.DoSomethingFromState4 - doSomething在类(DoSomethingFromState4.java:15)行:这是21-50的判断31
17:54:46.510 [main] INFO com.boot.lijunhao.zhaungtai.DoSomethingFromState4 - doSomething在类(DoSomethingFromState4.java:15)行:这是21-50的判断32
17:54:46.510 [main] INFO com.boot.lijunhao.zhaungtai.DoSomethingFromState4 - doSomething在类(DoSomethingFromState4.java:15)行:这是21-50的判断33
17:54:46.510 [main] INFO com.boot.lijunhao.zhaungtai.DoSomethingFromState4 - doSomething在类(DoSomethingFromState4.java:15)行:这是21-50的判断34
17:54:46.510 [main] INFO com.boot.lijunhao.zhaungtai.DoSomethingFromState4 - doSomething在类(DoSomethingFromState4.java:15)行:这是21-50的判断35
17:54:46.510 [main] INFO com.boot.lijunhao.zhaungtai.DoSomethingFromState4 - doSomething在类(DoSomethingFromState4.java:15)行:这是21-50的判断36
17:54:46.510 [main] INFO com.boot.lijunhao.zhaungtai.DoSomethingFromState4 - doSomething在类(DoSomethingFromState4.java:15)行:这是21-50的判断37
17:54:46.510 [main] INFO com.boot.lijunhao.zhaungtai.DoSomethingFromState4 - doSomething在类(DoSomethingFromState4.java:15)行:这是21-50的判断38
17:54:46.510 [main] INFO com.boot.lijunhao.zhaungtai.DoSomethingFromState4 - doSomething在类(DoSomethingFromState4.java:15)行:这是21-50的判断39
17:54:46.510 [main] INFO com.boot.lijunhao.zhaungtai.DoSomethingFromState4 - doSomething在类(DoSomethingFromState4.java:15)行:这是21-50的判断40
17:54:46.510 [main] INFO com.boot.lijunhao.zhaungtai.DoSomethingFromState4 - doSomething在类(DoSomethingFromState4.java:15)行:这是21-50的判断41
17:54:46.510 [main] INFO com.boot.lijunhao.zhaungtai.DoSomethingFromState4 - doSomething在类(DoSomethingFromState4.java:15)行:这是21-50的判断42
17:54:46.510 [main] INFO com.boot.lijunhao.zhaungtai.DoSomethingFromState4 - doSomething在类(DoSomethingFromState4.java:15)行:这是21-50的判断43
17:54:46.510 [main] INFO com.boot.lijunhao.zhaungtai.DoSomethingFromState4 - doSomething在类(DoSomethingFromState4.java:15)行:这是21-50的判断44
17:54:46.510 [main] INFO com.boot.lijunhao.zhaungtai.DoSomethingFromState4 - doSomething在类(DoSomethingFromState4.java:15)行:这是21-50的判断45
17:54:46.510 [main] INFO com.boot.lijunhao.zhaungtai.DoSomethingFromState4 - doSomething在类(DoSomethingFromState4.java:15)行:这是21-50的判断46
17:54:46.510 [main] INFO com.boot.lijunhao.zhaungtai.DoSomethingFromState4 - doSomething在类(DoSomethingFromState4.java:15)行:这是21-50的判断47
17:54:46.510 [main] INFO com.boot.lijunhao.zhaungtai.DoSomethingFromState4 - doSomething在类(DoSomethingFromState4.java:15)行:这是21-50的判断48
17:54:46.510 [main] INFO com.boot.lijunhao.zhaungtai.DoSomethingFromState4 - doSomething在类(DoSomethingFromState4.java:15)行:这是21-50的判断49
17:54:46.510 [main] INFO com.boot.lijunhao.zhaungtai.DoSomethingFromState4 - doSomething在类(DoSomethingFromState4.java:15)行:这是21-50的判断50
17:54:46.510 [main] INFO com.boot.lijunhao.zhaungtai.DoSomethingFromState5 - doSomething在类(DoSomethingFromState5.java:15)行:这是大于50的判断52
17:54:46.510 [main] INFO com.boot.lijunhao.zhaungtai.DoSomethingFromState5 - doSomething在类(DoSomethingFromState5.java:15)行:这是大于50的判断53
17:54:46.510 [main] INFO com.boot.lijunhao.zhaungtai.DoSomethingFromState5 - doSomething在类(DoSomethingFromState5.java:15)行:这是大于50的判断54

© 著作权归作者所有

共有 人打赏支持
月夜筱风
粉丝 0
博文 53
码字总数 19574
作品 0
闵行
程序员

暂无相关文章

开启Swarm集群以及可视化管理

在搭建的两台coreos服务器上开启swarm集群 前置条件: docker均开启2375端口 同一个局域网内 主服务器上安装Portainer容器 安装Portainer容器执行: docker run -d -p 9000:9000 --restart=a...

ykbj ⋅ 6分钟前 ⋅ 0

单例设计模式

1、单例模式确保某一个类只有一个实例,而且自行实例化并向整个系统提供这个实例 2、饿汉式单例类 在这个类被加载时,静态变量instance会被初始化,此时类的私有构造子会被调用 饿汉式是典型...

职业搬砖20年 ⋅ 11分钟前 ⋅ 0

前端基础(四):前端国际规范收集

字数:1142 阅读时间:5分钟 前言 由于前端技术的灵活性和杂乱性,导致网上的许多解决方案不够全面甚至是完全错误,容易起到误导作用。所以,我对搜索到的解决方案往往是存疑态度。那么,如何...

老司机带你撸代码 ⋅ 13分钟前 ⋅ 0

Failed to open/create Network-VirtualBox Host-Only

虚拟机版本 : Oracle Vm VirtualBox 5.2.12 报错时机:开网卡二,重启虚拟机报错 "Failed to open/create the internal network 'HostInterfaceNetworking-VirtualBox Host-Only Ethernet Ada......

p至尊宝 ⋅ 16分钟前 ⋅ 0

三分钟学会如何在函数计算中使用 puppeteer

摘要: 使用 puppeteer 结合函数计算,可以快速的构建弹性的服务完成各种功能,包括:生成网页截图或者 PDF、高级爬虫,可以爬取大量异步渲染内容的网页、模拟键盘输入、表单自动提交、登录网...

阿里云云栖社区 ⋅ 20分钟前 ⋅ 0

springMVC接收表单时 Bean对象有Double Int Char类型的处理

前台ajax提交表单price为double类型 后台controller就介绍不到 400错误 前台 实体类: public class ReleaseMapIconConfig{ private String id; private long maxValue; private long minVal......

废柴 ⋅ 22分钟前 ⋅ 0

ZOOKEEPER安装

工作需要在ubuntu上配置了一个zookeeper集群,有些问题记录下来。 1. zookeeper以来java,所以首先要安装java。但是ubuntu系统有自带的jdk,需要通过命令切换java版本: $ sudo update-alter...

恰东 ⋅ 25分钟前 ⋅ 0

linux 进程地址空间的一步步探究

我们知道,在32位机器上linux操作系统中的进程的地址空间大小是4G,其中0-3G是用户空间,3G-4G是内核空间。其实,这个4G的地址空间是不存在的,也就是我们所说的虚拟内存空间。 那虚拟内存空间...

HelloRookie ⋅ 25分钟前 ⋅ 0

myatis #{}与${}区别及原理

https://blog.csdn.net/wo541075754/article/details/54292751

李道福 ⋅ 28分钟前 ⋅ 0

三分钟学会如何在函数计算中使用 puppeteer

摘要: 使用 puppeteer 结合函数计算,可以快速的构建弹性的服务完成各种功能,包括:生成网页截图或者 PDF、高级爬虫,可以爬取大量异步渲染内容的网页、模拟键盘输入、表单自动提交、登录网...

猫耳m ⋅ 29分钟前 ⋅ 0

没有更多内容

加载失败,请刷新页面

加载更多

下一页

返回顶部
顶部