文档章节

设计模式-状态模式

月夜筱风
 月夜筱风
发布于 2017/09/01 18:02
字数 1672
阅读 12
收藏 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
闵行
程序员

暂无文章

命令行新建Maven多项目

参考地址 # DgroupId 可以理解为包名# DartifactId 可以理解为项目名mvn archetype:generate -DgroupId=cn.modfun -DartifactId=scaffold -DarchetypeArtifactId=maven-archetype-quickst......

阿白
55分钟前
1
0
OSChina 周四乱弹 —— 上帝对我单身年限的惩罚越来越长了

Osc乱弹歌单(2018)请戳(这里) 【今日歌曲】 @达尔文:分享张卫健的单曲《身体健康》 《身体健康》- 张卫健 手机党少年们想听歌,请使劲儿戳(这里) 昨天是重阳节咯, 可惜小小编辑总是晚...

小小编辑
57分钟前
10
0
django rest framework 外键序列化方法与问题总结

django rest framework 外键序列化方法与问题总结 当借口中需要出现一对多关系的时候,我们可以用rest_framwork的序列化功能来处理,代码如下. # models.pyfrom django.db import modelscl...

_Change_
昨天
3
0
SingleNumber136 leetCode

Given a non-empty array of integers, every element appears twice except for one. Find that single one. Note: Your algorithm should have a linear runtime complexity. Could you im......

woshixin
昨天
3
0
String ,  StringBuffer ,  StringBuilder的区别

String , StringBuffer , StringBuilder的区别 String 首先,String 是用来表示一个字符串常量的,它是一个不可变对象,意味着,一旦我们创建了某个字符串之后,就不能再改变它的值了,我们可...

tsmyk0715
昨天
6
0

没有更多内容

加载失败,请刷新页面

加载更多

返回顶部
顶部