Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

Make Your Domain Notify You Instead of Throwing an Exception

DZone's Guide to

Make Your Domain Notify You Instead of Throwing an Exception

A brief code snippet on how to validate data using the specification pattern in Java.

· Java Zone
Free Resource

Get the Edge with a Professional Java IDE. 30-day free trial.

This is a very short article, but very useful, because it talks about a very important topic.  A common approach for validating data inputted by users is throwing an exception, like the example below.

public void isNameValid(String name)
{
    if (name == null) 
      throw new    IllegalArgumentException("you must enter to the name");
}

At first glance, there is no problem with this approach. We just interrupt the flow and “notify” the user that the rule wasn’t satisfied. But let’s think more about that.

Let’s consider the same approach in a functionality that the user fills 10 fields. There are some problems:

  1. The system might have 10 requests until the user completes their task.

  2. Because of item 1, the user will get bored.

  3. The pepiline of the server will get full, and the GC mechanism will work more, hence the hardware will suffer.

  4. Because of so many requests, the performance of the system will surely degrade.

Specification Pattern

The specification pattern is a great way to organize and encapsulate business rules in such a way that we can create a group of validations and reuse all of them throughout the entire system. 

Although we have to write a bit more code, the benefits are many, as you can see in the list below.

Benefits

  1. The code is more flexibible.

  2. You can add and remove news rules or validations whenever it is necessary.

  3. The code becames testable.

  4. Business rules are isolated and reusable.

  5. You get rid of duplicate code.

As the basic premise here is to be concise and succinct, I decided won't put code here because the article would get pretty big, but I have a demo at my GitHub page: https://github.com/carlosbenicio/java-specifications.

In this demo, I have a class Person, and I have to validate some rules, including name, age, and gender. In orther to get it, I create a scope, register Person, and composite this scope with these three rules. This is a specification that implements "Specification interface and have the type of specification "Boolean IsSatisfiedBy(T t)". That is very simple, but very useful. I've used it here in my company and the results has been very satisfactory.

You can find a demo here: https://github.com/carlosbenicio/java-specifications.

Get the Java IDE that understands code & makes developing enjoyable. Level up your code with IntelliJ IDEA. Download the free trial.

Topics:
patterns of modular architecture ,software architecture ,software engineering ,java development

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}