Skip to content

java

Java Objects

Java Objects logo


The Java Objects class is a utility class introduced in Java 7 and expanded in Java 8 to provide a collection of static methods to help operating on Java objects. It has null-safe and null-tolerant methods for computing the hash code of an object, returning a string for an object, comparing two objects, null checking and more.

According to Brian Goetz (Oracle’s architect for the Java language), its features are also being introduced into the core of the language, so there is a chance the Objects class becomes more popular in the years that are coming.

That said, we are about to explore how to work with the Objects class.

Read More »Java Objects

Singleton

Singleton Design Pattern Logo


Singleton is a creational design pattern which ensures that a class has only one instance and provides a global point to access it.

Introduction

Sometimes it makes sense in a system to have just only one instance of a certain class, e.g. configuration classes that read properties from a unique file or hardware access to send files to a printer.

Although Singleton seems to be one of the most popular patterns and yet, one of the easiest to implement, there are still certain features of the Java language like concurrency and serialisation, that may create some pitfalls in the pattern implementation, pitfalls we have to cover in order to rest assured that our Singleton instances are really singular.

That said, this post is not going to be “just yet another Singleton quick tutorial”, it is going to show how to safely write a piece of code that properly adheres to the pattern’s contract, both using a regular Java class and an alternative implementation using an enum.

Read More »Singleton

Null Object

Null Object Design Pattern logo

Null Object is a behavioural design pattern based on inheritance which creates a valid representation of null objects in a system, in order to avoid returning null objects, on which respective null checks would be needed to prevent NullPointerException and unexpected behaviour of the application objects.

Introduction

It’s common to write methods that return null in situations where the requested information is not present or some conditions are not met in order to execute some chunk of code. However, sometimes this behaviour is poorly documented so it takes the developers using a given API by surprise, moreover, it can force that said developers to write lots of null checks to avoid runtime exceptions.

On either way, the application code might end up with a lack of cohesion and not clean at all, because now this chunk of code has to deal with a “null possible situation” and take decisions that would not be supposed to be taken by itself.

The Null Object design pattern comes to work on this problem, basically, instead of returning null where an object of class Foo was expected, one could return an object of a subclass of Foo in a basic valid state but at the same time, adhering to Foo‘s contract.

Read More »Null Object

Builder

Builder Design Pattern Logo

Builder is a creational design pattern which aims on decoupling the construction logic of a complex object from its representation.

Introduction

Sometimes an object construction can be complex, because of parameter validations, search of informations on files or database, or even for numerous parameters the object’s constructor is waiting to receive. If you mix up the logic creational logic along the class’s behaviour logic, you may lose the class’s cohesion and can make it difficult to reuse.

Builder design pattern comes to solve this problem, putting both creational and behavioural logic on it right places, by providing enough encapsulation for object’s construction, in such a way you can even develop any number of different implementations for a single builder.

Read More »Builder