An Introduction To Software Design Patterns
Tags: programming, architecture, beginners, softwarequality
Published: 2022-12-07 21:05:30

**Short Introduction To All 23 Design Patterns Created By The Famous Gang Of Four.**

[Paul Knulst](https://www.paulsblog.dev/)  in  [Programming](https://www.paulsblog.dev/tag/programming/) • 7 min read

* * *

Design Pattern is a term used for a general, reusable solution to a commonly occurring problem in software design. It is important to understand that Design Patterns are not finished pieces of code that can be transformed and directly applied to your software. Rather they are more like a template, a description, or a concept that can give you an idea of how to solve a problem that can be used in many different situations.

They can incredibly useful when used in the right places and for the right reasons. When used strategically, they enhance your software and make it more efficient by methods, already refined by a myriad of developers.

Why Should You Use A Design Pattern?
------------------------------------

Using Design Patterns in software development has several benefits:

*   **They are proven solutions:** Design patterns are often used by many different developers, so you can be sure that they work as intended. Furthermore, you can be confident that they were revised multiple times and optimization has taken place already.
*   **They are easily reusable:** Design patterns document a reusable solution that can be modified to solve multiple particular problems because they are not tied to a specific problem.
*   **They are expressive:** Large solutions can be explained elegantly by Design Patterns.
*   **They ease communication:** Many developers are familiar with Design Patterns and use them easily to communicate about possible solutions to given problems with each other.
*   **They prevent the need for refactoring code:** Often if you use Design Patterns while developing software you could avoid refactoring it later. This only applies if you use the correct one for a given problem because it describes already an optimal solution.  
*   **They lower the size of the codebase:** Because Design Patterns are usually elegant, optimized, and well-documented solutions they require less code than other solutions.

Where It All Began
------------------

In 1994 an iconic computer science book "[Design Patterns: Elements of Reusable Object-Oriented Software](https://amzn.to/3VRn3gC)" was first published by four authors: [Erich Gamma](http://en.wikipedia.org/wiki/Erich_Gamma), [Richard Helm](http://en.wikipedia.org/wiki/Richard_Helm), [Ralph Johnson](http://en.wikipedia.org/wiki/Ralph_Johnson), and [John Vlissides](http://en.wikipedia.org/wiki/John_Vlissides). In technology circles, you'll often see these authors named "The Gang of Four" shortened to GoF.

Although the Gang of Four wrote the book in a C++ context it still remains very relevant to Java, JavaScript, Python, or any other object-oriented programming language. The authors, through their experience in creating large-scale enterprise systems using C++, saw 23 common patterns emerge. These 23 Design Patterns are not especially unique to C++ but can be applied in any other language.

Normally, as a developer that creates small to enterprise-class applications, you will encounter the Gang of Four Design Patterns on a daily basis.

The 23 GoF Design Patterns
--------------------------

| ![All 23 Design Patterns introduced by Gang of Four in 1994](https://www.paulsblog.dev/content/images/2022/09/image--37-.webp) |
|:--:|
| <center>*All 23 Design Patterns introduced by the Gang of Four*</center> |

The 23 Gang of Four Design Patterns are divided into three categories:

1.  **Creational**: For handling Object creation mechanisms. They are used to create objects, rather than having to instantiate objects directly. Your program will be more flexible in deciding which object it creates for a specific use case.
2.  **Structural**: For identifying ways to realize relationships between objects. This concerns class/object composition with inheritance to define different ways to gain new functionality.
3.  **Behavioral**: For handling communication between different objects.

### Creational

There are 5 Design Patterns in the creational category designed by the Gang of Four.

[**1\. Abstract Factory:**](https://en.wikipedia.org/wiki/Abstract_factory_pattern) Creates an instance of several families of classes without detailing concrete classes. It allows the creation of a factory for factory classes.

**[2\. Builder:](https://en.wikipedia.org/wiki/Builder_pattern)** Separates object construction from its representation, and always creates the same type of object. It creates an object step by step and a method to finally get the object instance.

[**3\. Factory Method:**](https://en.wikipedia.org/wiki/Factory_method_pattern) Moves the responsibility of instantiating an object from the class to a Factory. The Factory then makes an instance of several derived classes based on interfaced data or events.

**[4\. Prototype:](https://en.wikipedia.org/wiki/Prototype_pattern)** This pattern provides a mechanism to copy or clone the original object to a new object and then modify it according to your needs to avoid costly object creation.

[**5\. Singleton:**](https://en.wikipedia.org/wiki/Singleton_pattern) Restricts the initialization of a class to ensure that only one instance of the class can be created. Multiple initialization attempts will result in returning the same object over and over again.

### Structural

There are 7 Design Patterns in the structural category designed by the Gang of Four.

[**1\. Adapter:**](https://en.wikipedia.org/wiki/Adapter_pattern) Provides an interface between two unrelated entities in a way that they both can work together. It wraps an interface between already existing classes to enable working with each other without modifying the source code directly.

[**2\. Bridge:**](https://en.wikipedia.org/wiki/Bridge_pattern) Defines a strategy to decouple the interfaces from implementation and hide the implementation details from the client program. So both can vary independently. It uses encapsulation, aggregation, and inheritance to separate responsibilities into multiple classes.

[**3\. Composite:**](https://en.wikipedia.org/wiki/Composite_pattern) This pattern is used if you have to implement a part-whole hierarchy. It creates a group of objects that should be treated equally as a single instance of one object. It composes zero-or-more objects in a way that they can be manipulated as one single object.

**[4\. Decorator:](https://en.wikipedia.org/wiki/Decorator_pattern)** Is used for modifying the behavior of objects during runtime. It dynamically adds alternate processing to objects without affecting other objects from the same class.

**[5\. Facade:](https://en.wikipedia.org/wiki/Facade_pattern)** Provides a unified interface to a set of interfaces in a subsystem. It defines a higher level that helps to easier use of the subsystem. It masks the underlying complex or structural code and serves as a "_front-facing interface_"

**[6\. Flyweight:](https://en.wikipedia.org/wiki/Flyweight_pattern)** Describes a fine-grained instance used for the efficient sharing of information. It caches and reuses object instances, used with immutable objects to minimize memory usage.

**[7\. Proxy:](https://en.wikipedia.org/wiki/Proxy_pattern)** Provides a placeholder for another object. You should use it to control access, reduce cost, and reduce complexity.

### Behavioral

There are 11 Design Patterns in the behavioral category designed by the Gang of Four.

**[1\. Chain of Responsibility:](https://en.wikipedia.org/wiki/Chain-of-responsibility_pattern)** Describes a technique to delegate commands or pass requests between a chain of objects to find the object that can handle the command or request.

**[2\. Command:](https://en.wikipedia.org/wiki/Command_pattern)** This pattern encapsulates a command request as an object to enable, log, and/or queue requests. Furthermore, it provides error handling for unhandled command requests,

**[3\. Interpreter:](https://en.wikipedia.org/wiki/Interpreter_pattern)** Includes language-specific elements in the application to match the grammar of the intended language. It defines a grammatical representation of a language and the interpreter that works with this grammar.

**[4\. Iterator:](https://en.wikipedia.org/wiki/Iterator_pattern)** Describes a standardized way to traverse through a group of objects. It accesses the containing objects sequentially without exposing their underlying representation.

**[5\. Mediator:](https://en.wikipedia.org/wiki/Mediator_pattern)** Provides centralized communication between different objects in a system to enable loose coupling between different objects. It encapsulates how these different objects interact with each other.

**[6\. Memento:](https://en.wikipedia.org/wiki/Memento_pattern)** Stores a state of an object whenever it is saved to enable restoring previous states by providing undo operations.

**[7\. Observer:](https://en.wikipedia.org/wiki/Observer_pattern)** Notifies state changes to a set of subscribing classes to ensure consistency between all of them.

[**8\. State:**](https://en.wikipedia.org/wiki/State_pattern) Enables behavioral changes based on an internal state of an object. If the state of an object changes it also changes how it reacts to requests.

**[9\. Strategy:](https://en.wikipedia.org/wiki/Strategy_pattern)** This pattern is used when multiple algorithms for a specific task exist and a client decides which implementation should be used at runtime. It encapsulates the algorithm inside the class separating selection from implementation.

**[10\. Template Method:](https://en.wikipedia.org/wiki/Template_method_pattern)** Defines an abstract skeleton of an algorithm in such a way that subclasses have to implement the concrete behavior.

**[11\. Visitor:](https://en.wikipedia.org/wiki/Visitor_pattern)** Separates an algorithm from an object structure on which it operates. This enables adding new operations to existing object structures without changing the structure.

Conclusion
----------

Now that you have read this short introduction to software Design Patterns, you should have an idea about several important techniques used in nearly every programming language.

You should now hone your skills by implementing all or at least some of these Design Patterns into your side projects or just as practice. Also, you can start to contemplate how to apply them in your real projects.

Although, there are several implementations that benefit from utilizing Design Patterns keep in mind that they were never meant to be hacked together shortcuts to be applied in a 'one-size-fits-all' manner to your code. There is no perfect substitute for a genuine problem-solving ability in software engineering.

It is paramount to understand, Design Patterns provide a common language to conceptualize repeating problems and solutions while working with a team or managing large code bases.

That being said, the most important caveat is ensuring that the **how** and **why** behind each Design Pattern are fully understood by the developer.

* * *

If you enjoyed reading this article consider commenting your valuable thoughts in the comments section. I would love to hear your feedback about all the Design patterns I mentioned here. Furthermore, share this article with your friends and colleagues to also help them to know about Design Patterns.

This article was originally published on my blog at [https://www.paulsblog.dev/an-introduction-to-software-design-patterns/](https://www.paulsblog.dev/an-introduction-to-software-design-patterns/)

Feel free to connect with me on [my blog](https://www.paulsblog.dev), [Medium](https://medium.knulst.de), [LinkedIn](https://www.linkedin.com/in/paulknulst/), and [Twitter](https://twitter.com/paulknulst).

* * *

🙌 Support this content
-----------------------

If you like this content, please consider [supporting me](https://www.paulsblog.dev/support/). You can share it on social media or [buy me a coffee](https://buymeacoffee.com/paulknulst)! Any support helps!

Furthermore, you can [sign up for my newsletter](https://www.paulsblog.dev/newsletter/) to show your contribution to my content. See the [contribute page](https://www.paulsblog.dev/contribute/) for all (free or paid) ways to say thank you!

Thanks! 🥰

* * *

Photo generated by [WordClouds.com](https://www.wordclouds.com)