Gang of Four
Gang of Four
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:
1. Creational Patterns
1. Structural Patterns
1. 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
1. 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.
1. 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.
1. 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.
1. 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.
1. 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.
1. 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 creating 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
1. 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.
1. 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.
1. 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.
1. 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.
1. Facade Pattern: The Facade Pattern provides a simplified interface to a complex
system, making it easier to use.
1. 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.
1. 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:
1. 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 separates the
sender of a request from the receiver, allowing multiple objects to participate in
handling requests.
1. 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.
1. Iterator Pattern: The Iterator Pattern offers a mechanism for sequentially
accessing elements within a collection without revealing the internal structure of the
collection.
1. Mediator Pattern: The Mediator Pattern creates a middleman object that manages
communication between group of objects, making them less dependent on each
other.
1. 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.
1. Observer Pattern: The Observer Pattern establishes a relationship where one thing
(called the subject) is watched by several other things (knows as observers).
1. State Pattern: The State Pattern enables an object to modify how it behaves as its
internal state changes.
1. Strategy Pattern: The Strategy Pattern involves defining a group of different
algorithms and making it possible to choose and switch between them as needed
while a program is running.
1. 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).