Concepts and Types of Design Patterns

What Is a Design Pattern?

A design pattern is an excellent reusable solution for recurring chronic problems that often appear in specific contexts when designing software.
“Don’t reinvent the wheel” means that there is no need to recreate from scratch something that has already been made and works well.

Types of Design Patterns

Known design patterns are divided into 23 patterns as follows. Broadly, they are classified into three types: Creational, Structural, and Behavioral. These are called GoF(Gang of Four) design patterns and were devised by four well-known developers: Erich Gamma, Richard Helm, Ralph Johnson, and John Vissides. These four developers organized “experience” and “internal accumulation” in the form of design patterns. They are called the Gang of Four, or GoF.

Creational Pattern

Creational patterns are related to object creation. They encapsulate object creation and composition, providing flexibility so that even if a specific object is created or changed, the overall program structure is not greatly affected.

  • Abstract Factory Methods: create products by assembling related parts.
  • Factory Methods: delegate instance creation to subclasses.
  • Builder: assemble complex instances.
  • Prototype: create instances by copying.
  • Singleton: only one instance.

Structural Pattern

Structural patterns create larger structures by combining classes or objects. For example, they can wrap two objects with different interfaces to provide a single interface, or combine objects to provide new functionality.

  • Adapter: reuse by wrapping with one layer.
  • Bridge: separate the hierarchy of features from the hierarchy of implementation.
  • Composite: treat containers and contents uniformly.
  • Decorator: treat decorations and contents uniformly.
  • Facade: provide a simple entry point.
  • Flyweight: eliminate waste by sharing identical objects.
  • Proxy: create it when it becomes necessary.

Behavioral Pattern

Behavioral patterns are related to algorithms and responsibility distribution between objects or classes. They focus on how to divide work that one object cannot perform alone among several objects while minimizing coupling between those objects.

  • Chain of Responsibility: pass responsibility along.
  • Command: turn a request into a class.
  • Interpreter: express grammar rules as classes.
  • Iterator: count one by one.
  • Mediator: communicate through a single counselor.
  • Memento: preserve state.
  • Observer: notify changes in state.
  • State: express state as a class.
  • Strategy: replace the whole algorithm.
  • Template Methods: delegate concrete processing to subclasses.
  • Visitor: move through a structure and perform work.

Java Example Source

References