Object Oriented Design Tips – State Machine

For some problems (like vending machine), it can be tackled by object oriented design with State Machine.

A state machine, also named as finite-state machine (FSM) or finite-state automation (FSA), finite automation, is a mathematical model of computation. It simulates the system with several states and it could change from one state to another (transition) according to certain input.

There are several representations for state machines, State/Event table, UML state machines, SDL state machines and other state diagrams.

Finite state machines can be subdivided into transducers, acceptors, classifiers and sequencers.

There are some concepts always used to build software application with finite state machines:

Automata-based programming (a programming paradigm in which the program or part of it is thought of as a model of a finite state machine (FSM) or any other (often more complicated) formal automaton (see automata theory).)

Event-driven finite-state machine (a finite-state machine (FSM) is event driven if the transition from one state to another is triggered by an event or a message.)

Virtual finite-state machine (a finite state machine (FSM) defined in a virtual environment. The VFSM concept provides a software specification method to describe the behavior of a control system using assigned names of input control properties and of output actions.)

State design pattern (a behavioral software design pattern that implements a state machine in an object-oriented way)

As the design for Vending Machine, the states could be idle, typing (waiting for typing), pay (processing payment) and pop out (item).

The initial status is idle and goes to typing if start typing, it allows clear, cancel and submit when typing item row and column number, then it takes care of payment after submit the item row and column number, finally it will pop out the item if paid successfully, otherwise pay again.   StateMachine

For classes in vending machine, there should be some buttons to indicate row (A, B, C) and column(1, 2, 3) number, some buttons to clear, cancel or submit row and column numbers, some slots to accept payment (cash, coin, credit/debit card), some slots to hold items (drink, snack, etc.).

class VendingMachine

public class VendingMachine {
  private Char row;
  private int col;
  private ArrayList<ButtonRow> btnRowList;
  private ArrayList<ButtonCol> btnColList;
  private ArrayList<ButtonType> btnTypeList;
  private ArrayList<SlotPayment> slotPayList;
  private ArrayList<SlotItem> slotItemList;

  public void acceptPayment();
  public void popOutItem();
  public void printReceipt();
}
 class ButtonRow
public class ButtonRow {
  private Char row;

  public void pressed();
  public void display();
}
 class ButtonCol
public class ButtonCol {
  private int col;

  public void pressed();
  public void display();
}
class ButtonType
public class ButtonType {
  private int type; // 0 - clear, 1 - cancel, 2 - submit

  public void clear();
  public void cancel();
  public void submit();
}
 class SlotPayment
public class SlotPayment {
  private int type; // 0 - cash, 1 -coin, 2 - credit/debit card

  public void payWithCash();
  public void payWithCoin();
  public void payWithCard();
}
 class SlotItem
public class SlotItem {
  private Char row;
  private int col;
  private Item item; // drink, snack

  public void popOut();
  public void addNew();
}
Buy me a coffee with PayPal and you will have more professional and interesting technical blogs. Feel free to visit my web app, WhizWallet, to apply for credit, store or gift cards.

Reference:

The Fundamental Concepts of Object-Oriented Programming

Advertisements

About liyao13

Yao Li is a web and iOS developer, blogger and he has a passion for technology and business. In his blogs, he shares code snippets, tutorials, resources and notes to help people develop their skills. Donate $5 to him for a coffee with PayPal at About Me page and read more professional and interesting technical blog articles. Follow him @Yaoli0615 at Twitter to get latest tech updates.
This entry was posted in CS Research&Application, Uncategorized and tagged , , . Bookmark the permalink.