Design-Patterns

Complete Journey of Design Patterns

Shanira Perera
5 min readJul 20, 2022

Hi all,

Welcome to the blog again. Today we’re going to talk about the Designs Patterns. OK, let’s find out what is meaning of design patterns.

  • Design Pattern is a general repeatable solution.
  • To a commonly occurring problem in software solutions.
  • This is not a completed design it cannot use directly to code.
  • This is like a template.

Why do we need Design Patterns?

  • We can improve our code readability and can speed up the development process.
  • The code is less likely to crash.

Design Pattern Categories

  • Creational
  • Structural
  • Behavioral

Creational Design Pattern

It deals with object creation mechanisms, trying to create objects suitable to the situation.

Ex: Singleton, Factory Method

Singleton Design Pattern

This is the most commonly used pattern. Singleton is one instance per container. If it’s a java program we can get one incidence per JVM. You should not excessive use because it is really hard to unit test. After all, there are no instances and there are no other references to create.

Uses of Singleton

  • As Logger.
  • As DB Connection.
  • Configuration read on an application.
  • Hardware interface access.
Example for Double Locking mechanism
Image by Author Example of Double-Locking mechanism.

Factory Design Pattern

In this pattern always use parameters to determine which instance want to get and in the singleton, we know the instance that we need to get but in this, we don’t know what is the instance to get.

Factory method patterns use in a higher level of a framework.

Image by Author via Tutorialspoint

In the Factory pattern, we create objects without exposing the creation logic to the client and refer to a newly created object using a common interface.

Prototype Design Pattern

This is not what we discuss previously because creating objects are very expensive so in this pattern, it wouldn’t create a new object it clones the existing object and gives it to use. This is not popular as previous patterns but this is very useful.

As an example, if we take a car registration system, it has thousands of vehicles to register in that case creating each object for that is very expensive. so that we use this pattern to create one instance and registered it and next time we can take a clone from it. That’s how the prototype design pattern is work.

Diagram of prototype design pattern
Image by Author via Tutorialspot

When you implement this prototype you need to consider this reason. when you clone your architecture you need to shallow copy or weather need a deep copy. a shallow copy means you just the first level of an object on the reference to the new object but the deep copy means it clones each object and the values to the new object.

NOTE: When you clone something and if you clone reference too. and you change something reference from there and it will affect to original one’s reference also so please be patient on that.

Builder Design Pattern

Builder pattern builds complex objects using simple objects and using step by step approach. Sometimes we need multiple constructors in that case we use This pattern to implement it. ok let’s clarify this using an example.

Think we have car renting service so when we rent a car other customer needs multiple features with that as insurance, road assistant, Highway Guides etc. so in that case if we traditionally create this we will create a lot of constructors. it’s very complicated your code.

In this case, some developers use telescopic constructors. what it means is you can have multiple constructors and each constructor uses other constructor depending on the parameter.

Some developers use Getters and Setters but the issue is using setters you can’t make it immutable because anyone can change the object after it creates. it’s not good at all.

Java String Builder is a very good example for this.

Example of Builder Design pattern
Image by Author via Tutorialspot

Chain Of Responsibility Design Pattern

Think if you have to implement a menu with separate permission like CEO, Manager, Employee likewise this pattern can use to implement it.

UML Diagram of Chain of Responsibility Pattern
Image by Author via Wikimedia

Ok, let’s talk about the what is the purpose that we need to use this pattern. in this pattern, the sender doesn’t know who is the receiver likewise receiver doesn’t know who is the sender therefore we can decouple the sender and the receiver..

Memento Design Pattern

A memento pattern is used to restore the state of an object to a previous state. like an Undo. it has three pillars

  • Originator — Maintain the state
  • Caretaker — Who keeps the track of the originator
  • Memento —old java object that contains basic

As an example when we create a shopping cart system we implement the cart item quantity if think we had 5 items we need to go back again to 4 items likewise another means is to roll back the shopping cart to the previous state. this implementation use this pattern.

Let’s talk about the Advantages and Disadvantages of this pattern

Advantages

  • You can simplify the originator’s code by letting the caretaker maintain the history of the originator’s state.
  • You can produce snapshots of the object’s state without violating its encapsulation.

Disadvantages

  • The app might consume lots of RAM if clients create mementoes too often.
  • Most dynamic programming languages, such as PHP, Python and JavaScript, can’t guarantee that the state within the memento stays untouched.
  • Caretakers should track the originator’s life cycle to be able to destroy obsolete mementoes.

Sign up to discover human stories that deepen your understanding of the world.

Free

Distraction-free reading. No ads.

Organize your knowledge with lists and highlights.

Tell your story. Find your audience.

Membership

Read member-only stories

Support writers you read most

Earn money for your writing

Listen to audio narrations

Read offline with the Medium app

Shanira Perera
Shanira Perera

Written by Shanira Perera

Hello, I’m Shanira, welcome to the world of technology. I am an Associate software engineer at virtusa. So let’s begin!

No responses yet

Write a response