文档章节

设计模式-状态模式

月夜筱风
 月夜筱风
发布于 2017/09/01 18:02
字数 1672
阅读 11
收藏 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
博文 55
码字总数 19574
作品 0
闵行
程序员

暂无文章

(一)软件测试专题——之Linux常用命令篇01

本文永久更新地址:https://my.oschina.net/bysu/blog/1931063 【若要到岸,请摇船:开源中国 不最醉不龟归】 Linux的历史之类的很多书籍都习惯把它的今生来世,祖宗十八代都扒出来,美其名曰...

不最醉不龟归
20分钟前
3
0
蚂蚁金服Java开发三面

8月20号晚上8点进行了蚂蚁金服Java开发岗的第三面,下面开始: 自我介绍(要求从实践过程以及技术背景角度着重介绍) 实习经历,说说你在公司实习所做的事情,学到了什么 关于你们的交易平台...

edwardGe
27分钟前
7
0
TypeScript基础入门 - 函数 - this(三)

转载 TypeScript基础入门 - 函数 - this(三) 项目实践仓库 https://github.com/durban89/typescript_demo.gittag: 1.2.4 为了保证后面的学习演示需要安装下ts-node,这样后面的每个操作都能...

durban
36分钟前
0
0
Spark core基础

Spark RDD的五大特性 RDD是由一系列的Partition组成的,如果Spark计算的数据是在HDFS上那么partition个数是与block数一致(大多数情况) RDD是有一系列的依赖关系,有利于Spark计算的容错 RDD中每...

张泽立
44分钟前
0
0
如何搭建Keepalived+Nginx+Tomcat高可用负载均衡架构

一.概述 初期的互联网企业由于业务量较小,所以一般单机部署,实现单点访问即可满足业务的需求,这也是最简单的部署方式,但是随着业务的不断扩大,系统的访问量逐渐的上升,单机部署的模式已...

Java大蜗牛
59分钟前
0
0

没有更多内容

加载失败,请刷新页面

加载更多

下一页

返回顶部
顶部