Creating, Using & Building a DSL

Share This Post

Share on facebook
Share on linkedin
Share on twitter
Share on email

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.


Conversion Post EN

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.

creando DSL 2

Using the DSL

usando DSL 2

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.

Leave a Reply

Your email address will not be published. Required fields are marked *

Subscribe To Our Newsletter

Get updates from our latest tech findings

About Apiumhub

Apiumhub brings together a community of software developers & architects to help you transform your idea into a powerful and scalable product. Our Tech Hub specialises in Software ArchitectureWeb Development & Mobile App Development. Here we share with you industry tips & best practices, based on our experience.

Popular posts​
Free PDF with Software Architecture Interviews

Are you Data Driven?

Let's build your success together.

Talk to us

Have a challenging project?

We Can Work On It Together