Introduction to Domain Modeling

Domain Modeling

Note: The following text comes from my book, Learn Scala 3 The Fast Way! (Book 1: The Adventure Begins)

This lesson provides an introduction to domain modeling (DM) in Scala 3.

In both FP and OOP, programmers model the world around us. We see things like pizza, people, cars, etc., and we model these things, typically with a construct like a class. Language creators also found that interfaces are a good thing, so most languages have some sort of interface construct.

Because Scala supports both FP and OOP, it takes domain modeling a step further and provides ways to model our world in both domains. The approaches are a little different, so I’ll describe each approach separately.

Tools for OOP domain modeling

Because most programmers are familiar with OOP, I’ll start with it first.

When programming in an OOP style, you’ll primarily use these Scala constructs:

  • Traits
  • Enums
  • Classes
  • Objects

In the OOP style you’ll use these constructs in these ways:

  • Traits: In an OOP style you’ll primarily use traits as interfaces. If you’ve used Java, you can use Scala traits just like interfaces in Java 8 and newer, with both abstract and concrete members. Classes will later be used to implement these traits.

  • Enums: You’ll primarily use enums to create simple sets of constants, like the positions of a display (top, bottom, left, and right).

  • Classes: In OOP you’ll primarily use plain classes, not case classes (which are discussed below). You’ll also define their constructor parameters as var fields so they can be mutated. They’ll also contain methods based on those mutable fields. You’ll override the default accessor and mutators methods (getters and setters) as needed.

  • Objects: You’ll primarily use the object construct as a way to create the equivalent of static methods in Java, like a StringUtils object that contains “static” methods that operate on strings.

All of these tools (constructs) are described in the lessons that follow.

Tools for FP domain modeling

When programming in an FP style, you’ll primarily use these constructs:

  • Traits
  • Enums
  • Case classes
  • Objects

In the FP style you’ll use these constructs as follows:

  • Traits: Traits are used to create small, logically-grouped units of behavior. The behaviors are typically written as def methods, and they are also implemented as pure functions. These traits will later be combined into concrete objects.

  • Enums: Enums are used to create algebraic data types (ADTs). Don’t let this term scare you, ADTs are actually a simple concept with a scary name.

  • Case classes: You’ll use case classes to create objects with immutable fields (known as immutable records in some languages, such as the record type in Java 14). Case classes are like plain classes, but have additional, standard methods baked into them to make FP easier.

  • Objects: In FP you’ll typically use objects as a way to make one or more traits “real,” in a process that’s technically known as reification.