# Software Design Patterns: Introduction

*2024-02-11*

> An introduction to software design patterns


Throughout my career, I kept finding myself writing code for solutions that, on the surface, looked increasingly similar to things I'd written before. What I didn't realise at the time was that I was inadvertently implementing patterns that have existed in software engineering for decades—the [Gang of Four design patterns](https://www.amazon.com.au/dp/B000SEIBB8).

Once I noticed this, I discovered most of these patterns were already well-documented on sites like [Wikipedia](https://en.wikipedia.org/wiki/Software_design_pattern). But I'd never actually sat down to learn them properly from scratch, stepping through each one with a debugger to understand how they really work.

There was another problem: working examples in C# were almost impossible to find. Most resources contained diagrams only, and the code samples that did exist were written in Java or other languages. What I wanted was dependency-free sample code that I could set a breakpoint in and watch execute, line by line.

So I sat down and worked through a 15-hour [Pluralsight course](https://app.pluralsight.com/library/courses/patterns-library/table-of-contents) on the subject, writing up each pattern as I went. This gave me both a learning exercise and a reference to return to later.

Working C# examples for every pattern in this series are available on GitHub:

[maceage/design-patterns](https://github.com/maceage/design-patterns)

## The Gang of Four patterns

The patterns fall into three broad categories:

| Category                                                             | Description                                                                                 |
|----------------------------------------------------------------------|---------------------------------------------------------------------------------------------|
| [Creational]({{< relref "software-design-patterns-creational" >}})   | Focuses on object creation mechanisms, creating objects in a manner suited to the situation |
| [Structural]({{< relref "software-design-patterns-structural" >}})   | Concerned with how classes and objects are composed together to form larger structures      |
| [Behavioural]({{< relref "software-design-patterns-behavioural" >}}) | 	Deals with object interaction and the delegation of responsibility                          |

Rather than one enormous post listing every pattern, each one gets its own detailed write-up:

- [Creational]({{< relref "software-design-patterns-creational" >}})
  - [Builder pattern]({{< relref "software-design-patterns-creational#builder-pattern" >}})
  - [Factory pattern]({{< relref "software-design-patterns-creational#factory-pattern" >}})
  - [Lazy Load pattern]({{< relref "software-design-patterns-creational#lazy-load-pattern" >}}) 
    - [Ghosts]({{< relref "software-design-patterns-creational#lazy-load-pattern-ghosts" >}})
    - [Lazy Init]({{< relref "software-design-patterns-creational#lazy-load-pattern-lazy-init" >}})
    - [Value Holder]({{< relref "software-design-patterns-creational#lazy-load-pattern-value-holder" >}})
    - [Virtual Proxy]({{< relref "software-design-patterns-creational#lazy-load-pattern-virtual-proxy" >}})
  - [Prototype pattern]({{< relref "software-design-patterns-creational#prototype-pattern" >}})
  - [Singleton pattern]({{< relref "software-design-patterns-creational#singleton-pattern" >}})
- [Structural]({{< relref "software-design-patterns-structural" >}})
  - [Adapter pattern]({{< relref "software-design-patterns-structural#adapter-pattern" >}})
  - [Bridge pattern]({{< relref "software-design-patterns-structural#bridge-pattern" >}})
  - [Composite pattern]({{< relref "software-design-patterns-structural#composite-pattern" >}})
  - [Decorator pattern]({{< relref "software-design-patterns-structural#decorator-pattern" >}})
  - [Façade pattern]({{< relref "software-design-patterns-structural#facade-pattern" >}})
  - [Flyweight pattern]({{< relref "software-design-patterns-structural#flyweight-pattern" >}})
  - [Marker pattern]({{< relref "software-design-patterns-structural#marker-pattern" >}})
  - [Proxy pattern]({{< relref "software-design-patterns-structural#proxy-pattern" >}})
  - [Repository pattern]({{< relref "software-design-patterns-structural#repository-pattern" >}})
- [Behavioural]({{< relref "software-design-patterns-behavioural" >}})
  - [Chain of Responsibility pattern]({{< relref "software-design-patterns-behavioural#chain-of-responsibility-pattern" >}})
  - [Command pattern]({{< relref "software-design-patterns-behavioural#command-pattern" >}})
  - [Event Aggregator pattern]({{< relref "software-design-patterns-behavioural#event-aggregator-pattern" >}})
  - [Interpreter pattern]({{< relref "software-design-patterns-behavioural#interpreter-pattern" >}})
  - [Iterator pattern]({{< relref "software-design-patterns-behavioural#iterator-pattern" >}})
      - [Iterator example]({{< relref "software-design-patterns-behavioural#iterator-iterator-example" >}})
      - [Tree example]({{< relref "software-design-patterns-behavioural#iterator-tree-example" >}})
  - [Mediator pattern]({{< relref "software-design-patterns-behavioural#mediator-pattern" >}})
  - [Memento pattern]({{< relref "software-design-patterns-behavioural#memento-pattern" >}})
  - [Null Object pattern]({{< relref "software-design-patterns-behavioural#null-object-pattern" >}})
  - [Observer pattern]({{< relref "software-design-patterns-behavioural#observer-pattern" >}})
    - [Traditional]({{< relref "software-design-patterns-behavioural#observer-traditional-pattern" >}})
    - [IObserver]({{< relref "software-design-patterns-behavioural#observer-iobserver-pattern" >}})
    - [EventDelegate]({{< relref "software-design-patterns-behavioural#observer-event-delegate" >}})
  - [Rules pattern]({{< relref "software-design-patterns-behavioural#rules-pattern" >}})
  - [State pattern]({{< relref "software-design-patterns-behavioural#state-pattern" >}})
  - [Strategy pattern]({{< relref "software-design-patterns-behavioural#strategy-pattern" >}})
  - [Template Method pattern]({{< relref "software-design-patterns-behavioural#template-method-pattern" >}})
  - [Visitor pattern]({{< relref "software-design-patterns-behavioural#visitor-pattern" >}})
