Enable javascript in your browser for better experience. Need to know to enable it? Go here.
Domain Modeling: What you need to know before coding

Domain Modeling: What you need to know before coding

Starting to write code without proper planning is like trying to build IKEA furniture with a blindfold on. If against all odds, you somehow manage to assemble something resembling a dresser, there’s a good chance you’ve forgotten a crucial piece and you’ll be throwing the whole thing out in a week and heading to Pottery Barn (like you should have in the first place). [1]

 

When getting to know a new game, which is completely unknown, the first thing we ask ourselves is what is it about? How do we play? What are the rules? Something very similar happens when we develop software, we need to know what we are going to build, what business concepts are involved with the system and how they are related to each other, in a tweet: we need to understand the domain.

 

Domain? What is that?

“A domain is a collection of related concepts, relationships, and workflows” [2]. These are some examples of domains:

Domain Model

Domain model is a structured visual representation of interconnected concepts or real-world objects that incorporates vocabulary, key concepts, behavior, and relationships of all of its entities. [3]

 

Building a Domain Model

When building an application, the first step is always like a whiteboard.

A domain model helps us to understand the real-world visually. In it we will see in an illustrated way:

  • Define entities or conceptual classes.

  • Associations between entities.

Let's see an example explained with a pokemon battle game.

 

Step 1: Entities

The first step when we are building a domain model is thinking about what entities exist; but What is an entity? It is everything that can be unique, identifiable, that exists and is part of a domain.

Thinking about pokemons, our domain can have entities like:

Step 2: Entities relationships

So, pokemon, type, move, trainer, team and battle are entities and they exist in our pokemon battle game domain. Each domain can be as complex or detailed as required, in this case we are going to explain in a very simple way how a pokemon battle game is and how entities are related to each other:

 

A pokemon is a creature with incredible powers that can belong to one or two types, has a maximum of four movements or abilities that can only be of a single type, each pokemon belongs to a team managed by a trainer, each team can contain a maximum of six pokemons and a pokemon battle is performed between two trainers with their respective teams. Both teams face off in one-on-one matches using their movements and the trainer who defeats the opposing team will win the battle.

 

We will be applying a useful technique — linguistic analysis: identifying the nouns and noun phrases in textual description of a domain, and considering them as entities or attributes.

Let’s analyze our domain:

  1. When we see a straight line connecting two entities together this shows us that these two entities are related.

  2. A verb in the middle of the line indicates what type of relationship exists between two entities. For example, the team entity “contains” pokemon.

  3. The numbers and the asterisk at the ends of the line indicate multiplicity, that is, it defines how many instances of an entity A can be associated with one instance of an entity B. For example one (1) battle “is composed by” two (*) trainers.


Now, we will add attributes and operations to our entities. For example for the pokemon entity we can have some attributes like name and weight and operations like attack and evolve. Let's see the following domain model with attributes and operations:

Now we have a more detailed model called Class Diagram (It is the main building block of object-oriented modeling. It is used for general conceptual modeling of the structure of the application, and for detailed modeling, translating the models into programming code). For attributes and operations we added - and + symbols, they mean private (-) and public (+) access for more información check this vídeo

 

We can conclude that from an entity or class we can create objects or instances. For example, if we want to create our pokemon team with, the entity or class pokemon with it’s type would be the templates and each pokemon and type would be our objects and we would have something like this:

Black diamonds ⧫ are composition relationships: this means a relationship is totally strong in such a way that conceptually one of these classes could not live if the other did not exist. For example: a leaf could not exist without a tree, therefore we can say that several "leaf" entities have a composition relationship with a tree.

If you are interested in learning more about classes and objects please check this out.

Again, the domain model can be as detailed as we need it, the important thing is that we understand that it helps us to better visualize the problem that we must solve.

References:

 

[1] Domain Modeling by Example. Associating Objects with Python. https://codeburst.io/rule-your-domain-model-d4beae6806c.

[2] Domain Modeling. http://www.cs.sjsu.edu/~pearce/modules/lectures/ooa/analysis/DomainModeling.htm

[3] A Brief Introduction to Domain Modeling. https://olegchursin.medium.com/a-brief-introduction-to-domain-modeling-862a30b38353

[4] Tutorial de diagramas de clases UML. https://www.youtube.com/watch?v=UI6lqHOVHic

[5] De POO a SOLID explicado con Pokémon por Jorge Mendez Ortega. https://medium.com/@jmz12/javascript-el-paradigma-de-poo-b04d19b6322c

Disclaimer: The statements and opinions expressed in this article are those of the author(s) and do not necessarily reflect the positions of Thoughtworks.

Keep up to date with our latest insights