-
Gang of Four
-
In the context of design patterns in computer science, the "Gang of Four" refers to a group of four authors who wrote the seminal book "Design Patterns: Elements of Reusable Object-Oriented Software."
This book, often simply referred to as the "Gang of Four book" or "GoF book," was published in 1994 and is considered a classic in software engineering literature.
The Gang of Four consists of:
- Erich Gamma: Known for his work on software design patterns and for being one of the leaders in the Eclipse project.
- Richard Helm: An expert in object-oriented software engineering.
- Ralph Johnson: Contributed significantly to the development of design patterns and is a professor at the University of Illinois at Urbana-Champaign.
- John Vlissides: Was involved in the study and promotion of design patterns in software engineering.
Their book consists of 23 classic software design patterns, providing insights into how these patterns can be applied to solve recurring problems in object-oriented design.
The patterns described in the book are divided into three main categories:
- Creational patterns
- Structural patterns
- Behavioral patterns
These patterns provide solutions to common design problems and help developers create more maintainable, flexible, and reusable software components.
1. Creational Design Patterns
Creational design patterns are a category of design patterns in software engineering that deal with the process of object creation, ensuring that objects are created in a manner suitable for the situation and are created efficiently, flexibly, and in a way that promotes reusability.
These patterns provide various ways to instantiate objects or manage the instantiation process, depending on the specific needs of the application. There are several commonly recognized creational design patterns, each addressing different aspects of object creation:
Types of Creational Design Patterns
- Singleton ensures a single instance of a class exists throughout the application.
- Factory Method allows subclasses to decide on the exact objects to instantiate.
- Abstract Factory provides a way to create families of related objects.
- Builder separates the construction of a complex object from its representation.
- Prototype uses cloning to create new objects based on existing ones.
Structural design patterns are a category of design patterns in software engineering that focuses on simplifying the structure of classes and objects. These patterns deal with how objects are composed into larger structures, while also making these structures flexible and efficient.
Here are some commonly recognized structural design patterns:
Types of Structural Design Patterns
- Adapter Pattern: The Adapter Pattern enables one class to interact with another that has a different interface. It serves as a bridge between two incompatible interfaces.
- Bridge Pattern: This distinguishes between an object's abstraction (how it acts) and its implementation. This promotes independent change.
- Composite Pattern: The Composite Pattern composes objects into tree structures to represent part-whole hierarchies. Clients can treat individual objects and compositions of objects uniformly.
- Decorator Pattern: Adds behavior or responsibilities to objects dynamically without altering their structure. It involves attaching additional responsibilities to an object dynamically.
- Facade Pattern: Provides a unified interface to a set of interfaces in a subsystem. It defines a higher-level interface that makes the subsystem easier to use.
- Flyweight Pattern: Flyweight Patterns allow us to save memory and resources by repeatedly employing the same kind of objects. Instead of producing many copies, it determines whether we already have one and, if not, generates a new one. In this manner, we avoid wasting space on identical objects.
- Proxy Pattern:Provides a surrogate or placeholder for another object to control access to it. It involves creating a proxy object that acts as an intermediary between the client and the real object.
Behavioral design patterns are a category of design patterns in software engineering that focus on the interaction and communication between objects. When developers adopt these patterns, it's like putting together a block puzzle(Jigsaw), with the pieces fitting together effortlessly, making the software more organized, easy to alter, and less likely to break when we add or change things.
These patterns characterize how classes and objects interact and distribute responsibilities among them. They emphasize the ways in which objects collaborate and communicate to fulfill a given task or responsibility.
Types of Behavioral Design Patterns
- Chain of Responsibility Pattern: Allows multiple objects to handle a request without specifying the receiver explicitly. It lets more than one object handle the request by chaining them together and passing the request along the chain until an object handles it.
- Command Pattern: Converts a request into a separate object known as a command. This pattern encapsulates all of the relevant information for an action, combining it with its associated parameters into a single object. Its major function is to separate the object that initiates the action from the object that performs it.
- Iterator Pattern: Provides a way for accessing elements progressively within a collection without revealing the collection's internal structure.
- Mediator Pattern: Defines an object that encapsulates how a set of objects interact. It promotes loose coupling by keeping objects from referring to each other explicitly and allows their interactions to vary independently.
- Memento Pattern: Captures and externalizes an object's internal state without violating encapsulation, so that the object can be restored to this state later. Eg: similar to taking a snapshot of an item's current situation and saving it in a way that allows you to restore the object to that identical state in the future. It's similar to saving a game so you can continue from where you left off.
- Observer Pattern: Defines a one-to-many dependency/relation between objects so that when one object changes state, all its dependents are notified and updated automatically.
- State Pattern: Allows an object to alter its behavior when its internal state changes.
- Strategy Pattern: defines a set of various algorithms, encapsulates each one, and makes them interchangeable. therefore making it easy to select and switch between them while a program is executing.
- Template Method Pattern: Defines the skeleton of an algorithm in a method, deferring some steps to subclasses. It allows subclasses to redefine certain steps of an algorithm without changing its structure.Eg: offering a recipe with some fixed stages (skeleton) but allows other cooks (subclasses) to add their unique ingredients or flavors (override particular parts) without modifying the main cooking instructions (overall structure).
- Visitor Pattern:Defines a new operation to a collection of objects without changing the objects themselves. It separates an algorithm from the object structure it operates on.
The Gang of Four (GoF) Design Patterns are 23 tested solutions to common software design issues. They promote best practices, code reuse, and separation of concerns, which results in resilient and scalable systems. These patterns are divided into three categories: creational, structural, and behavioral, and each addresses a distinct design difficulty. Using GoF Design Patterns, developers can design code that is clearer, more maintainable, and more versatile.