Design Patterns in Java

In developing a system, it is expected that some requirements are guaranteed, for example, performance, robustness, understanding, ease of reuse, modification, and use. The Design Patterns were created by the architect Christopher Alexander , in the 1970s. During this period the architect wrote two books: “Pattern Language” and “Timeless Way of Building“. These books were an example of how Christopher thought in order to document these patterns.

In 1995, the professionals: Erich Gamma, Richard Helm, Ralph Johnson and John Vlissides , wrote and released the book called “Design Patterns: Elements of Reusable Object-Oriented Software”, which shows the details of the 23 Design Patterns. For this achievement, the professionals were baptized with the name Gang of Four” (Gang of Four or GoF).

The goal of design patterns are:

  • Reusable components that make it easy to standardize.
  • Enabling agile solutions for recurring problems in developing the system.

There are two design patterns known to software engineering. Those are:

  • Standard GoF (Gang of Four) patterns
  • GRASP (General Responsibility Assignment Software Patterns).

GRASP Patterns

These patterns consists of guidelines for assigning responsibility to classes and objects in object-oriented design. The different patterns and principles and standards used in GRASP patterns are:

  • Specialist in information
  • Creator
  • Controller
  • Weak coupling
  • High Cohesion
  • Polymorphism
  • Pure Fabrication
  • Indirection
  • Protected Variations

GoF patterns

These patterns consist of three groups as shown below:

  • Creational Patterns : Factory Method, Abstract Factory, Singleton, Builder, Prototype.
  • Structural Patterns : Adapter, Bridge, Composite, Decorator, Facade, Flyweight, Proxy.
  • Behavioral Patterns : Chain of Responsibility, Command, Interpreter, Iterator, Mediator, Memento, Observer, State, Strategy, Template, Method, Visitor.

GoF Patterns of Creation

The patterns of this type require a treatment of how objects (classes) are created to meet the diverse needs. In Java, objects are instantiated through their builders, but the use of them is limited when:

  • The code that references the creation of an object needs to know the builders of it, it increases the coupling of classes.
  • The object can not be created partially.
  • The builder can not control the number of instances in the present application.

Illustrative pattern

Figure 1 : Illustration of the figurative Pattern GoF Creation

Factory Method : This standard defines an interface for creating an object, letting the subclasses remain responsible for deciding which class to instantiate.

Abstract Factory : Allows you to draw an interface for creating families of related objects or interdependent, not specifying their concrete classes. These standards can be created using concrete factories, which are responsible for the creation of new objects to meet customer needs. Therefore, this practice helps to exclude the dependency between the client and the class of objects used by the client.

Singleton : Used when desired, that a class has only one instance of the application. Below are some aspects that should be taken care of when creating this pattern:

  • The class is final, it does not allow the creation of subclasses of the class itself.
  • Access is granted through the method that returns a single instance of the class, or is creating one, if it has not been created.

Builder : Provides a generic interface for the incremental construction of aggregations. This pattern hides the details of how the components are created, composed and represented.

Prototype : Defines the types of objects to be created from an instance that serves as a prototype. New objects are created based on this prototype.

GoF Structural Patterns

This standard describes the following aspects: preparation, organization and association between objects and classes / interfaces. Allows to combine objects into more complex structures, or describe how the classes are inherited or composed from other.


Figure 2 : Illustration GoF Structural Pattern

Adapter : The action of this pattern converts the interface of a class into another, expected by the client object. Through this conversion, allows classes with incompatible interfaces, able to be adapted so that other objects can work together.

Bridge : This pattern separates an abstraction from its implementation, allowing both to work independently, and establishes a bridge between them.

Composite : Consisting of objects aggregation trees. Aggregate objects are treated as a single object.

Decorator : This standard seeks to offer a flexible alternative to extension object dynamic new features, without the use of inheritance. An example is the use of the scroll bar that can be incorporated dynamically to a window, text box or web page.

Facade : Provides a unified interface to a set of objects that consists of a subsystem, defining a high-level interface that facilitates the use.

Flyweight : Use sharing to support efficiently to a large number of objects with high level of granularity. This pattern creates models for each object, which concentrates all the common features of an object.

Proxy : Allows access is controlled by another object, which acts as a substitute. Generally used in aspect-oriented programming, aiming to help sort, wrap, modularizing methods and organize code according to importance.

GoF Behavioral Patterns

These patterns show the process of how objects or classes communicate. In general, look for a loose coupling between objects, although the communication that exists between them.


Figure 3 : Illustration of GoF Behavioral Pattern

Chain of Responsibility : This design pattern consists of a source of command objects and a series of processing objects. Each processing object contains logic that defines the types of command objects that it can handle; the rest are passed to the next processing object in the chain. A mechanism also exists for adding new processing objects to the end of this chain.

Command : encapsulates a message or request as an object, so that it can set parameters with different messaging clients.

Interpreter : Are representations and abstractions for grammars for parsing, being used for more language definition.

Iterator : Used to provide a way to access elements of a collection of objects sequentially without exposing the internal structures.

Mediator : This pattern defines an object that encapsulates how a set of objects interact. This pattern is considered to be a behavioral pattern due to the way it can alter the program’s running behavior.

Memento : Works as a “snapshot”, it captures and externalizes the internal state of an object without violating encapsulation, allowing the object can be restored to this state in the future. One example is operations that need to be reversed, so that the previous state of the object is then restored.

Observer : Used to synchronize, coordinate and maintain consistency between related objects which have one to many dependency between objects, ie, when the state of an object changes, all dependents are notified and updated automatically.

State : This patter encapsulates varying behavior for the same routine based on an object’s state object. This can be a cleaner way for an object to change its behavior at runtime without resorting to large monolithic conditional statements.

Strategy : Defines a new set of algorithms without changing the classes of the elements on which it operates.

Template Method : This pattern defines the program skeleton of an algorithm in a method, called template method, which defers some steps to subclasses. It lets one redefine certain steps of an algorithm without changing the algorithm’s structure.

Visitor : It is a way of separating an algorithm from an object structure on which it operates. A practical result of this separation is the ability to add new operations to existing object structures without modifying those structures. It is one way to follow the open/closed principle.


In this article, we saw types of design patterns GoF patterns and GRASP. We also saw further categorization in each of these patterns. This article aims at giving a basic knowledge of design patterns. With this basic knowledge you can proceed to understand each of these patterns better and implement in your day to day software applications.

Leave a Reply

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

Pin It on Pinterest

Share This

Share this post with your friends!