Flutter Design Patterns: 6 — State
An overview of the State design pattern and its implementation in Dart and Flutter
Previously in the series, I have analysed one of the most practical design patterns you can use in day-to-day coding — Strategy. In this article, I will analyse and implement a pattern, which has a similar structure to Strategy but is used for a different purpose — the State design pattern.
Table of Contents
- What is the State design pattern?
- Other articles in this series
- Your contribution
What is the State design pattern?
The State is a behavioural design pattern, which intention in the GoF book is described like this:
Allow an object to alter its behaviour when its internal state changes. The object will appear to change its class.
To understand a general idea behind the State design pattern, you should be familiar with the concept of a Finite-state machine. Here you can see an example of a Finite-state machine model of a JIRA task:
At any given moment, there is a finite number of states which a task can be in. Each one of the states is unique and acts differently. At any time, the task could be switched from one state to another. The only limitation — there is a finite set of switching rules (transitions) which define the states that could be switched to from the current state. That is, a task from a state of open could not be switched to reopened, closed could not be switched to in progress or resolved, etc.
The similar approach could be applied in OOP to objects. To simply implement the Finite-state machine in code is by using several conditional operators in a single class and selecting the appropriate behaviour depending on the current state of the object. However, by introducing new states, this kind of code becomes very difficult to maintain, states interlace with each other, the class is committed to a particular state-specific behaviour. I have not even mentioned how difficult it becomes to test this kind of code and the implemented business logic inside.
To resolve these issues, a State design pattern is a great option since for each state a separate State class is created which encapsulates the logic of the state and changes the behaviour of a program or its context. Also, it makes adding new states an easy task, state transitions becomes explicit.
Let’s move to the analysis to understand how the State pattern works and how it could be implemented.
The general structure of the State design pattern looks like this:
- Context — maintains an instance of a ConreteState subclass that defines the current state. The Context class does not know any details about ConcreteStates and communicates with the state object via the State interface. Also, Context provides a setter method to change the current state from ConcreteStates classes;
- State — defines an interface which encapsulates the state-specific behaviour and methods. These methods should make sense for all ConreteStates, there should be no methods defined which will never be called from a specific implementation of the State interface;
- ConcreteStates — each class implements a behaviour associated with a state of the Context. State objects may reference the Context to obtain any required information from the context or to perform the state transition by replacing the state object linked to the Context class (via the exposed setter method);
- Client — uses the Context object to reference the current state and initiate its transition, also it may set the initial state for the Context class if needed.
Strategy vs State
I expect you have noticed, that the structure of the State design pattern looks really similar to the Strategy. The main difference between these patterns — in the State pattern, the particular states may be aware of each other, while specific strategies almost never know any details about other implementations of the strategy. Basically, this key difference changes the meaning of the pattern and its usage in code.
The State design pattern should be used when you have an object that behaves differently depending on its current state, the number of states is enormous, and the state-specific code changes frequently. By encapsulating each state and its implementation details in a separate class, you can add new states more easily, also you can change the existing states independently of each other. This idea promotes several SOLID principles which are already discussed in the series: the Single Responsibility Principle (each state is encapsulated in its class) and the Open/Closed Principle (new states could be introduced without changing existing state classes). Pretty much any logic based on the idea of the Finite-state machine could be implemented using the State design pattern. Some possible real-world examples: managing order’s state in the e-commerce application, showing the appropriate colour in the traffic lights based on the current state, different article state in Medium (draft, submitted, published…), etc. In the implementation section, I will introduce one more relevant use case of the State design pattern — resource loading from API.
The following implementation (or at least the idea behind it) could be applied to pretty much every Flutter application which loads resources from any kind of external source, e.g., API. For instance, when you load resources asynchronously using HTTP and calling the REST API, usually it takes some time to finish the request. How to handle this and not “freeze” the application while the request is finished? What if some kind of error occurs during this request? A simple approach is to use animations, loading/error screens, etc. It could become cumbersome when you need to implement the same logic for different kind of resources. For this, the State design pattern could help. First of all, you clarify the states which are common for all of your resources:
- Empty — there are no results;
- Loading — the request to load the resources is in progress;
- Loaded — resources are loaded from the external source;
- Error — an error occurred while loading the resources.
For all of these states, a common state interface and context is defined which could be used in the application.
Let’s dive into the implementation details of the State design pattern and its example in Flutter!
The class diagram below shows the implementation of the State design pattern:
IState defines a common interface for all the specific states:
- nextState() — changes the current state in StateContext object to the next state;
- render() — renders the UI of a specific state.
NoResultsState, ErrorState, LoadingState and LoadedState are concrete implementations of the IState interface. Each of the states defines its representational UI component via render() method, also uses a specific state (or states, if the next state is chosen from several possible options based on the context) of type IState in nextState(), which will be changed by calling the nextState() method. In addition to this, LoadedState contains a list of names, which is injected using the state’s constructor, and LoadingState uses the FakeApi to retrieve a list of randomly generated names.
StateContext saves the current state of type IState in private currentState property, defines several methods:
- setState() — changes the current state;
- nextState() — triggers the nextState() method on the current state;
- dispose() — safely closes the stateStream stream.
The current state is exposed to the UI by using the outState stream.
StateExample widget contains the StateContext object to track and trigger state changes, also uses the NoResultsState as the initial state for the example.
An interface which defines methods to be implemented by all specific state classes. Dart language does not support the interface as a class type, so we define an interface by creating an abstract class and providing a method header (name, return type, parameters) without the default implementation.
A class which holds the current state in currentState property and exposes it to the UI via outState stream. The state context also defines a nextState() method which is used by the UI to trigger the state’s change. The current state itself is changed/set via the setState() method by providing the next state of type IState as a parameter to it.
Specific implementations of the IState interface
ErrorState implements the specific state which is used when an unhandled error occurs in API and the error widget should be shown.
LoadedState implements the specific state which is used when resources are loaded from the API without an error and the result widget should be provided to the screen.
NoResultsState implements the specific state which is used when a list of resources is loaded from the API without an error, but the list is empty. Also, this state is used initially in the StateExample widget.
LoadingState implements the specific state which is used on resources loading from the FakeApi. Also, based on the loaded result, the next state is set in nextState() method.
A fake API which is used to randomly generate a list of person names. The method getNames() could return a list of names or throw an Exception (error) at random. Similarly, the getRandomNames() method randomly returns a list of names or an empty list. This behaviour is implemented because of demonstration purposes to show all the possible different states in the UI.
First of all, a markdown file is prepared and provided as a pattern’s description:
StateExample implements the example widget of the State design pattern. It contains the StateContext, subscribes to the current state’s stream outState and provides an appropriate UI widget by executing the state’s render() method. The current state could be changed by triggering the changeState() method (pressing the Load names button in UI).
StateExample widget is only aware of the initial state class — NoResultsState but does not know any details about other possible states, since their handling is defined in StateContext class. This allows to separate business logic from the representational code, add new states of type IState to the application without applying any changes to the UI components.
The final result of the State design pattern’s implementation looks like this:
As you can see in the example, the current state is changed by using a single Load names button, states by themselves are aware of other states and set the next state in the StateContext.
All of the code changes for the State design pattern and its example implementation could be found here.
Other articles in this series
- 0 — Introduction
- 1 — Singleton
- 2 — Adapter
- 3 — Template Method
- 4 — Composite
- 5 — Strategy
- 7 — Facade
- 8 — Interpreter
- 9 — Iterator
- 10 — Factory Method
- 11 — Abstract Factory
- 12 — Command
- 13 — Memento
- 14 — Prototype
- 15 — Proxy
- 16 — Decorator
- 17 — Bridge
- 18 — Builder
- 19 — Flyweight
- 20 — Chain of Responsibility
- 21 — Visitor
👏 Press the clap button below to show your support and motivate me to write better!
💬 Leave a response to this article by providing your insights, comments or wishes for the series.
📢 Share this article with your friends, colleagues in social media.
➕ Follow me on Medium.
⭐ Star the Github repository.