Over a million developers have joined DZone.

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

Easily build powerful user management, authentication, and authorization into your web and mobile applications. Download this Forrester report on the new landscape of Customer Identity and Access Management, brought to you in partnership with Stormpath.

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.


  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.

Building Identity Management, including authentication and authorization? Try Stormpath! Our REST API and robust Java SDK support can eliminate your security risk and can be implemented in minutes. Sign up, and never build auth again!

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

The best of DZone straight to your inbox.

Please provide a valid email address.

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.

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

{{ parent.tldr }}

{{ parent.urlSource.name }}