What is design pattern? You must know if you are a senior programmer!

Saurav Satpathy
bitMountn
Published in
3 min readJun 11, 2019

Design Patterns are reusable solutions to commonly occurring problems. Design patterns encourages us to write clean and modular code. Every design pattern thrives on one concept, avoid touching the working code. We all know that once we touch working code, unknowingly we introduce defects, and a lot more testing needs to be done to ensure that we didn’t break anything.

These patterns helps minimise code duplication, prevents tight coupling and standardise way of writing code. The motto is to design flexible and reusable object-oriented code.

History: Design patterns were started as best practices and later they become popular after they were collected in a formal form in the Gang Of Four book.

We will discuss about four major types of design patterns:

Creational Design Pattern

Creational design patterns are design patterns that deal with object creation mechanisms. The basic form of object creation could result in design problems thus to solve we have few creational design patterns.

Why use Creational Pattern?

  1. It encapsulates object creation code, so that all object creation code is in one place, and any modifications would require changes only in one place.
  2. Code becomes more unit testable.
  3. It encourages Open Close & Dependency Inversion of SOLID principles, which is open for extension and closed for modification and high level modules shouldn’t directly depend on low level modules.
  4. Makes dynamic object creation simple, whereby the required concrete type can be returned based on some parameters. In real use cases we don’t generally create object by just init() rather we pass some type to it for example CarType.
enum CarType{
Sedan,
SUV,
HatchBack
}
createCar(type: CarType) -> Car

Example: Factory, Factory Method, Abstract Factory, Singleton, Prototype

Behavioural Design Pattern

Behavioural design patterns are concerned about interaction among objects. It provide solution for better interaction between objects while being loosely coupled with each other. Here are some examples of interactions:

—Hey, let me know if any update.

— Guys, I will let you all know when I have something to say.

— Do you know who lives next to your house?

Why use Behavioural Pattern?

— Encapsulate behaviours of object.

— Divide responsibilities among objects.

—Makes interaction among objects simple, and less dependent on each-other.

Example: Iterator, Delegation, Observer, Chain Of Responsibility, Memento

Structural Design Patterns

Structural design patterns are concerned about relationship among objects. Structural patterns explains how to assemble smaller components into larger structures, add new functionalities, while keeping the structures flexible and loosely coupled. It deals in inheritance and compositions.

Why use Structural Pattern?

— Enhances reusability.

— Reduces complexity by providing cleaner, simpler way to use a system.

— Helps in adding functionalities without modifying internal structure.

Example: Adapter, Facade, Decorator

Architectural Design Pattern

Architectural patterns are similar to other design patterns like creational, behavioural, structural etc. but have a broader scope. These patterns considers multiple layers of a system, like presentation layer, business layer, persistence layer.

Why use Architectural Pattern?

— Makes applications flexible and scalable.

— Makes code modular and avoids code duplication.

—Enable change quicker.

Example: MVC, MVVM, VIPER

*****************************************************************

Extra: The pattern discussed above are most commonly used, if you come across certain scenario which these pattern doesn’t solve you may look further in the list below and explore. Below is the wiki screenshot which has the comprehensive list of design patterns.

Image Credit: https://en.wikipedia.org/wiki/Software_design_pattern

References: Wiki, oodesign

--

--