代碼重構----使用java有限狀態機來消除太多的if else判斷

1. 狀態機基本概念

http://zh.wikipedia.org/wiki/%E6%9C%89%E9%99%90%E7%8A%B6%E6%80%81%E6%9C%BA

狀態存儲關於過去的信息,就是說:它反映從系統開始到現在時刻的輸入變化。轉移指示狀態變更,並且用必須滿足來確使轉移發生的條件來描述它。動作是在給定時刻要進行的活動的描述。有多種類型的動作:

進入動作(entry action):在進入狀態時進行
退出動作:在退出狀態時進行
輸入動作:依賴於當前狀態和輸入條件進行
轉移動作:在進行特定轉移時進行

FSM(有限狀態機)可以使用上面圖 1 那樣的狀態圖(或狀態轉移圖)來表示。此外可以使用多種類型的狀態轉移表。下面展示最常見的表示:當前狀態(B)和條件(Y)的組合指示出下一個狀態(C)。完整的動作信息可以只使用腳註來增加。包括完整動作信息的 FSM 定義可以使用狀態表

狀態轉移表
當前狀態 →
條件 ↓
狀態 A狀態 B狀態 C
條件 X
條件 Y 狀態 C
條件 Z

除了建模這裏介紹的反應系統之外,有限狀態自動機在很多不同領域中是重要的,包括電子工程語言學計算機科學哲學生物學數學邏輯學。有限狀態機是在自動機理論計算理論中研究的一類自動機。在計算機科學中,有限狀態機被廣泛用於建模應用行爲、硬件電路系統設計、軟件工程,編譯器、網絡協議、和計算與語言的研究。

軟件應用

下列概念經常用來建造有有限狀態機的軟件應用:

  • 事件驅動 FSM
  • 虛擬 FSM (VFSM)
  • 基於自動機編程

 2. State machines vs threads

 

Alan Cox once said "A Computer is a state machine. Threads are for people who can't program state machines".

http://www.uml-diagrams.org/examples/java-6-thread-state-machine-diagram-example.html

Java 6 Thread States and Life Cycle

UML Protocol State Machine Diagram Example

This is an example of UML protocol state machine diagram showing thread states and thread life cycle for the Thread class in Java 6.

Thread is a lightweight process, the smallest unit of scheduled execution. Instance of the Thread class in Java 6 could be in one of the following states:

  • new,
  • runnable,
  • timed waiting,
  • waiting,
  • blocked,
  • terminated.

These states are Java Virtual Machine (JVM) states reported by JVM to Java programs. At any given point in time thread could be in only one state.

Protocol state machine example - Thread States and Life Cycle in Java 6.

Protocol state machine example - Thread states and life cycle in Java 6

New is the thread state for a thread which was created but has not yet started.

At the lower operating system (OS) level, JVM’s runnable state could be considered as a composite state with two substates. When a thread transitions to the runnable JVM state, the thread first goes into the ready substate. Thread scheduling decides when the thread could actually start, proceed or be suspended. Thread.yield() is explicit recommendation to thread scheduler to pause the currently executing thread to allow some other thread to execute.

A thread in the runnable state is executing from the JVM point of view but in fact it may be waiting for some resources from the operating system.

Timed waiting is a thread state for a thread waiting with a specified waiting time. A thread is in the timed waiting state due to calling one of the following methods with a specified positive waiting time:

  • Thread.sleep(sleeptime)
  • Object.wait(timeout)
  • Thread.join(timeout)
  • LockSupport.parkNanos(timeout)
  • LockSupport.parkUntil(timeout)

A thread is in the waiting state due to the calling one of the following methods without timeout:

  • Object.wait()
  • Thread.join()
  • LockSupport.park()

Note, that thread in the waiting state is waiting for another thread to perform a particular action. For example, a thread that has called Object.wait() on an object is waiting for another thread to call Object.notify() or Object.notifyAll() on that object. A thread that has called Thread.join() is waiting for a specified thread to terminate. It means that waiting state could be made a composite state with states corresponding to these specific conditions.

Thread is in the blocked state while waiting for the monitor lock to enter a synchronized block or method or to reenter a synchronized block or method after calling Object.wait().

A synchronized statement or method acquires a mutual-exclusion lock on behalf of the executing thread, executes a block or method, and then releases the lock. While the executing thread owns the lock, no other thread may acquire the lock and is blocked waiting for the lock.

After thread has completed execution of run() method, it is moved into terminated state.

 

3. 框架

http://stackoverflow.com/questions/10875317/recommended-fsm-finite-state-machine-library-for-java

From original question:

Updates:

Also:

發佈了285 篇原創文章 · 獲贊 2 · 訪問量 15萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章