JAVA--狀態模式

關於設計模式的文章,後續依次會更新,這裏先放個彩蛋。
學習設計模式,一定要有自己的想法並在項目運用起來才能體驗到它強大的好處,不然都是自嗨,俺第一次使用這個模式的時候是在做手遊的時候,當時在軍團戰的時候,會出現多達7個狀態,並且是依次連續出現,不能跳過。這些是題外話了,下面正式分析這個模式。

狀態模式和適用的場景


  • 類中定義了很多條件判斷語句來進行不同的切換,並且下一個狀態的切換是由上一個狀態觸發的
  • 不想把具體的狀態實現細節,數據結構暴露給調用者。
  • 方便新增新的狀態,也即方便擴展

這種說法好似放之四海而皆準,太過朦朧,有點霧裏看花。下面使用一個網上下單的例子進行說明。下單的流程圖如下:

Created with Raphaël 2.2.0開始搜索商品並下單商家付款覈對定單信息發貨結束

在操作訂單的時候,有一個很明顯的特性,就是訂單的狀態不能隨機跳躍,如果從“商品下單之後” 直接跳到 發貨狀態,那你老闆,肯定會殺了你這個程序員來祭天。。。。。

狀態模式角色分析


  • 具體的狀態類,這個可以上面這個流程圖可以看出,在程序的角度,可以把各個狀態抽象爲一個類,這個類就用來處理這個狀態所有的業務
  • 爲了使得具體的狀態類亂來(亂定義函數接口),需要一個小組長來規範它的 行爲(當然具體的行爲,這個小組長無權過問,你只要在它規定的範圍內幹活,就沒事 )接口規範類,暫且給它命名爲:BaseOrderState
  • 有了小組長之後,還需要一個調度員,它的角色就是告訴小組長,什麼時候用什麼狀態,小組長收到通知之後就需要拿出具體的狀態來應對,暫且將這個調度員命名爲:StateContext

具體的代碼實現


調度員類—狀態管理類

public class StateContext {

    private BaseOrderState state;

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

    public void nextState(){
        this.state.nextState(this);
    }

    public void preState(){
        this.state.preState(this);
    }

    public String getCurState(){

        String curState = this.state.getCurState();
        System.out.println("27--------------State:"+curState);
        return this.state.getCurState();
    }
}

小組長類—具體狀態函數接口約束類

/**
 * Created by liuxiaobing on 13/07/2018.
 *  所有訂單的 基類,訂單類,均需要擴展此類
 *  訂單狀態爲:預訂---》 付款---》 覈對--》 發貨
 */

public abstract class BaseOrderState {

    private StateContext context;

    public abstract void nextState(StateContext context);

    public abstract void preState(StateContext context);

    public abstract String getCurState();
}

具體狀態類—預訂狀態

public class BookingState extends BaseOrderState {

    private static BookingState mState;
    public static BookingState getInstance(){
        if(mState == null){
            mState = new BookingState();
        }
        return mState;
    }


    /*預訂狀態下一個狀態 付款狀態*/
    @Override
    public void nextState(StateContext context) {
        context.setState(PayState.getInstance());
    }

    @Override
    public void preState(StateContext context) {

        System.out.println("-----回到了訂單的初始狀態了,沒有上一個狀態");
    }

    @Override
    public String getCurState() {
        return BookingState.class.getName();
    }
}

具體狀態類—付款狀態

public class PayState extends BaseOrderState {

    private static PayState mState;
    public static PayState getInstance(){
        if(mState == null){
            mState = new PayState();
        }
        return mState;
    }


    @Override
    public void nextState(StateContext context) {
        context.setState(CheckState.getInstance());
    }

    @Override
    public void preState(StateContext context) {
        context.setState(BookingState.getInstance());
    }

    @Override
    public String getCurState() {
        return PayState.class.getName();
    }
}

具體狀態類—覈對狀態

public class CheckState extends BaseOrderState{

    private static CheckState mState;

    public static CheckState getInstance(){
        if(mState == null){
            mState = new CheckState();
        }
        return mState;
    }
    @Override
    public void nextState(StateContext context) {
        context.setState(DeliveryState.getInstance());
    }

    @Override
    public void preState(StateContext context) {
        context.setState(PayState.getInstance());
    }

    @Override
    public String getCurState() {
        return CheckState.class.getName();
    }
}

具體狀態類—發貨狀態

public class DeliveryState extends BaseOrderState {

    private static DeliveryState mState;

    public static DeliveryState getInstance(){
        if(mState == null){
            mState = new DeliveryState();
        }
        return mState;
    }

    public DeliveryState(){
        try {
            throw new Exception("請走單例獲取實例模式");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }


    @Override
    public void nextState(StateContext context) {
        System.out.println("29----------訂單流程快要走完了,沒有下一個狀態");
    }

    @Override
    public void preState(StateContext context) {
        context.setState(CheckState.getInstance());
    }

    @Override
    public String getCurState() {
        return DeliveryState.this.getClass().getName();
    }
}

測試類:

package com.example.liuxiaobing.statemodel;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;

import com.example.liuxiaobing.statemodel.state_model2.BookingState;
import com.example.liuxiaobing.statemodel.state_model2.StateContext;
import com.example.liuxiaobing.statemodel.statemodel.BlackState;
import com.example.liuxiaobing.statemodel.statemodel.Context;

public class MainActivity extends AppCompatActivity {

    private StateContext mStateMgr;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();
    }

    private void initView(){
        mStateMgr = new StateContext();
        mStateMgr.setState(BookingState.getInstance());
        findViewById(R.id.next_state).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                mStateMgr.nextState();
            }
        });

        findViewById(R.id.pre_state).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                mStateMgr.preState();
            }
        });

        findViewById(R.id.cur_state).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                mStateMgr.getCurState();
            }
        });
    }
}

佈局文件:

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.example.liuxiaobing.statemodel.MainActivity">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Hello World!"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent" />
    
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:orientation="horizontal">

        <Button
            android:id="@+id/next_state"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="next_state"/>

        <Button
            android:id="@+id/pre_state"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="pre_state"/>

        <Button
            android:id="@+id/cur_state"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="cur_state"/>


    </LinearLayout>

</android.support.constraint.ConstraintLayout>

測試ui:

這裏寫圖片描述

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章