Design Patterns

Tharushi Chamalsha
4 min readAug 26, 2022

What is a design pattern?

Design pattern is a quality and general solution for the problems frequently emerged in software development process. Design patterns are used to represent some of the best practices adapted by experienced object-oriented software developers.

In this article I am going to describe about 5 mostly used design patterns in the software development process. Firstly lets get to know what are the benefits we can get using these design patterns.

  1. Time saving. Since the code becomes reusable you dont need to duplicated the code again and again.
  2. Communication will become easier within the development team.
  3. Increase the testability of the code.
  4. Increase the extensibility of the code.
  5. More maintainable codes.

Design patterns can be divided into 3 parts.

01 creational : Describe the way you create the objects.

ex: Factory , Singleton

02 structural : Describe the structure of the objects.

ex: Adaptor , Decorator

03 behavioural : Describe the behaviour of the objects.

ex: Observer

Now let’s go through these patterns one by one.

Singleton pattern

Singleton pattern is used when we want only one instance of a class. This restrict the the instantiation of a class to one object.

ex :-DB connection, clipboard, configurations

We dont need several instance when it more resource consuming or if we want to centralise to one object.

Steps : 1. make the constructor of the class private to restrict instantiation from other class.

2. Private static variable of the same class that is the only instance of the
class.

3. Public static method that returns the instance of the class, this is the global
access point for the outer world to get the instance of the singleton class.

Observer Pattern

When we have events that happen constantly and other classes need to capture those data then we have to use this design pattern.

Simply when there is one-to-many relationship between objects such as if one object is modified, its dependent objects are to be notified automatically observer pattern is used. Observer pattern falls under behavioural pattern category.

Ex:- Think about a cricket match application which notify the subscribers about the match status. You have use this observer pattern to this purpose.

Observer Pattern Class Diagram

Factory Pattern

When we have one interface and multiple implementations of that
and at run time we have to decide which object to create we can use this pattern.

Factory pattern is one of the most used design patterns in Java.

Ex:- Think about a calculator application which has several operations and user has to select which operation to do in that moment. We use factory design pattern here.

Factory Pattern Class Diagram

Adapter pattern

This pattern acts as a bridge between two interfaces that are incompatible.
It is often used to make existing classes work without modifying their source code.

This type of design pattern comes under structural pattern as this pattern combines the capability of two independent interfaces.

Ex:- Think about a media player. You have several types of files to play. Mp3,Mp4,JPG,PNG etc. Here Mp3 and Mp4 can be played using normal play method.But image files cannot play like that. So you have to override that play method. To do that you can use adapter class.

Adapter Pattern Class Diagram

Decorator pattern

Using this pattern you can add new functionalities to an exiting object without changing it.

This type of design pattern comes under structural pattern as this pattern acts as a wrapper to existing class.

Ex:-Think about you are going to implement a shape displaying program. There are different types of shapes. And you need to add colours to those shapes. To add these features you can use decorator pattern, so that you do not have to change existing code.

Decorator Pattern Class Diagram

You can refer following Github repository to see how the above examples are applied in code.

https://github.com/tchamalsha/Architecture-design-patterns

--

--