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.

CLICK HERE to download the below table as a PDF file to use as a quick handy reference offlineCLICK to Download Design Patterns Quick Reference as a PDF

Creational Design Patterns
Pattern Name Tutorial Link Pattern Description
Abstract Factory Provides an interface for creating families of related or dependent objects without specifying their concrete classes.
Factory Method read tutorialClick to Read Factory Method Design Pattern Tutorial Deals with the problem of creating related objects without specifying the exact class of object that will be created.
Singleton This pattern ensures a class has only one instance and provides a global(app-level) point of access to it.
Prototype read tutorialClick to Read Prototype Design Pattern Tutorial Specify the kinds of objects to create using a prototypical instance, and create new objects by copying this prototype.
Builder read tutorialRead Builder Pattern Tutorial Separates 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
Adapter read tutorialAdapter Design Pattern tutorial This pattern lets classes work together that could not otherwise because of incompatible interfaces.
Bridge This pattern decouples an abstraction from its implementation so that they become loosely coupled.
Composite read tutorialLink to Composite Design Pattern Tutorial This pattern allows aggregating objects such that individual objects and composition of objects can be treated uniformly.
Decorator This pattern attaches additional responsibilities to an object dynamically while keeping the interface same
Facade read tutorialLink to Facade Design Pattern Tutorial This pattern provides a simpler interface to a larger and more complex system such as a class library or a complex API.
Flyweight This pattern minimizes memory usage by sharing common data between objects.
Proxy read tutorial Click to Read to Proxy Design Pattern Tutorial Proxy 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 Responsibility read tutorialRead Chain of Responsibility Pattern Tutorial This 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.
Command In this pattern an object is used to represent and encapsulate all the information needed to call a method at a later time.
Interpreter This pattern defines a representation for a given language’s grammar along with an interpreter that uses the representation to interpret sentences in the language.
Iterator read tutorialRead Iterator Pattern Tutorial This pattern provides a way to access the elements of an aggregate object sequentially without exposing its underlying representation.
Mediator In 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.
Memento read tutorialLink to Memento Design Pattern Tutorial Mementos capture and externalize an object’s internal state allowing the object to be restored to this state later.
Observer read tutorialClick to Read Observer Design Pattern Tutorial An observable object called ‘Subject’ maintains a list of objects called ‘Observers’. Subject notifies the observers of any state changes.
State read tutorialClick to Read State Design Pattern Tutorial State pattern allows an object to alter its behavior when its internal state changes.
Strategy read tutorialStrategy Design Pattern Tutorial This pattern defines a family of algorithms, encapsulate each one, and make them interchangeable.
Template Method read tutorialLink to tutorial on Template Method Pattern Pattern defines steps of an algorithm as methods in an interface while allowing subclasses to override some steps in the overall algorithm.
Visitor read tutorialLink to Visitor Design Pattern Tutorial Pattern 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.

Digiprove sealCopyright © 2014-2017 JavaBrahman.com, all rights reserved.
  • Comment Avoir Une Belle Peau

    This site was… how do you say it? Relevant!! Finally I
    have found something which helped me. Appreciate it!

  • Edwin

    Hi, I check your blog regularly. Yoour writinbg style is awesome, keep uup the goo work!

  • java brahman

    Hi, Thanks for the appreciation. I do have a facebook page for my blog. Do “Like” it to get regular updates as and when I post new articles… https://www.facebook.com/javabrahman

  • warez community

    Way cool! Somme very valid points! I appreciate you writing this article and the rest of
    the webgsite is als very good.