Skip to content

creational pattern

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

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

Design Patterns

Design Patterns Logo Image


Design patterns are a collection of abstract solutions to recurrent problems in object-oriented software development. These solutions are presented like templates that can be applied in many situations and programming languages, but with similar structures, providing a design which contributes to system’s flexibility, extensibility, portability and code reuse.

Read More »Design Patterns