文档章节

设计模式-状态模式

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

暂无文章

jquery通过id显示隐藏

var $div3 = $('#div3'); 显示 $div3.show(); 隐藏 $div3.hide();

yan_liu
今天
3
0
《乱世佳人》读书笔记及相关感悟3900字

《乱世佳人》读书笔记及相关感悟3900字: 之前一直听「荔枝」,后来不知怎的转向了「喜马拉雅」,一听就是三年。上班的时候听房产,买房了以后听装修,兴之所至时听旅行,分手后听亲密关系,...

原创小博客
今天
3
0
大数据教程(9.6)map端join实现

上一篇文章讲了mapreduce配合实现join,本节博主将讲述在map端的join实现; 一、需求 实现两个“表”的join操作,其中一个表数据量小,一个表很大,这种场景在实际中非常常见,比如“订单日志...

em_aaron
今天
3
0
cookie与session详解

session与cookie是什么? session与cookie属于一种会话控制技术.常用在身份识别,登录验证,数据传输等.举个例子,就像我们去超市买东西结账的时候,我们要拿出我们的会员卡才会获取优惠.这时...

士兵7
今天
3
0
十万个为什么之为什么大家都说dubbo

Dubbo是什么? 使用背景 dubbo为什么这么流行, 为什么大家都这么喜欢用dubbo; 通过了解分布式开发了解到, 为适应访问量暴增,业务拆分后, 子应用部署在多台服务器上,而多台服务器通过可以通过d...

尾生
今天
4
0

没有更多内容

加载失败,请刷新页面

加载更多

返回顶部
顶部