2 min read

Design Patterns

What are Design Patterns?

A software design pattern is a general, reusable solution to a commonly occurring problem within a given context.

There are three categories of design patterns:

  • Creational: Creational patterns provide various object creation mechanisms, which increase flexibility and reuse of existing code.
  • Structural: Structural patterns explain how to assemble objects and classes into larger structures while keeping these structures flexible and efficient.
  • Behavioural: Behavioural design patterns are concerned with algorithms and the assignment of responsibilities between objects.

Why use design patterns?

Design patterns provide us with a way to solve issues related to software development using a proven solution. This includes problems further down the line when it comes to refactoring or changing the code potentially being significantly easier and faster.

It helps us to write clean code in a way that other people can instantly recognise by also knowing the same pattern.

Design patterns also simplify communication. For example, in code reviews, instead of explaining every individual step of dependency injection which can be a lot to explain in a code review context, we can simply say: "Can you apply dependency injection here?"

How we should learn design patterns

Here outlined is a general framework for learning any skill, be it a programming technique or even martial arts. I will explain how this applies to design patterns specifically.

Four stages of learning

  • Ignorance: Not knowing the pattern exists
  • Awakening: Become aware the pattern exists
  • Overzealous: Typically it can be tempting to use the pattern everywhere, but through practice, you can learn what situations it fits and when it doesn't
  • Mastery: You've internalised the skills - you can almost do it automatically, you don't need to stop and consciously think about it, but you can just apply it effectively (like muscle memory)

T-Shaped Knowledge

T-Shaped Knowledge is about being generally at awakening level of all patterns (the horizontal line of the T). So you are aware of all of them, but then focus on mastery of a few patterns (the vertical line of the T).

What makes up a design pattern?

  • Name and classification: What is it called?
  • Intent: What is it used for?
  • Motivation or Scenario: What initial problem does this solve?
  • Applicability or Context: When/where is it good to apply this pattern?
  • Structure: Usually diagrams to illustrate the pattern and how it works, how the different parts communicate with each other
  • Participants: List of classes/objects in the pattern and what their role is
  • Collaboration: How do they interact with one and other?
  • Consequences: What are the potential downsides or trade-offs?
  • Sample Code: Examples
  • Known uses: Looking at real world implementations of the pattern
  • Related Patterns: Other similar patterns with slight differences

What constitutes basic knowledge of a pattern?

You want to have basic knowledge for all the popular design patterns as part of T-Shaped knowledge. In order to have basic knowledge of a design pattern, in other words the 'awakening' phase where you are aware of it, you need to know the name, intent, motivation and applicability.

Going Deeper - What constitutes advanced knowledge of a design pattern?

You want to have mastery of a few patterns, this is the vertical line of the T Shape described earlier. In order to have mastery, you need to know about the following:

  • Structure: Strong understanding of the pattern without having to review documentation and diagrams.
  • Participants and Collaboration: The different parts and how they interact with each other.
  • Implementation and consequences: When to use the pattern and what are the trade-offs and downsides of the pattern.