What are design patterns?
Tags: java, cleancode, oop, designpatterns
Published: 2019-12-22 12:58:01

---
title: What are design patterns?
published: true
date: 2019-12-22 13:59:22 UTC
tags: ["java", "cleancode", "oop", "designpatterns"]
canonical_url: https://talkoverflow.com/posts/what-are-design-patterns/
---

## <svg viewbox="0 0 16 16" width="16" height="16" fill="currentcolor" aria-hidden="true"><path fill-rule="evenodd" d="M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"></path></svg>High-level overview

**OOP** (object-oriented programming) gurus once concluded that it’s pointless to reinvent the wheel every time you encounter a problem in application design. They knew they should not always tackle problems from the ground up. Instead, they gathered and categorized **battle-tested approaches in structuring code** that were **reoccurring in many projects**. This is why in many systems, applications and frameworks we see repetitive patterns and class diagrams.

Reusing known patterns is not specific to software development. Take a look around you. In literature, themes, motifs and symbols from the bible or other ancient sources are still present in modern writings. This applies also to other aspects of life.

> **Patterns that worked before are likely to work now. This, in theory, is a promise that design patterns give us.**

Before we introduce any specific design pattern, let’s first discuss what are they made of and how they are categorized.

## <svg viewbox="0 0 16 16" width="16" height="16" fill="currentcolor" aria-hidden="true"><path fill-rule="evenodd" d="M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"></path></svg>Design Pattern structure

According to [GOF](https://www.amazon.com/Design-Patterns-Elements-Reusable-Object-Oriented/dp/0201633612), a design pattern is made of **four elements** :

1. **Name** - one or two words that describe the pattern
2. **Problem description** - describes when to use the given pattern
3. **Solution** - abstract description of classes, objects and their relations in the pattern
4. **Consequences** - pros and cons of using the pattern

Each pattern belongs to one of **three categories** :

- **Creational** - patterns responsible for object creation.

- **Structural** - patterns involved in the way of composing classes and objects.

- **Behavioural** - patterns that describe the way objects and classes work, share tasks and cooperate.

Besides this, there is one more important thing that separates design patterns:

### <svg viewbox="0 0 16 16" width="16" height="16" fill="currentcolor" aria-hidden="true"><path fill-rule="evenodd" d="M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"></path></svg>The Scope

**Class-scope** - when pattern is categorized as class-scope, it means that its goals are achieved at the compile time using inheritance

**Object-scope** - when pattern is categorized as object-scope its goals are achieved through composition during the runtime

## <svg viewbox="0 0 16 16" width="16" height="16" fill="currentcolor" aria-hidden="true"><path fill-rule="evenodd" d="M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"></path></svg>Summary

**A design pattern is a battle-tested, proven and general solution to a commonly occurring problem.** It describes how to solve the given problem and can be used in many different situations.