Gang of Four design patterns are a set of classic software design principles that address common coding problems. Explore the 23 Gang of Four design patterns and how they approach problem-solving in software development.
Gang of Four design patterns is a nickname for the software design patterns outlined in the book Design Patterns: Elements of Reusable Object-Oriented Software, co-authored by Erich Gamma, Richard Helm, Ralph Johnson, and John Vlissides, commonly known as the “Gang of Four.” The 1994 book became a seminal work outlining 23 common software design problems and describing an optimal way to overcome the problem in object-oriented programming. Software developers, software engineers, and other members of a software development team use Gang of Four design patterns as time-tested methods of creating accurate and elegant code.
Discover the 23 design patterns outlined by the Gang of Four as well as when they may be relevant to your development projects.
A software design pattern is a strategy for overcoming a common challenge in software development. Developers and software engineers use design patterns to apply time-tested techniques that make the development process smoother and faster.
Software developers and engineers use various tools to help them create software programs faster, including libraries and frameworks that offer reusable code snippets. By copying and pasting these pieces of code into their work, developers can lean on time-tested solutions to work faster than writing every piece of code by hand.
A design pattern is a similar tool that developers can reach for again and again. Instead of containing prewritten code, a design pattern is a technique, structure, or template for solving a programming problem. For example, subclassing is a design pattern built into Java that allows similar objects to inherit state and behavior from a superclass but modify or override these traits as needed. Instead of offering a copy/paste solution, design patterns describe a way to approach problems.
The Gang of Four design patterns, sometimes called GoF, refer to those published in the 1994 book Design Patterns: Elements of Reusable Object-Oriented Software. The authors, Erich Gamma, Richard Helm, Ralph Johnson, and John Vlissides, were computer scientists and researchers who identified 23 common design problems in object-oriented programming and presented a way of approaching a solution for each. Design Patterns was a popular book and became an influential resource for software development, where it is still considered an important work today. The authors gained the nickname “the Gang of Four” due to how widely considered and referenced their work was.
A lot has changed in software development technology over the last 30 years, but Gang of Four design principles remain relevant today. They form the foundation for other design principles and can still be used in software engineering and architecture.
Design patterns offer software developers the benefit of a proven solution to a common problem that they can use and reuse multiple times. Instead of having to come up with a new and novel solution every time you have a problem, you can use a design pattern as a template, saving you both time and brain power. You can reach for these solutions often, adapting them to the specific circumstances as needed while keeping the main approach to problem-solving the same. Design patterns also allow developers to use a common language to talk about situations within the code and how they implemented code to overcome the problem.
The 23 design patterns found within Design Patterns fall into three categories: creational patterns, structural patterns, and behavioral patterns. Creational design patterns create and initialize objects, structural design patterns create structures of classes and objects, and behavioral design patterns allow objects and structures to communicate with each other and work together.
Creating objects is a common problem in software development, particularly in object-oriented programming. If you have many similar objects, it can be tedious and time-consuming to code each one, and you end up repeating a lot of the same code. Gang of Four design patterns include five creational design patterns: abstract factory, factory, builder, prototype, and singleton.
Factory method: The factory method is a simple way of creating objects within a class that doesn’t require you to specify the subclass for each object.
Abstract factory: This method uses a factory within each class and organizes the group of classes by theme, providing a new interface for creation and avoiding conditional logic.
Builder: The builder design pattern is a method for creating objects with many parameters or both optional and mandatory properties.
Prototype: The prototype design pattern is an efficient way of creating objects that are similar to an existing object.
Singleton: The singleton method ensures that only one class of each kind exists, such as a sitemap of an entire website, so that the class is the same globally.
Once you program all the objects and classes you need, it may be necessary to create larger structures that modify the way those objects and classes behave. In this case, you may reach for a structural design pattern. The Gang of Four outlined seven kinds of structural design patterns: adapter, bridge, composite, decorator, facade, flyweight, and proxy.
Adapter: An adapter allows two unrelated interfaces to work together, similar to how you might use an adapter to charge your phone on the wrong kind of charging cord.
Bridge: A bridge allows you to use an abstraction or hierarchy in an interface independently of its implementation so you can use one without needing to reference the other.
Composite: A composite design pattern allows you to group objects that you want to treat the same into a container called a composite.
Decorator: A decorator design pattern allows you to add behavior to an item without affecting other objects within the class.
Facade: A facade design creates a unifying interface within which you can access a sub-system of interfaces.
Flyweight: Flyweight is a design pattern for creating many fine-grained objects that lighten the memory requirements of the device running the program.
Proxy: A proxy design pattern allows you to control an object through a placeholder or proxy where you can limit access to the object. For example, a version of your product that your client can preview without damaging it.
The third category of design patterns is behavioral patterns, which explain how objects will communicate with each other and which parts of the programming are responsible for every step of a given process. Behavioral patterns allow you to use objects and classes in different ways without writing hard code to modify your data. You can draw from 11 Gang of Four behavioral design patterns: chain of responsibility, command, interpreter, iterator, mediator, memento, observer, state, strategy, the template method, and visitor.
Chain of responsibility: Chain of responsibility is a method of determining which entity will handle any given request based on loose coupling.
Command: The command design pattern is a method of taking a command and all the information the object needs to perform the command and encapsulating it into its own object so you can easily reuse the command in different contexts.
Interpreter: The interpreter design pattern is a method for defining the grammar of a language and constructing sentences using classes to represent grammatical rules.
Iterator: Iterator design patterns allow you to search through and access objects without showing the implementation of the iterator and in a scalable way.
Mediator: A mediator design pattern is a flexible way to allow two objects to have independent actions and still communicate with one another.
Memento: This design method allows you to restore your object to a previous state before a command or other change to its behavior.
Observer: An observer design pattern allows you to create an event or trigger an action when something the object is observing changes, for example, sending out a notification to your followers when you post new content.
State: State design patterns allow you to modify how objects will behave based on their state.
Strategy: This is a method for allowing more than one possible behavior depending on the input.
Template method: The template method allows you to create a template of an operation or function where the subclasses will determine some of the steps.
Visitor: A visitor design pattern allows you to change the behavior of an algorithm temporarily.
The Gang of Four design patterns are classic strategies for creating effective and efficient code. If you want to learn more about software design patterns or explore a career as a software engineer, you can find out more on Coursera. For example, you could enroll in the IBM DevOps and Software Engineering Professional Certificate and earn a Professional Certificate to help you begin a career as a software engineer.
Editorial Team
Coursera’s editorial team is comprised of highly experienced professional editors, writers, and fact...
This content has been made available for informational purposes only. Learners are advised to conduct additional research to ensure that courses and other credentials pursued meet their personal, professional, and financial goals.