dev-resources.site
for different kinds of informations.
Finite-state machine example in JavaScript
Published at
7/2/2024
Categories
javascript
patterns
example
Author
artem
Author
5 person written this
artem
open
What is Finite-state machine?
Context
FSM refers to classes of automata
The finite state machine (FSM) is a software design pattern where a given model transitions to other behavioral states through external input.
Example using if else
Let's say we have a simple task where we check, for example, a traffic light and perform actions depending on the current state.
function trafficLightAction(color) {
if (color === 'green') {
console.log('Go');
} else if (color === 'yellow') {
console.log('Slow down');
} else if (color === 'red') {
console.log('Stop');
} else {
console.log('Invalid color');
}
}
// Function call examples
trafficLightAction('green'); // Return: Go
trafficLightAction('yellow'); // Return: Slow down
trafficLightAction('red'); // Return: Stop
trafficLightAction('blue'); // Return: Invalid color
Example with using Finite-state machine (FSM)
Now let's implement the same functionality using a state machine. A state machine will be an object where each key (state) is associated with a specific action.
const trafficLightFSM = {
green: () => console.log('Go'),
yellow: () => console.log('Slow down'),
red: () => console.log('Stop'),
invalid: () => console.log('Invalid color'),
};
function trafficLightActionFSM(color) {
const action = trafficLightFSM[color] || trafficLightFSM['invalid'];
action();
}
// Function call examples
trafficLightActionFSM('green'); // Return: Go
trafficLightActionFSM('yellow'); // Return: Slow down
trafficLightActionFSM('red'); // Return: Stop
trafficLightActionFSM('blue'); // Return: Invalid color
Now, our traffic light will works well.
Disclaimer:
Several levels of additional tests would not hurt here, and perhaps another programming language ;)
patterns Article's
30 articles in total
Streamlining Data Flow in Angular: The Power of the Adapter Pattern ๐
read article
CQRS โ Command Query Responsibility Segregation โ A Java, Spring, SpringBoot, and Axon Example
read article
Mastering the Container-Presenter Pattern in Angular: A Deep Dive
read article
Repository Design Pattern, Promoting Packages via ADS, and New Arabic Article โจ
read article
Flexibilidad y Escalabilidad: Usando Strategy y Factory Patterns
read article
Understanding Domain Events in TypeScript: Making Events Work for You
read article
Padrรตes de Projeto em React [HOCs, Render Props, Hooks]
read article
Mobile Development Platforms and software architecture pattern in mobile development
read article
Finite-state machine example in JavaScript
currently reading
OOP Simplified: Quick Factory Methods with Encapsulation, Abstraction, and Polymorphism in TypeScript
read article
Finite-state machine example in JavaScript
read article
How to avoid N + 1 and keep your Ruby on Rails controller clean
read article
Types Of Software Architecture
read article
Testando das trincheiras: Usando um "clock" fixo
read article
ยฟPOR QUร no estรกs usando estos providers de Angular?
read article
Common and Useful Deployment Patterns
read article
Reusing state management: HOC vs Hook
read article
Understanding JavaScript Creational Patterns for Object Creation
read article
Understanding Design First: Principles, Patterns, and Best Practices Explained
read article
The Architecture Might Not Be the Problem
read article
Padrรฃo JumpTable com Javascript
read article
Explorando os Fundamentos dos Padrรตes de Projeto: Conceitos Bรกsicos
read article
Six Factors That Raise The Risk Of Bugs In A Codebase
read article
Microservices: Avoiding the Pitfalls, Embracing the Potential - A Guide to Anti-Patterns
read article
Android Presentation Patterns: MVI
read article
Entendendo o Pub/Sub com Javascript
read article
The Consumer Conundrum: Navigating Change in Microservices Without Gridlock
read article
๐ช๐ต๐ฎ๐ ๐ฎ๐ฟ๐ฒ ๐๐ต๐ฒ ๐ฅ๐ฒ๐ด๐๐
๐ฃ๐ฎ๐๐๐ฒ๐ฟ๐ป๐?
read article
Using a Trait in Builder CLIs
read article
CLI Contexts
read article
Featured ones: