Domain Model Example

Before we delve into domain model examples, let's take a step back and talk about what a domain model is. A domain model is a representation of the real-world space you, and your colleagues, are working in. Every software company, and even non-technical companies, have an implicit domain model, even if they haven't explicitly created one.

One of the ideas behind a domain model is to create a shared understanding between everyone working in a given area. Speaking from experience, it becomes easier to work through problems and understand the area you're working in with a shared vocabulary. This shared vocabulary is often referred to as a ubiquitous language.

We're going to move on to a domain model example now, but you can check out domain models if you want a more in-depth look at them.

Creating A Domain Model Example

Without further ado, let's take a look at an example domain model. I'm going to use a music streaming service as a fictional example.

---
title: Music Streaming Domain Model
---
classDiagram 
    Listener "0..*" -- "0..*" Artist: follows
    Listener "1" *-- "0..*" Playlist: creates
    
    Artist "1..*" *-- "0..*" Single: creates
    Artist "1" *-- "0..*" Album: creates

    Album "1" o-- "3..*" Single: contains

    Playlist "0..*" *-- "1..*" Single: contains
        

This diagram was created using diagrams as code.

In reality, this is a subset of what a real music streaming company would have in their domain model, as this is just an example domain model. It serves the purposes of a domain model example though.

To explain what's going on a little, each box represents an entity within the domain. An entity represents an important concept within a domain. We have five entities: listener, artist, single, album and playlist. Entities on their own don't serve much purpose, their power comes when they are related to other entities.

That's exactly what we do with the lines between the entities. Each relationship also has a descriptive label, informing the reader a little about how the relationship works. For example, a listener follows artists, and creates playlists.

Finally, we have what's called multiplicity. That's the little numbers you see where each line connects to an entity. In short, it details how many of one entity relate to another. In the case of listener and playlist, a listener creates 0 to many playlists (as they may choose not to create any, or many) and a playlist belongs to one listener (whoever created the playlist).

There are different types of relationship too. If you look at the line between Listener and Artist, there's a simple line, which is an association. In short, this means two entities are related, but they can exist independently of one another, and there is no owner in this relationship. There are two others to cover: composition and aggregation.

You can see aggregation between Album and Single, indicated by an empty diamond. Aggregation between two entities shows a parent & child relationship, with the parent being on the diamond side. These two entities can still exist independently of one another though. As for composition, it's shown by a solid diamond, and is very similar to aggregation. However, composition dictates two entities cannot exist independently, so if one is deleted, so is the other.

In the real world, domain models can get quite a bit more complicated than in this example domain model. I've briefly covered the main parts of this example domain model, but there's a lot more to understand when it comes to domain models.

Create Domain Models Yourself

Creating Diagrams With Modern Diagramming Techniques Book

This post took a look at a domain model example. I created the domain model using MermaidJS.

You can quickly and easily create a variety of diagrams, using simple Markdown-like syntax, and render them in seconds.

I cover how to create domain models, along with a number of other use cases and diagrams, in my book: Creating Software Using Modern Diagramming Techniques, published via The Pragmatic Programmers.