设计模式-状态模式
设计模式-状态模式
月夜筱风 发表于5个月前
设计模式-状态模式
  • 发表于 5个月前
  • 阅读 10
  • 收藏 0
  • 点赞 0
  • 评论 0

标题:腾讯云 新注册用户域名抢购1元起>>>   

一,定义

输入图片说明

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
×
月夜筱风
如果觉得我的文章对您有用,请随意打赏。您的支持将鼓励我继续创作!
* 金额(元)
¥1 ¥5 ¥10 ¥20 其他金额
打赏人
留言
* 支付类型
微信扫码支付
打赏金额:
已支付成功
打赏金额: