Builder Pattern Ensures Illegal States in Finite State Machines

A couple of years ago, I wrote that "The Builder pattern is a finite state machine!". A state machine consists of states and transitions between them. As a developer, I want to make illegal states unrepresentable, i.e., users of my API can’t create non-existent transitions. My hypothesis is that only a static typing system allows this at compile-time. Dynamic typing systems rely on runtime validation. In this blog post, I will show that it holds true, with a caveat. If your model has many combinations, you also need generics and other niceties to avoid too much boilerplate code. My exploration will use #Python, #Java, #Kotlin, #Rust, and #Gleam. With that background in mind, let’s move on to the model itself. #builderpattern #finiteStateMachine #illegalState #programming #coding (link in the comments)

  • No alternative text description for this image

To view or add a comment, sign in

Explore content categories