Design Patterns — Quick Guide

Basic Design patterns that every developer should know!

Kalana Tebel
5 min readOct 20, 2021

What are Design Patterns?

Basically, a design pattern is a general repeatable solution to a commonly occurring problem while building software or application. And keep in mind, designs pattern are not finished design that can be transformed directly into code. This will basically show the direction on how to solve a problem that can be used in many different situations.

Gang Of Four (GOF)

In 1994, four authors Erich Gamma, Richard Helm, Ralph Johnson and John Vlissides published a book titled Design Patterns — Elements of Reusable Object-Oriented Software which initiated the concept of Design Pattern in Software development.

These authors are collectively known as Gang of Four (GOF). According to these authors, design patterns are primarily based on the following principles of object-orientated design.

  • Program to an interface, not an implementation
  • Favor object composition over inheritance

Classification of GOF Design Patterns

As per the book Design Patterns — Elements of Reusable Object-Oriented Software they have mentioned 3 main categories of design patterns. Those are as follows.

  1. Creational

Creational Design patterns basically tell about the creation of the objects while hiding the creation logic.

Ex:- Singleton Pattern, Factory Method

2. Structural

These patterns are concerned about the relationship between objects.

Ex:- Facade, Decorator, Adapter

3. Behavioral

These patterns are specifically concerned with the communication or the interaction between objects.

Ex:- Observer, Momento

Hope now you all have a clear idea picture about the classification of design patterns. So now let’s move to have a look at some of the basic design patterns that are usually used in the industry.

Singleton Pattern

This is one of the simplest design patterns used by developers.

Definition

“Define a class that has only one instance and provides a global point of access to it”

This means that for a given class there can only be one instance.

There are two forms of Singleton design pattern.

  • Eager Instantiation: Instance is created every time when the class load even when we are not using the instance.
  • Lazy Instantiation: Instance is created at the time of calling of the get instance.

Pros of Singleton Pattern

  • Saves memory because an object is not created at each request. Only a single instance is reused again and again.

Usage

  • Used in logging, cache, session, drivers etc..

Singleton Design Patten Implementation

Eager Instantiation

Create a Private Static instance

  • We will be calling the instance directly from the class name.

Ex:- private static className instance = new className();

Define a private constructor.

  • Because if it is public then we can create multiple objects from outside. So we can create an object only from that class.

Ex:- private className(){}

Public Method to return the instance

  • Since the constructor is private we should have a public method to return the instance.

Ex:- public static className getInstance(){ return instance;}

Here’s a sample Java code for Eager Instatiation of Singleton Pattern

Eager Instantiation

In Eager Instantiation each and every time when the class is loading the object is created. This means that it will use the memory even though we are not using it. So since it is a problem what we will do is we will create the object only when the get instance method is called and this way is called Lazy Instantiation. Below is a sample code for that.

Lazy Instantiation

Facade Pattern

Facade is a part of Gang of Four design pattern and it is categorized under Structural design patterns. The facade pattern hides the complexities of the system and provides an interface to the client using which the client can access the system.

Pros of Facade Pattern

  • It shields the clients from the complexities of the sub-system components.
  • It promotes loose coupling between subsystems and their clients.

Usage

  • When you want to provide a simple interface to a complex sub-system.

Example

Let’s assume you want to go to a mobile shop to buy a phone. There you saw a wide range of mobile phones varieties and you want to know the model and the price of each phone. What you will do is you will ask from Shop Keeper who knows about his shop and he will tell you all the details of the mobile phone you want.

So in this case Shop keeper acts as the Facade, since he hides the complexities of the mobile shop system.

Sample Implementation Code (JAVA)

Observer Pattern

Observer pattern falls under the type of behavioral pattern category. This pattern is widely-used when there is one-to-many relationships between objects. It means that if one object is modified or changed, all its dependents objects should be notified automatically.

Observer pattern uses three actor classes. Subject, Observer, and Client. Subject is an object having methods to attach and detach observers to a client object.

Example

  • A real-world example of Observer pattern can be any social media platform such as Facebook/Youtube/Instagram. Let’s assume that you have subscribed to a YouTube channel(Subject) you likely most. So whenever that YouTube channel uploaded a video you(Observer) will be notified that the YouTube channel has uploaded a new video. But once you unsubscribe that YouTube channel, you will not get notifications from that YouTube channel in the future.

Sample Java Implementation code can be found here.

So that’s the end of this chapter. I hope you all gained a clear understanding of the basic design patterns that is being used by the developers more often and please share your ideas and thoughts in the comment section below.

Thank you and be safe.

--

--

Kalana Tebel

A Computer Science enthusiast. Software Engineer. Full Stack developer. Music Lover