DEV Community


Posted on • Originally published at on

Creating, Using & Building a DSL

The main idea behind this article about building a DSL is to play and experiment with some design patterns on a surface level.

We’ll have a character with a position, and we’ll move it in four directions thanks to the commands grouped in a DSL. These commands can be undone and move the character back to a previous position.

Building a DSL: Character

The first thing that we’ll create is our Character domain class, where we’ll put all the logic related to the character’s movement. It will be able to move up, down and sideways.

DSL character


_ Memento is a pattern design with the goal of stacking an object’s state at a given time._

We’ll create our Memento class to stack each of our Character’s previous states.

DSL memento


_ Originator creates a Memento object containing a photography of its internal state._

We’ll create an Originator interface and implement it on our Character class so it knows how to create memories and restore itself from one of them.

DSL originator

At this point, I’ve realized that Memento doesn’t seem fit for immutable code. It would be normal to match the values of the current instance with the Memento’s one.


_ Command is the base class of all the commands. Each command shall implement said interface and its ‘do’ method, which will execute different actions through polymorphism. _

  • We save a memory of the object we’re about to modify.
  • The ‘do’ method will be abstract, and each command will have a different implementation through polymorphism.
  • The ‘undo’ method will be in charge of undoing the action done by the command through the memento. We’ll implement it a single time in the base class.

DSL command

Concrete Command

_ Concrete Command is each of the commands that our system has. They must inherit the Command base class. Each one will have its own implementation._

It’s important that the commands are devoid of any logic aside from calling the class method that contains all logic. In our example, we’ll create a command to move our Character on any direction.

DSL concrete command


_ CommandManager is the component that will execute the commands and stack their history with the goal of doing and undoing the commands. _

Each of this class’s methods returns a Tupla with:

  • A new CommandManager instance with an updated history of commands.
  • A new updated Character instance.

DSL commandmanager

Creating the DSL

_ Extension methods is a pattern that has the capability of aggregating behaviour to already existing classes without modifying the original classes._

We’ll add methods to the Tupla using exension methods, to group all commands and build our DSL.

Using the DSL

The console output will be:

Character(x=-1, y=-1)

Now all that’s left is adding the commands we’ve created to the DSL to extend it.

The post Creating, Using & Building a DSL appeared first on Apiumhub.

Top comments (0)