Gang of Four (GoF) Java Design Patterns

What is a Design Pattern?

Basically, a design pattern represents the best practices used by the most experienced object-oriented software developers, as these are the solutions to the most general problems that any Software developer might have faced during the process of software development.

A group of 4 authors named Erich Gamma, John Vlissides, Ralph Johnson and Richard Helm published a book titled “Design Patterns – Elements of Reusable Objected Oriented Software” in the year 1994. This has been the initiation of the concept of Design patterns in the Software development.

These authors are commonly known to be as the infamous “Gang of Four” then onwards and hence the name – “Gang of Four” Java Design Patterns. The two important points that most of the design patterns talk about are:

  • Program to an interface but not to an implementation
  • Favor Object Composition as against Inheritance

With the discussion above giving us the background of the Design Patterns as a whole and the most commonly used phrase “Gang of Four” demystified, we will go to the next section of the topic – the types of design patterns available.

 Types of Design Patterns:

As per the references given in the book Design Patterns – Elements of Reusable Object-Oriented Software, there are 23 design patterns that can be broadly divided into 3 categories.

Creational Pattern:

These kinds of patterns generally provide ways and means of creating Objects in the best possible way, instead of using the traditional new operator. These patterns give the flexibility in making the decision to create what objects based on the given use case.

Structural Pattern:

These design patterns generally talk about the Class and the Object compositions. Inheritance as a concept is used to compose the interfaces and also to define ways to compose objects to obtain newer functionalities.

Behavioral Pattern:

These design patterns are generally used for identifying the best possible way of communication within the objects.

There is also relatively a newer set of patterns discussed as the J2EE patterns identified by the Sun Java Center. These are specifically concerned with the Presentation layer.

23 GoF Design Patterns, their definitions:

The points above should provide you the basic knowledge about the concept of Design patterns and also the kinds of patterns that are available for your consumption directly. Now with these details, we shall directly dive into the details and specifications of these design patterns individually.

1. Mediator design pattern:

Use Mediator pattern to “Define an object that encapsulates how a set of objects interact. Mediator design pattern promotes loose coupling by keeping objects from referring to each other explicitly, and it lets you vary their interaction independently.”

2. Adapter design pattern:

Adapter design pattern lets you “Convert the interface of a class into another interface the client expects. Adapter lets classes work together that couldn’t otherwise because of incompatible interfaces.”

3. Proxy design pattern:

Use Proxy design pattern to “Provide a surrogate or placeholder for another object to control access to it. Ex: Usage of Proxy design pattern can be seen in RMI (Remote Method Invocation) APIs.

4. Observer design pattern:

Observer design pattern should “Define a one-to-many dependency between objects in that when one object changes state, all its dependents are notified and updated automatically.”

5. Strategy design pattern:

Strategy design pattern should “Define a family of algorithms, encapsulate each one, and make them interchangeable. Strategy lets the algorithm vary independently from clients that use it.”

6. Decorator design pattern:

Decorator design pattern should “Attach additional responsibilities to an object dynamically. Decorators provide a flexible alternative to sub-classing for extending functionality.”

7. Factory design pattern:

Factory design pattern should “Define an interface for creating an object, but let subclasses decide which class to instantiate. Factory method lets a class defer instantiation to subclasses.”

8. Chain of Responsibility design pattern:

Chain of Responsibility design pattern should “Avoid coupling the sender of a request to its receiver by giving more than one object a chance to handle the request. Chain the receiving objects and pass the request along with the chain until an object handles it.”

9. Singleton design pattern:

Singleton design pattern must “Ensure a class only has one instance, and provide a global point of access to it.”

10. Flyweight design pattern:

Flyweight design pattern should “Use sharing to support large numbers of fine-grained objects efficiently. A Flyweight is a shared object that can be used in multiple contexts simultaneously. The Flyweight acts as an independent object in each context – it’s indistinguishable from an instance of the object that’s not shared.”

11. Façade design pattern:

Façade design pattern should “Provide a unified interface to a set of interfaces in a system. Façade defines a higher-level interface that makes the subsystem easier to use.”

12. Template Method design pattern:

Template Method design pattern will “Define the skeleton of an algorithm in an operation, differing some steps to subclasses. Template method lets subclasses redefine certain steps of an algorithm without changing the algorithm’s structure.”

13. Builder design pattern:

Builder design pattern lets you “Separate the construction of a complex object from its representation so that the same construction process can create different representations.”

14. Iterator design pattern:

You can use the Iterator design pattern to “Provide a way to access the elements of an aggregate object sequentially without exposing its underlying representation.”

15. Composite design pattern:

Use Composite design pattern to “Compose objects into tree structures to represent part-whole hierarchies. Composite lets clients treat individual objects and compositions of objects uniformly.”

16. State design pattern:

State design pattern will “Allow an object to alter its behavior when its internal state is changed. The object will appear to change its class.”

17. Command design pattern:

Use Command design pattern to “Encapsulate a request as an object, thereby letting you parameterize clients without different request, queue or log requests, and support undoable operations.”

18. Abstract Factory design pattern:

Abstract Factory design pattern should “Provide an interface for creating families of related or dependent objects without specifying their concrete classes.”

19. Prototype design pattern:

Prototype design pattern should “Specify the kinds of objects to create using a prototypical instance, and create new objects by copying this prototype.”

20. Interpreter design pattern:

Interpreter design pattern should “Given a language, define a representation for its grammar along with an interpreter that uses the representation to interpret sentences in the language.”

21. Memento design pattern:

Memento design pattern is designed to “Without violating encapsulation, capture and externalize an object’s internal state so that the object can be restored to this state later.”

22. Visitor design pattern:

Visitor design pattern should “Represent an operation to be performed on the elements of an object structure. Visitor lets you define a new operation without changing the classes of the elements on which it operates.”

23. Bridge design pattern:

Bridge design pattern should “Decouple an abstraction from its implementation so that the two can vary independently.”

Source: “Design Patterns – Elements of Reusable Objected Oriented Software. For more information on these topics, we would suggest going through the above book to get familiarized with the topics.

Article Name
Gang of Four (GoF) Java Design Patterns
What is a Design Pattern? Basically, a design pattern represents the best practices used by the most experienced object-oriented software developers, as these are the solutions to the most general problems that any Software developer might have faced during the process of software development.
Publisher Name
Publisher Logo

3 replies on “Gang of Four (GoF) Java Design Patterns”

In 1994, four authors Erich Gamma, Richard Helm, Ralph Johnson and John Vlissides published a book titled Design Patterns – Elements of Reusable Object-Oriented Software which initiated the concept of Design Pattern in Software development.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.