Holi Sale. Get upto 40% OFF on Job-oriented Training! Offer Ending in
D
H
M
S
Get Now
Gang of Four Design Patterns

Gang of Four Design Patterns

19 Jan 2024
Beginner
231K Views
7 min read
Learn via Video Course & by Doing Hands-on Labs

⭐ .NET Design Patterns Course: Design Patterns in C# Online Training

Gang of Four Design Patterns in .NET: An Overview

The Gang of Four (GoF) Design Patterns, described in Erich Gamma, Richard Helm, Ralph Johnson, and John Vlissides' book "Design Patterns: Elements of Reusable Object-Oriented Software," is a collection of tried-and-true answers to common design difficulties in software development. The GoF Design Patterns promote best practices, code reuse, and separation of concerns, which aids in the creation of robust and scalable systems. If you want to improve your understanding and application of these strong patterns, check out our informative Design Pattern Tutorial for practical tips.

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 Gang of Four.

Why are they known as the Gang of Four?

The Gang of Four are four smart people who wrote a book about clever approaches to handling common computer programming difficulties. They are known as the Gang of Four since they wrote the book together. Their book has proven extremely beneficial to many programmers throughout the world.

Types of Gang of Four Design Patterns

The Gang of Four (GOF) patterns are a collection of 23 commonly used software design patterns described in the book Design Patterns: Elements of Reusable Object-Oriented Software.

These patterns are divided into three main categories:

  1. Creational Patterns
  2. Structured Patterns
  3. Behavioural Patterns

1. Creational Design Patterns

Creational Design Patterns concentrate on the process of object creation in software development. These patterns ensure that we develop things in a way that is both easy and adaptable, allowing us to update them later if necessary. They conceal the complex details of how we put things together.

Types of Creational Design Patterns

  1. 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 create different sorts of objects.
  2. Abstract Factory Pattern: Assume you're in charge of a lavish dinner party and require matching tableware, cutlery, and decorations. The abstract factory 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.
  3. 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 instances when your application requires a single point of control or coordination.
  4. Prototype Pattern: Instead of starting from scratch, you replicate an existing one to save time and resources. This is useful when you have an object that is comparable to what you need but wants to make a few changes.
  5. 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.
  6. 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 combine 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

  1. 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.
  2. Bridge Pattern: The Bridge Pattern distinguishes between an object's abstraction (how it acts) and its implementation. This promotes independent change.
  3. 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.
  4. 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.
  5. Facade Pattern: The Facade Pattern simplifies the interface to a complex system, making it more usable.
  6. 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.

3. Behavioral Design Patterns

These patterns help in resolving typical issues with how parts of codeshare jobs, 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, making the software more organized, easy to alter, and less likely to break when we add or change things. 

Types of Behavioral Design Patterns

  1. Chain of Responsibility Pattern: A design pattern used in software development. It allows you to route a request through a sequence of objects, each with its logic for processing the request or passing it along the chain. This technique helps to make code more modular and flexible by separating the sender of a request from the receiver, allowing multiple objects to participate in request handling.
  2. Command Pattern: The Command Pattern is a behavioral design pattern that 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.
  3. Iterator Pattern: The Iterator Pattern provides a way for accessing elements progressively within a collection without revealing the collection's internal structure.
  4. Mediator Pattern: The Mediator Pattern creates an intermediary entity that manages communication between groups of objects, reducing their dependence on one another.
  5. Memento Pattern: The Memento Pattern is 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.
  6. Observer Pattern: The Observer Pattern provides a relationship in which one object (the subject) is observed by multiple other things (known as observers).
  7. State Pattern: The State Pattern allows an entity to adapt its behavior as its internal state changes.
  8. 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.
  9. Template Method Pattern: The Template Method Pattern is similar to 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).
Summary

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,.NET developers may design code that is clearer, more maintainable, and more versatile.

FAQs

Q1. What is the builder pattern in Gang of Four?

The builder pattern is a design pattern that provides a versatile solution to many object construction issues in object-oriented programming. The builder design pattern aims to isolate the creation of a complex entity from its representation. It's one of the Gang of Four design patterns.

Q2. What is the command structure in Gang of Four?

The Command Pattern is a GoF (Gang of Four) design pattern. The purpose of this design pattern is to encapsulate method calls, allowing you to separate the class that invokes a method from the class that contains the function. The Command Pattern additionally supports "undo" and "redo" instructions.

Q3. Why use a gang of four design patterns?

Because there were four creators, it was given the name "Gangs of Four" design patterns. In addition, it was condensed to "GoF Design Patterns."

Q4. What did the Gang of Four do?

The Gang of Four controlled the CCP's power organs in the latter stages of the Cultural Revolution, however, it is unclear which significant choices were made by Mao Zedong and carried out by the Gang, and which were the outcome of the Gang of Four's strategy.

Q5. What is the Gang of Four's mediator pattern?

According to the Gang of Four book, the Mediator pattern is defined as "an object that encapsulates how a set of objects interacts." The mediator encourages loose coupling by preventing items from directly referencing each other, and it allows you to control their interaction independently."
Share Article
About Author
Shailendra Chauhan (Microsoft MVP, Founder & CEO at Scholarhat by DotNetTricks)

Shailendra Chauhan is the Founder and CEO at ScholarHat by DotNetTricks which is a brand when it comes to e-Learning. He provides training and consultation over an array of technologies like Cloud, .NET, Angular, React, Node, Microservices, Containers and Mobile Apps development. He has been awarded Microsoft MVP 8th time in a row (2016-2023). He has changed many lives with his writings and unique training programs. He has a number of most sought-after books to his name which has helped job aspirants in cracking tough interviews with ease.
Accept cookies & close this