Introduction to Java Design Patterns

Java Design Patterns: Overview

While talking of design patterns there are two things we take into account. First, design and second, pattern. For every problem there should be a feasible design and if the problem occurs quite frequently then we create a reusable solution to that problem. This reusable solution is called design pattern. A design is considered to be good if it helps in developing a system that is behaviorally, economically, and socially feasible and can accommodate changes with minimal effort. There could be more than one good design for a particular system and we can pick a good design and create a pattern which solves a particular type of problem. That pattern will describe the problem which it was created for and then it can be used over and over again whenever the problem occurs.

Formally the word pattern was used by Christopher Alexander (a building architect), who defined a pattern as "a solution to a problem in a context." Alexander says that a description of a pattern involves four items:

  • The name of the pattern.
  • The purpose of the pattern, the problem it solves.
  • How we could accomplish this?
  • The constraints and forces we have to consider in order to accomplish it.

Christopher Alexander further postulated that patterns could be used together to solve complex architectural problems.

However, Christopher talks of architectural patterns but patterns occur in software design too. Gamma, Helm, Johnson, and Vlissides in their classical book "Design Patterns: Elements of Reusable Object-Oriented Software" identified several patterns and developed standards for cataloguing new ones. Note that the authors did not create the patterns described in the book. Rather, they identified these patterns as already existing within the software community, patterns that reflected what had been learned about high-quality designs for specific problems.

Key Features of a Design Pattern

To describe a design pattern there could be several different forms. We include the following items in pattern description. These items we are calling key features of a design pattern.

Item Description
Name Every pattern has a unique name that identifies that pattern.
Purpose The problem that the pattern is trying to solve.
Solution How the pattern provides a solution to the problem in the context in which it shows up.
Constraints The constraints and forces we have to consider in order to accomplish it.

While explaining design patterns one by one we will include above key features in every design pattern's description.

Types of Design Patterns

Erich Gamma et al. in their book 'Elements of Reusable Object-Oriented Software' on design patterns classified design patterns in three categories - creational, structural, and behavioral.

Creational Patterns

Creational patterns address the question "how an object should be created so that it does not add the complexity to new object creation?" Creational patterns isolate the details of object creation. This pattern is useful when there are yet-to-be known classes in an application. Following design patterns fall under creational patterns:

  • Factory Method
  • Abstract Factory Pattern
  • Builder Pattern
  • Object Pool Pattern
  • Prototype Pattern
  • Singleton Pattern

Structural Patterns

Structural patterns are useful when classes and objects are composed to form larger structures. These patterns use inheritance to compose interfaces or implementations. For example, consider how multiple inheritance mixes two or more classes into one. The result is a class that combines the properties of its parent classes. This pattern is particularly useful for making independently developed class libraries work together. Following design patterns fall under structural patterns:

  • Adapter Pattern
  • Bridge Pattern
  • Composite Pattern
  • Decorator Pattern
  • Facade Pattern
  • Flyweight Pattern
  • Proxy Pattern

Behavioral Patterns

Behavioral patterns are used for assigning responsibilities between objects. Behavioral patterns describe how do objects interact with each othe?. They focus on how objects communicate with each other. Behavioral class patterns use inheritance to distribute behavior between classes and behavioral object patterns use object to assign the responsibilities between objects. Following design patterns fall under behavioral patterns:

  • Chain of Responsibility Pattern
  • Command Pattern
  • Interpreter Pattern
  • Iterator Pattern
  • Mediator Pattern
  • Memento Pattern
  • Observer Pattern
  • State Pattern
  • Strategy Pattern
  • Template Pattern
  • Visitor Pattern

Last Word

In this tutorial we presented a quick overview of Java design patterns. We will discuss all design patterns one by one in individual tutorials. Hope you have enjoyed reading this tutorial, please do write us if you have any suggestion/comment or come across any error on this page. Thanks for reading!

References

  1. Design Patterns: Elements of Reusable Object-Oriented Software


Share this page on WhatsApp

Get Free Tutorials by Email

About the Author

is the founder and main contributor for cs-fundamentals.com. He is a software professional (post graduated from BITS-Pilani) and loves writing technical articles on programming and data structures.