The Interpreter Pattern
Of all the design patterns in all the world, this one is my favorite. The interpreter pattern involves encoding the actions a user can take through an app into a clean structure and interpreting that.
This is useful for many reasons: video games use this extensively – if you want your players to be able to “mod” the game and change things about it, they have to provide a recipe that your game can read, and voila – they can extend your game.
Want to provide replays? You can create a video player in your game that reads a stream of instructions and displays those actions on the screen, thereby compressing the replay file to just a set of binary encoded instructions.
Say you want to add redo and undo to your application. If you do it normally, you can’t do undo anything, because you’ve deleted the previous state of the application, and can’t undo the mutation. With actions, you can define a redo and undo for each action, thereby adding that to your app.
Say you want to test your application by giving it a valid set of instructions and making sure it does the right thing. If you don’t have a set of instructions to pass in, and the app can’t execute them, you’re out of luck. But if you can, you can randomly generate a set of instructions and see if you get the result you want, or scaffold a test that does a specific set of actions and verifies its output. You can even take this to an extreme and fuzz your entire application this way.
There’s many other things you can do by turning an application into one that interprets a set of instructions, but these were the ones that came to my mind.