Gang of Four (GOF) Design Patterns
Last Updated :
31 Oct, 2023
The Gang of Four (GoF) Design Patterns, introduced in the book “Design Patterns: Elements of Reusable Object-Oriented Software,” authored by Erich Gamma, Richard Helm, Ralph Johnson, and John Vlissides, provide a catalog of proven solutions to common design problems in software development. The GoF Design Patterns encourage best practices, code reusability, and the separation of concerns, aiding in the development of robust and scalable applications.
What are Gang of Four (GOF) Design Patterns?
The Gang of Four Design Patterns is a set of solutions to common problems we encounter in software design and development. They were first introduced in the book Design Patterns: Elements of Reusable Object-Oriented Software, published in 1994. The book was written by Erich Gamma, Richard Helm, Ralph Johnson, and John Vlissides, who are collectively known as Gang of Four.
Why they’re called Gang of Four?
The Gang of Four are four smart people who wrote a book about clever ways to solve common problems in computer programming, and they’re called the Gang of Four because there were four of them who wrote the book together. Their book has been a big help to many programmers around the world.
Types of Gang of Four Design Patterns
The Gang of Four (GOF) patterns are set of 23 common software design patterns introduced in the book Design Patterns: Elements of Reusable Object-Oriented Software.
These patterns categorize into three main groups:
- Creational Patterns
- Structural Patterns
- Behavioral Patterns
These patterns provide solutions to common design problems and help make software systems more modular, flexible and maintainable.
Creational Design Patterns
Imagine you’re in a pizza restaurant, and you’re craving a delicious pizza. The Chef has to make it, right? But here’s the thing – the Chef doesn’t just randomly throw ingredients together. There’s a method to it, a recipe. In the world of software, we often need to create things too, just like Pizzas. That’s where Creational Design Patterns come in.
Think of Creational Design Patterns as those secret Pizza recipes that Chef use. These patterns help us create objects in a smart and organized way, just like how a Chef makes a perfect Pizza every time.
Creational Design Patterns focus on the process of object creation in software development. These patterns make sure that we create things in a way that’s not only easy but also flexible, so we can change them later if we need too. They hide the complicated details of how we put pieces together.
Types of Creational Design Patterns
- Factory Method Pattern: Think of it as a way to make objects with flexibility. It’s like having a blueprint for creating things. You define an interface for creating objects, but the actual creation is left to subclasses. This means different subclasses can create objects of different types using the same method.
- Abstract Factory Pattern: Imagine you’re in charge of a fancy dinner party, and you need matching tableware, cutlery and decorations. The abstract factory is like one-stop for all these related items. It provides a way to create families of objects, ensuring that everything you create fits together seamlessly.
- Singleton Pattern: This one’s all about exclusivity. It ensures that a class has just one instance, like having a VIP pass to a club. You can access that instance from anywhere, making it handy for situations where you want a single point of control or coordination in your application.
- Prototype Pattern: Instead of creating something from scratch, you make a copy of an existing one, saving time and resources. This is great when you have an object that’s similar to what you need, and you want to tweak it a bit.
- Builder Pattern: The Builder Pattern is like a set of instructions for making something complex. It helps you create that complex thing step by step, one piece at a time, without worrying too much about the details.
- Object Pool Pattern: Think of it as a resource manager for reusable items. Imagine a library that lends out books. Instead of buying a new book each time, you check one out and return it when you’re done. The object pool keeps a collection of objects, like database connections or threads, and hands them out when needed. This saves time and resources compared to createing and destroying objects frequently.
Structural Design Patterns
A Structural Design pattern is like a recipe for putting together different objects and classes to build a bigger structure. It’s a bit like following a blueprint to construct a house. These patterns teach us how to combine the unique parts of a system in a way that’s easy to change or expand without affecting the entire system.
Types of Structural Design Patterns
- Adapter Pattern: The Adapter Pattern allows one class to work with another class that has a different interface. It acts as a bridge between two incompatible interfaces.
- Bridge Pattern: The Bridge Pattern separates on object’s abstraction (how it behaves) from it’s implementation (how it’s done). This helps to change both independently.
- Composite Pattern: The Composite Pattern lets you the creation of hierarchical structures with various complexities while maintaining the ability to treat each element, whether simple or complex, as an individual entity.
- Decorator Pattern: The Decorator Pattern allows you to add new behaviors or responsibilities to objects without altering their existing code. It’s like adding layers of wrapping to a gift.
- Facade Pattern: The Facade Pattern provides a simplified interface to a complex system, making it easier to use.
- Flyweight Pattern: The Flyweight Patterns lets us save memory and resources by using the same kind of objects again and again. Instead of making lots of copies, it checks if we already have one, and if not, it creates a new one. This way, we don’t waste space on objects that are the same.
- Proxy Pattern: The Proxy Pattern provides a placeholder of another object to control access to it. It’s like having a remote control for a TV; you interact with the remote instead of directly with the TV.
Behavioral Design Patterns
Imagine you’re building a puzzle. In software development, we build complex programs like puzzles made up of many pieces (objects or classes). Behavioral design patterns are like special instructions for how these puzzle pieces should talk to each other and work together.
These patterns help solve common problems in how pieces of code share tasks, hide whay they do, and stay organized. When developers use these patterns, it’s like building a puzzle where the pieces fit together easily, making the software more organized, easy to change, and less likely to break when we need to add or change things. So it’s like having a guide to make sure all the parts of your software work together smoothly.
Types of Behavioral Design Patterns:
- Chain of Responsibility Pattern: The Chain of Responsibility Pattern is a desing pattern in software development. It lets you pass a request through a series of objects, each having its own logic to handle the request or pass it along the chain. This pattern helps in making code more modular and flexibly, as it seprates the sender of a request from the receiver, allowing multiple objects to participate in handling requests.
- Command Pattern: The Command Pattern is a behavioral design pattern that transforms a request into an independent object called a command. This pattern encapsulate all the necessary information for an action, bundling the action and its associated parameters into a single object. It’s primary purpose is to decouple the object triggering the operation from the object performing the operation.
- Iterator Pattern: The Iterator Pattern offers a mechanism for sequentially accessing elements within a collection without revealing the internal structure of the collection.
- Mediator Pattern: The Mediator Pattern creates a middleman object that manages communication between group of objects, making them less dependent on each other.
- Memento Pattern: The Memento Pattern is like taking a snapshot of an object’s current condition and saving it in a way that you can use it to bring the object back to that exact state in the future. It’s like saving a game so you can continue from where you left off.
- Observer Pattern: The Observer Pattern establishes a relationship where one thing (called the subject) is watched by several other things (knows as observers).
- State Pattern: The State Pattern enables an object to modify how it behaves as its internal state changes.
- Strategy Pattern: The Strategy Pattern involves defining a group of different algorithm and making it possible to choose and switch between them as needed while a program is running.
- Template Method Pattern: The Template Method is like providing a recipe with some fixed steps (skeleton), but allowing different cooks (subclasses) to add their unique ingredients or flavors (override specific parts) without changing the basic cooking instructions (overall structure).
Conclusion
Design patterns are like smart and efficient recipes for coding in the world of software development. They help us solve common problems and build software that works well, is easy to update, and can handle changes without breaking. The Gang of Four Design Patterns, introduced by a group of four experts in their famous book, Design Patterns: Elements of Reusable Object-Oriented Software. They are divided into three categories: Creational, Structural and Behavioral patterns, each serving a different purpose.
Similar Reads
Software Design Patterns Tutorial
Software design patterns are important tools developers, providing proven solutions to common problems encountered during software development. This article will act as tutorial to help you understand the concept of design patterns. Developers can create more robust, maintainable, and scalable softw
9 min read
Complete Guide to Design Patterns
Design patterns help in addressing the recurring issues in software design and provide a shared vocabulary for developers to communicate and collaborate effectively. They have been documented and refined over time by experienced developers and software architects. Important Topics for Guide to Desig
11 min read
Types of Software Design Patterns
Designing object-oriented software is hard, and designing reusable object-oriented software is even harder. Christopher Alexander says, "Each pattern describes a problem which occurs over and over again in our environment, and then describes the core of the solution to that problem, in such a way th
9 min read
1. Creational Design Patterns
Creational Design Patterns
Creational Design Patterns focus on the process of object creation or problems related to object creation. They help in making a system independent of how its objects are created, composed, and represented. Creational patterns give a lot of flexibility in what gets created, who creates it, and how i
4 min read
Types of Creational Patterns
2. Structural Design Patterns
Structural Design Patterns
Structural Design Patterns are solutions in software design that focus on how classes and objects are organized to form larger, functional structures. These patterns help developers simplify relationships between objects, making code more efficient, flexible, and easy to maintain. By using structura
7 min read
Types of Structural Patterns
Adapter Design Pattern
One structural design pattern that enables the usage of an existing class's interface as an additional interface is the adapter design pattern. To make two incompatible interfaces function together, it serves as a bridge. This pattern involves a single class, the adapter, responsible for joining fun
8 min read
Bridge Design Pattern
The Bridge design pattern allows you to separate the abstraction from the implementation. It is a structural design pattern. There are 2 parts in Bridge design pattern : AbstractionImplementationThis is a design mechanism that encapsulates an implementation class inside of an interface class. The br
4 min read
Composite Method | Software Design Pattern
Composite Pattern is a structural design pattern that allows you to compose objects into tree structures to represent part-whole hierarchies. The main idea behind the Composite Pattern is to build a tree structure of objects, where individual objects and composite objects share a common interface. T
9 min read
Decorator Design Pattern
The Decorator Design Pattern is a structural design pattern that allows behavior to be added to individual objects dynamically, without affecting the behavior of other objects from the same class. It involves creating a set of decorator classes that are used to wrap concrete components. Important To
9 min read
Facade Method Design Pattern
Facade Method Design Pattern is a part of the Gang of Four design patterns and it is categorized under Structural design patterns. Before we go into the details, visualize a structure. The house is the facade, it is visible to the outside world, but beneath it is a working system of pipes, cables, a
8 min read
Flyweight Design Pattern
The Flyweight design pattern is a structural pattern that optimizes memory usage by sharing a common state among multiple objects. It aims to reduce the number of objects created and to decrease memory footprint, which is particularly useful when dealing with a large number of similar objects. Table
10 min read
Proxy Design Pattern
The Proxy Design Pattern a structural design pattern is a way to use a placeholder object to control access to another object. Instead of interacting directly with the main object, the client talks to the proxy, which then manages the interaction. This is useful for things like controlling access, d
9 min read
3. Behvioural Design Patterns
Behavioral Design Patterns
Behavioral design patterns are a category of design patterns that focus on the interactions and communication between objects. They help define how objects collaborate and distribute responsibility among them, making it easier to manage complex control flow and communication in a system. Table of Co
5 min read
3. Types of Behvioural Patterns
Chain of Responsibility Design Pattern
The Chain of Responsibility design pattern is a behavioral design pattern that allows an object to pass a request along a chain of handlers. Each handler in the chain decides either to process the request or to pass it along the chain to the next handler. Table of Content What is the Chain of Respon
10 min read
Command Design Pattern
The Command Design Pattern is a behavioral design pattern that turns a request into a stand-alone object called a command. With the help of this pattern, you can capture each component of a request, including the object that owns the method, the parameters for the method, and the method itself. By d
10 min read
Interpreter Design Pattern
The Interpreter Design Pattern is a behavioral design pattern used to define a language's grammar and provide an interpreter to process statements in that language. It is useful for parsing and executing expressions or commands in a system. By breaking down complex problems into smaller, understanda
10 min read
Mediator Design Pattern
The Mediator Design Pattern simplifies communication between multiple objects in a system by centralizing their interactions through a mediator. Instead of objects interacting directly, they communicate via a mediator, reducing dependencies and making the system easier to manage. Table of Content Wh
7 min read
Memento Design Pattern
The Memento Design Pattern is a behavioral pattern that helps save and restore an object's state without exposing its internal details. It is like a "snapshot" that allows you to roll back changes if something goes wrong. It is widely used in undo-redo functionality in applications like text editors
6 min read
Observer Design Pattern
The Observer Design Pattern is a behavioral design pattern that defines a one-to-many dependency between objects. When one object (the subject) changes state, all its dependents (observers) are notified and updated automatically. Table of Content What is the Observer Design Pattern?Real-world analog
8 min read
State Design Pattern
The State design pattern is a behavioral software design pattern that allows an object to alter its behavior when its internal state changes. It achieves this by encapsulating the object's behavior within different state objects, and the object itself dynamically switches between these state objects
11 min read
Strategy Design Pattern
The Strategy Design Pattern defines a family of algorithms, encapsulates each one, and makes them interchangeable, allowing clients to switch algorithms dynamically without altering the code structure. Table of Content What is the Strategy Design Pattern?Components of the Strategy Design PatternComm
11 min read
Template Method Design Pattern
The Template Method Design Pattern is a behavioral design pattern that provides a blueprint for organizing code, making it flexible and easy to extend. With this pattern, you define the core steps of an algorithm in a method but allow subclasses to override specific steps without changing the overal
8 min read
Visitor design pattern
An object-oriented programming method called the Visitor design pattern makes it possible to add new operations to preexisting classes without changing them. It improves the modularity and maintainability of code, which makes it perfect for operations on a variety of object structures. Its advantage
7 min read
Top Design Patterns Interview Questions [2024]
A design pattern is basically a reusable and generalized solution to a common problem that arises during software design and development. Design patterns are not specific to a particular programming language or technology instead, they provide abstract templates or blueprints for solving recurring d
9 min read
Software Design Pattern in Different Programming Languages
Java Design Patterns Tutorial
Design patterns in Java refer to structured approaches involving objects and classes that aim to solve recurring design issues within specific contexts. These patterns offer reusable, general solutions to common problems encountered in software development, representing established best practices. B
8 min read
Python Design Patterns Tutorial
Design patterns in Python are communicating objects and classes that are customized to solve a general design problem in a particular context. Software design patterns are general, reusable solutions to common problems that arise during the design and development of software. They represent best pra
8 min read
Modern C++ Design Patterns Tutorial
Design patterns in C++ help developers create maintainable, flexible, and understandable code. They encapsulate the expertise and experience of seasoned software architects and developers, making it easier for newer programmers to follow established best practices. What are C++ Design Patterns?A des
7 min read
JavaScript Design Patterns Tutorial
Design patterns in Javascipt are communicating objects and classes that are customized to solve a general design problem in a particular context. Software design patterns are general, reusable solutions to common problems that arise during the design and development of software. They represent best
8 min read
Software Design Pattern Books
Gang of Four (GOF) Design Patterns
The Gang of Four (GoF) Design Patterns, introduced in the book "Design Patterns: Elements of Reusable Object-Oriented Software," authored by Erich Gamma, Richard Helm, Ralph Johnson, and John Vlissides, provide a catalog of proven solutions to common design problems in software development. The GoF
8 min read
Design Patterns Gamma
Elements of Reusable Object-Oriented Software is a software engineering book describing software design patterns. The book was written by Erich Gamma, Richard Helm, Ralph Johnson, and John Vlissides, with a foreword by Grady Booch. The book is divided into two parts, with the first two chapters expl
6 min read
Software Design Pattern in Development
Some other Popular Design Patterns
Introduction to Pattern Designing
In software development, pattern designing refers to the application of design patterns, which are reusable and proven solutions to common problems encountered during the design and implementation of software systems. Important Topics for Pattern Designing What is Pattern Designing?Characteristics o
6 min read
Catalog of Design Patterns
A Pattern, in general, refers to the particular layout which helps to perform certain tasks with ease. Technically, patterns can be considered as devices that help the program in sharing the knowledge of their design. The term design pattern is used in object-oriented terminology to perform the task
6 min read
Front Controller Design Pattern
The front controller design pattern means that all requests that come for a resource in an application will be handled by a single handler and then dispatched to the appropriate handler for that type of request. The front controller may use other helpers to achieve the dispatching mechanism. UML Dia
3 min read
Java Dependency Injection (DI) Design Pattern
The dependency Injection Design Pattern in Java, is part of Design Patterns, let suppose that you have a company, that companies dependent on the employees, and the employees is dependent on the company. So here company will be the dependency of the employee, and employee dependency is the company.
11 min read
Caching Design Pattern
In today's digital world, speed and efficiency matter a lot. When we use apps and websites, we want things to happen quickly. But making applications run fast is a bit tricky. That's where the caching design pattern comes in. Imagine caching as a fast storage trick. It stores important data from app
10 min read
Repository Design Pattern
The Repository design pattern is a structural pattern that abstracts data access, providing a centralized way to manage data operations. By separating the data layer from business logic, it enhances code maintainability, testability, and flexibility, making it easier to work with various data source
6 min read
Design Patterns in Different Languages
Modern C++ Design Patterns Tutorial
Design patterns in C++ help developers create maintainable, flexible, and understandable code. They encapsulate the expertise and experience of seasoned software architects and developers, making it easier for newer programmers to follow established best practices. What are C++ Design Patterns?A des
7 min read
Java Design Patterns Tutorial
Design patterns in Java refer to structured approaches involving objects and classes that aim to solve recurring design issues within specific contexts. These patterns offer reusable, general solutions to common problems encountered in software development, representing established best practices. B
8 min read
JavaScript Design Patterns Tutorial
Design patterns in Javascipt are communicating objects and classes that are customized to solve a general design problem in a particular context. Software design patterns are general, reusable solutions to common problems that arise during the design and development of software. They represent best
8 min read
Python Design Patterns Tutorial
Design patterns in Python are communicating objects and classes that are customized to solve a general design problem in a particular context. Software design patterns are general, reusable solutions to common problems that arise during the design and development of software. They represent best pra
8 min read
Design Patterns in C sharp (C#)
Design patterns in C Sharp (C#) help developers create maintainable, flexible, and understandable code. They encapsulate the expertise and experience of seasoned software architects and developers, making it easier for newer programmers to follow established best practices. Important Topics for the
15+ min read