22
DecGang of Four Design Patterns
Gang of Four Design Patterns
Gang of Four Design Patterns are basic concepts in application development. They provide proven answers to common design difficulties, hence improving system safety and maintainability. These patterns offer a straightforward and effective method for tackling complicated software design difficulties.
In this Design Pattern tutorial, we'll examine the Gang of Four Design Patterns, including "What are Design Patterns?" and "When to use them?" We'll also discuss several Gang of Four Design Patterns.So, let us begin by exploring "What are Design Patterns?"
What are the Gang of Four (GOF) Design Patterns?
- The Gang of Four Design Patterns is a collection of solutions to common challenges encountered in software design and development.
- They were initially introduced in 1994 with the publication of Design Patterns: Elements of Reusable Object-Oriented Software.
- The book was authored by Erich Gamma, Richard Helm, Ralph Johnson, and John Vlissides, also known as the Gang of Four.
Read More: |
Types of Design Patterns |
Different Types of Software Design Principles |
Why are they known as the Gang of Four?
- The name "Gang of Four" refers to the four authors Erich Gamma, Richard Helm, Ralph Johnson, and John Vlissides, who collaborated on the seminal book "Design Patterns: Elements of Reusable Object-Oriented Software" published in 1994.
- This book popularized the concept of design patterns in software engineering, significantly influencing how software design is approached.
Types of Gang of Four Design Patterns
The Gang of Four design patterns are divided into three categories:
- Creational Patterns: Attempt to generate things in a manner appropriate to the situation (e.g., Singleton, Factory Method, Abstract Factory, Builder, Prototype).
- Structural Patterns: Concerned with object composition, specifying how to combine items to achieve additional functionality (e.g., adapter, composite, proxy, flyweight, facade, bridge, decorator).
- Behavioral Patterns: These patterns describe how objects interact and how responsibilities are distributed (for example, Strategy, Observer, Command, Iterator, Mediator, Memento, State, Visitor, Template Method, Chain of Responsibility, Interpreter).
1. Creational Design Patterns
Creational design patterns are concerned with the generation of objects, ensuring that they are formed in a situationally appropriate manner and providing flexibility in how objects are instantiated.
The Gang of Four introduced the following main creational design patterns:
Types of Creational Design Patterns
- Factory Method Pattern: Consider the Factory Method Pattern as a method for creating flexible objects. It's like having a blueprint for building stuff. You define an interface for creating objects, but the actual generation is up to subclasses. This means that various subclasses can use the same method to make different sorts of objects.
- Abstract Factory Pattern: Assume you're in charge of a lavish dinner party and require matching tableware, cutlery, and decorations. The abstract factory pattern serves as a one-stop shop for all of these connected things. It allows you to establish object families, which ensures that everything you make fits together flawlessly.
- Singleton Pattern: This design is focused on exclusivity. It assures that each class has only one instance, similar to having a VIP pass to a club. You may access that instance from anywhere, making it useful in cases where your application requires a single point of control or coordination.
- Prototype Pattern: Instead of starting from scratch, you replicate an existing one to save time and resources.This design patternis useful when you have an object comparable to what you need but wantto make a few changes.
- Builder Pattern: The Builder Pattern is a set of instructions for creating anything complex. It allows you to build that complex item one piece at a time without worrying too much about the intricacies.
- Object Pool Pattern: Consider it a resource manager for reusable objects. Imagine a library that lends out books. Instead of buying a new book every time, you borrow one and return it when you're finished. The object pool stores a group of objects, such as database connections or threads, and distributes them when needed. This saves time and resources over repeatedly creating and removing things.
2. Structural Design Patterns
- A Structural Design pattern is a formula for combining many objects and classes to create a larger structure.
- It's similar to building a house based on a blueprint.
- These patterns tell us how to incorporate the distinct components of a system in a way that is simple to update or grow without affecting the overall system.
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: The Bridge Pattern distinguishes between an object's abstraction (how it acts) and its implementation. This promotes independent change.
- Composite Pattern: The Composite Pattern allows you to create hierarchical systems of varying complexities while still treating each part as a unique object, regardless of how simple or complex it is.
- Decorator Pattern: The Decorator Pattern lets you add additional behaviors or responsibilities to objects without changing their original code. It's like wrapping a present in multiple layers.
- Facade Pattern: The Facade Pattern simplifies the interface to a complex system, making it more usable.
- Flyweight Pattern: Flyweight Patterns allow us to save memory and resources by repeatedly employing the same kind of objects. Instead of producing many copies, they determine whether we already have one and, if not, generate a new one. In this manner, we avoid wasting space on identical objects.
3. Behavioral Design Patterns
- These patterns help resolve typical issues with how parts of codeshare work, disguise what they do, and stay organized.
- When developers adopt these patterns, it's like putting together a jigsaw, with the pieces fitting together effortlessly.
- This makes the software more organized, easy to alter, and less likely to break when we add or change things.
Types of Behavioral Design Patterns
- Chain of Responsibility Pattern: Design pattern that passes a request through a series of objects, each handling or passing it on, in order to make code more modular and flexible by separating request senders from receivers
- Command Pattern: The Command Pattern turns a request into a command object that encapsulates the operation and its arguments, separating the initiator from the executor.
- Iterator Pattern: The Iterator Pattern provides a way for accessing elements progressively within a collection without revealing the collection's internal structure.
- Mediator Pattern: The Mediator Pattern creates an intermediary entity that manages communication between groups of objects, reducing their dependence on one another.
- Memento Pattern: The Memento Pattern keeps an object's state, allowing it to be restored to the same state later, similar to saving a game to resume later.
- Observer Pattern: The Observer Pattern provides a relationship in which one object (the subject) is observed by multiple other things (known as observers).
- State Pattern: The State Pattern allows an entity to adapt its behavior as its internal state changes.
- Strategy Pattern: The Strategy Pattern entails establishing a set of various algorithms and making it easy to select and switch between them while a program is executing.
- Template Method Pattern: The Template Method Pattern defines a predefined recipe with steps that subclasses can customize, allowing them to change particular pieces without affecting the overall structure.
Summary
Gang of Four (GOF) Design Patterns provide standardized answers to common software design difficulties, which improves system safety and maintenance. These patterns are divided into three types: creational, structural, and behavioral, with each addressing a specific aspect of object creation, composition, and interaction. Understanding and applying these patterns leads to more robust and adaptive software architecture. Also, consider our Software Architecture and Design Certification Training for a better understanding of other Java concepts.