JB Header
GOF / Gang of Four Design Patterns in Java
This tutorial provides an overview of GOF/Gang of Four Design Patterns. It starts with the basic definition of a design pattern. Next it introduces Gang of Four's design patterns, followed by the three categories of GOF Design Patterns. It then lists out all the GOF design patterns category-wise, provides a brief description of each pattern, and a link to the detailed tutorial for each of the design patterns' implementation and usage in Java.
What is a design pattern A design pattern is a general reusable solution to a commonly occurring problem in software design. It is a template for how to solve a problem that can be used in many different situations. Patterns are formalized best practices that the programmer can use to solve common problems when designing an application or system. Gang of Four Design Patterns These are design patterns which were defined by four authors - Erich Gamma, Richard Helm, Ralph Johnson and John Vlissides in their book Design Patterns: Elements of Reusable Object-Oriented Software. A lot has evolved in the field of software design since this book came out in 1994. This book and its patterns however make the foundation of the field of object oriented design patterns. Categories of GOF Design Patterns Gang Of Four design patterns are grouped into 3 categories:
  1. Creational Design Patterns: Creational patterns deal with object creation i.e they look at ways to solve design issues arising out of creation of objects.
  2. Structural Design Patterns: Structural patterns ease the design by identifying a simple way to realize relationships between entities.
  3. Behavioral Design Patterns: Behavioral patterns identify common communication patterns between objects and realize these patterns.
Having looked at the 3 categories of GOF design patterns, now let us take a look at the individual patterns in each of these categories.
Creational Design Patterns
Pattern Name Tutorial Link Pattern Description
Abstract FactoryProvides an interface for creating families of related or dependent objects without specifying their concrete classes.
Factory Methodread tutorialClick to Read Factory Method Design Pattern TutorialDeals with the problem of creating related objects without specifying the exact class of object that will be created.
SingletonThis pattern ensures a class has only one instance and provides a global(app-level) point of access to it.
Prototyperead tutorialClick to Read Prototype Design Pattern TutorialSpecify the kinds of objects to create using a prototypical instance, and create new objects by copying this prototype.
Builderread tutorialRead Builder Pattern TutorialSeparates the construction of a complex object from its representation, thus enabling the same construction process to create various representations.
Structural Design Patterns
Pattern Name Tutorial Link Pattern Description
Adapterread tutorialAdapter Design Pattern tutorialThis pattern lets classes work together that could not otherwise because of incompatible interfaces.
BridgeThis pattern decouples an abstraction from its implementation so that they become loosely coupled.
Compositeread tutorialLink to Composite Design Pattern TutorialThis pattern allows aggregating objects such that individual objects and composition of objects can be treated uniformly.
DecoratorThis pattern attaches additional responsibilities to an object dynamically while keeping the interface same
Facaderead tutorialLink to Facade Design Pattern TutorialThis pattern provides a simpler interface to a larger and more complex system such as a class library or a complex API.
FlyweightThis pattern minimizes memory usage by sharing common data between objects.
Proxyread tutorial Click to Read to Proxy Design Pattern TutorialProxy is a surrogate or placeholder class for another class mostly done with an intention of intercepting access to the said class.
Behavioral Design Patterns
Pattern Name Tutorial Link Pattern Description
Chain of Responsibilityread tutorialRead Chain of Responsibility Pattern TutorialThis pattern defines a chain of processing objects in a chain in such a way that the incoming request is processed by each processing objects in sequence.
CommandIn this pattern an object is used to represent and encapsulate all the information needed to call a method at a later time.
InterpreterThis pattern defines a representation for a given language's grammar along with an interpreter that uses the representation to interpret sentences in the language.
Iteratorread tutorialRead Iterator Pattern TutorialThis pattern provides a way to access the elements of an aggregate object sequentially without exposing its underlying representation.
MediatorIn this pattern communication between objects is encapsulated with a mediator object. Objects no longer communicate directly with each other, but instead communicate through the mediator.
Mementoread tutorialLink to Memento Design Pattern TutorialMementos capture and externalize an object's internal state allowing the object to be restored to this state later.
Observerread tutorialClick to Read Observer Design Pattern TutorialAn observable object called 'Subject' maintains a list of objects called 'Observers'. Subject notifies the observers of any state changes.
Stateread tutorialClick to Read State Design Pattern TutorialState pattern allows an object to alter its behavior when its internal state changes.
Strategyread tutorialStrategy Design Pattern TutorialThis pattern defines a family of algorithms, encapsulate each one, and make them interchangeable.
Template Methodread tutorialLink to tutorial on Template Method PatternPattern defines steps of an algorithm as methods in an interface while allowing subclasses to override some steps in the overall algorithm.
Visitorread tutorialLink to Visitor Design Pattern TutorialPattern separates an algorithm from the object structure on which it operates, which provides the ability to add new operations to existing object structures without modifying those structures.
Summary In the above tutorial we understood the Gang of Four design patterns, their categories and the individual patterns making up each of the categories. The next logical step will be start going through the individual design patterns' tutorials and understanding their working in-depth. Please use the 'read tutorial' links in the patterns' tables to go to each individual design pattern.