Skip to content
Beh01der edited this page Apr 16, 2013 · 6 revisions

EasyFlow is a simple and lightweight Finite State Machine for Java

With EasyFlow you can:

  • implement complex logic but keep your code simple and clean
  • handle asynchronous calls with ease and elegance
  • avoid concurrency by using event-driven programming approach
  • avoid StackOverflow error by avoiding recursion
  • simplify design, programming and testing of complex java applications

All this in less then 30kB and no run-time overhead! Download EasyFlow 1.1

Here is a simple example illustrating how a state machine can be definded and implemented with EasyFlow

This is a State diargam fragment describing a simple ATM workflow

State diagram fragment

With EasyFlow we can define the above state machine like this

EasyFlow<FlowContext> flow = FlowBuilder

    .from(SHOWING_WELCOME).transit(
        onCardPresent.to(WAITING_FOR_PIN).transit(
            onPinProvided.to(...).transit(
                ...
            ),
            onCancel.to(RETURNING_CARD).transit(
                onCardExtracted.to(SHOWING_WELCOME)
            )
        )
    )

then all what's left to do is to implement our state handlers like so

SHOWING_WELCOME.whenEnter(new StateHandler<FlowContext>() {
    @Override
    public void call(State<FlowContext> state, final FlowContext context) throws Exception {
        ...
        btnOption1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onCardPresent.trigger(context);
            }
        });
        ...
    }
});
...

and start the flow

flow.start(new FlowContext());

See the complete example

Clone this wiki locally