An Introduction to Domain-Driven Design In Java

July 08, 2022
domain driven design java



Domain-driven design in Java

When we discuss the process of software development in Java, the most common topics that come up are the frameworks, the Java libraries that can be used or the APIs. It is quite common that one of the most important aspects of software development is often overshadowed, that is the approach or concept applied to the development process. One such concept is Domain Driven Design in Java.

new java job roles

The concept of Domain Driven Design was initially introduced by a well-known programmer, Eric Evans in 2004. He introduced this approach to programming in his book Domain-Driven Design: Tackling Complexity in Heart of Software. He explained it as a top-down approach to building software by reflecting a real-world building process.

We will further be discussing the Domain Driven Design in Java, how it works, the concept behind it and how it can improve your software development process.

What is a Domain?

To further understand this approach, we need to understand the context of the Domain in it. “Domain” in DDD Java is officially referred to as the main area of knowledge and activity based on which the application will be based. If simply put, the Domain of an application would be what is commonly referred to as the business logic of an application. The business logic consists of various guidelines and rules that explain the business object and how the data should be processed and modelled in the application to get the desired output.

Why do we need Domain driven design in Java?

According to Eric Evans, when you are developing a software, the focus should not be primarily on technology, rather it should be on the business logic. For instance, Let’s say you have designed a Java application using the best technology stack and infrastructures available. When the application is finally delivered to the client, it is ultimately the end-user who can decide whether the final product is a success or not. It must tick all the boxes when it comes to solving its business needs and requirements. It will not matter how attractive or user-friendly the application would be, how fast it is or how well it is developed if it cannot efficiently get the tasks done.

Since the early 2000s when the new technologies started to get readily available, it was often observed that software developers tend to use the new technologies regardless of whether they are required to get the task done or not. Later on, these technologies became the prime focus of software developers instead of focusing on the business logic of the software. To cater to that, the Domain Driven design was introduced in Java. It was soon identified as an extremely efficient design in the Java community and the majority of frameworks under Spring are also built upon Domain-Driven design in Java.

Identifying the subdomains

A domain is big enough to be covered as one operation. To narrow it down and make it more understandable, it is divided into further subdomains that are categorized into three types,

1. Core domains

As the name suggests, it is the core or the base of the application. The core domain acts as a foundation for the application that is the fundamental requirement of the application. For instance, a data management system would be a core domain of a school LMS.

2. Supporting subdomains

These provide the supporting features to execute the tasks for the core domain. For instance, for a school LMS would require features to manage classes, subjects, grades etc.

3. Generic subdomains

The Generic subdomains are the ones that often apply to various applications. An example could be scheduling sessions, it would be the same for scheduling interviews, doctors’ appointments, auditions etc

Identifying the correct subdomains is a very crucial task as if a subdomain would be identified incorrectly, it can cause a chain of problems down the road. It is also to be noted that the same subdomain can sometimes also be placed into different subdomain categories depending on the requirements and the core domain of the project. Even though they require different amounts of resources and may also have different requirements, all of these subdomains play an equally important part in the overall solution regardless of the category in which they lie.

Type of Domain Driven Design

The domain-driven design can be categorized into two types to be implemented in Java applications,

1. Strategic Domain-driven design

It primarily works on creating the context of the domain and the relationship between subdomains. The strategic domain-driven design is used to develop proper relations between all the identified subdomains for connecting the whole system together. The strategic domain-driven design was originally aimed for bigger projects but later it was realized that developers can also benefit from it in smaller projects.

Another advantage of strategic design is the introduction of boundaries. Scope creep can be a common problem during the development of Java applications. It is also sometimes very difficult to prevent technical scope creep so boundaries prove to be a great help in dividing the project into smaller parts (subdomains) that help in focusing on the right part at the right time.

2. Tactical domain-driven design

The Java developers mainly deal with this aspect of DDD Java. the tactical domain-driven design is more focused on the development tasks. It makes use of two significant things, value objects and entities.

a. Value object

One of the most important concepts in tactical DDD in Java is the value object. A value object is used to treat the data in the objects based on its value. Two value objects with the same value can be considered the same value object and that is why they would also be interchangeable. For the same reason, value objects should always be made immutable. Instead of changing the state of the value object, it must be replaced with a new instance.

You can use value objects to wrap the data with business meaning. For example, instead of using a BigDecimal for monetary values, use a Money value object that wraps a BigDecimal. If you are using multiple currencies, you can create a Currency value object as well and make the Money object wrap a BigDecimal Currency pair.

Using the value objects brings context to the values. The type itself will instantly let you know what data you are dealing with. It will also help you in making sure that the value object always contains a valid value. For instance, you can validate an e-mail address in the constructor of your EmailAddress value object.

 

See this example below where A Money value object in Java is demonstrated:

public class Money implements Serializable, Comparable<Money> {
     private final BigDecimal amount;
     private final Currency currency; 
     public Money(BigDecimal amount, Currency currency) {
         this.currency = Objects.requireNonNull(currency);
         this.amount = Objects.requireNonNull(amount));
     }
     public Money add(Money other) {
         assertSameCurrency(other);
         return new Money(amount.add(other.amount), currency);
     }
     public Money subtract(Money other) {
         assertSameCurrency(other);
         return new Money(amount.subtract(other.amount), currency);
     }

b. Entities

Another important concept in tactical DDD in Java is the entity. An entity is a mutable object that contains the data but its identity is of importance. To be able to identify an entity, every entity is assigned a unique ID at the time of creation and it remains unchanged throughout the lifespan of the entity. Two entities with the same ID and type would be considered the same entity regardless of all other properties. Each property in an entity also has a setter to alter the values.

Entity VS Value Object

It is often not that simple to know whether to model some data as a value object or as an entity. You can do that by applying the scenario to a real-world concept. For example, if you are building a ticket booking system for a one-time event, the date is something that is just printed on every ticket and it is not concerning what object instance is used as long as the date is correct. In such a case, the date would be a value object. In another scenario, if you are building a similar software for a railway system, you need to know exactly what will be the date and time of departure of every train and each train will be unique. In this case, the date will be an entity.

Value objects are relatively easier to work with as they are immutable and small that is why it is preferable to aim for a design with few entities and more valuable objects.

Conclusion

This was just a brief introduction to Domain-driven design in Java. We have just scratched the surface as there is much more to discover about this software design approach. It is evident that the implementation of domain-driven design in Java would not only save time and resources but also makes various parts of your code robust and stable.

Also Read: An Introduction To Inline Classes In Java

new Java jobs



author

admin


Candidate signup

Create a free profile and find your next great opportunity.

JOIN NOW

Employer signup

Sign up and find a perfect match for your team.

HIRE NOW

How it works

Xperti vets skilled professionals with its unique talent-matching process.

LET’S EXPLORE

Join our community

Connect and engage with technology enthusiasts.

CONNECT WITH US