Creating, Using & Building a DSL
Tags: agilewebandappde, dsl
Published: 2020-03-04 13:45:58

---
title: Creating, Using & Building a DSL
published: true
date: 2019-10-22 15:02:21 UTC
tags: Agile web and app de,DSL
canonical_url: https://apiumhub.com/tech-blog-barcelona/creating-using-building-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](https://apiumhub.com/wp-content/uploads/2019/10/DSL-character-3.png)

### Memento

_ **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](https://apiumhub.com/wp-content/uploads/2019/10/DSL-memento-2.png)

### Originator

_ **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](https://apiumhub.com/wp-content/uploads/2019/10/DSL-originator-1024x429.jpg)

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

_ **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](https://apiumhub.com/wp-content/uploads/2019/10/DSL-command-2.png)

### 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](https://apiumhub.com/wp-content/uploads/2019/10/DSL-concrete-command-1024x407.jpg)

### CommandManager

_ **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](https://apiumhub.com/wp-content/uploads/2019/10/DSL-commandmanager-1024x355.jpg)

### 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.

![](https://apiumhub.com/wp-content/uploads/2019/10/creando-DSL-2.png)

### Using the DSL

![](https://apiumhub.com/wp-content/uploads/2019/10/usando-DSL-2.png)

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](https://apiumhub.com/tech-blog-barcelona/creating-using-building-dsl/) appeared first on [Apiumhub](https://apiumhub.com).