Skip to content

design patterns

Python function decorator

python function decorator post logo

This guide will show you how to create a Python function decorator with a few easy steps, to help you separate different concerns in code. This guide does not aim to provide reference material regarding decorators, but rather explain the basics and provide a starting point for implementing your own decorator.

What is a decorator?

A Python decorator is similar to a Java annotation if you are familiar with the JVM language, where you decorate or “annotate” a function prefixing the decorator’s name with @. For example, the decorator below publishes the function hello() on the url / on a Flask application:

def hello() -> str:
    return 'Hello world from Flask!'

More broadly speaking, a decorator is a structural design pattern that lets you add behaviour to an object, by wrapping its decorated function in a new function that has the same signature.

Read More »Python function decorator


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.


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.


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 Design Pattern Logo

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


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