Skip to content

Design Patterns

Design Patterns refer to reusable code design experiences that are repeatedly used in software design. The purpose of using design patterns is to create reusable code, enhancing the scalability and maintainability of the code.

The term "Design Patterns" was coined and refined in the 1990s by four individuals: Erich Gamma, Richard Helm, Ralph Johnson, and John Vlissides. They also authored a book titled Design Patterns. These four are also known as the Gang of Four (GoF).

Why Use Design Patterns?

The fundamental reason for using design patterns is to achieve maintainable and scalable software development. This requires maximizing code reuse and reducing code coupling. Design patterns are primarily distilled from Object-Oriented Programming (OOP) and are based on the following principles:

Open Closed Principle

Proposed by Bertrand Meyer, the Open Closed Principle states that software should be open for extension but closed for modification. This means that when adding new features, you should avoid changing existing code as much as possible. Ideally, new functionality should be achieved by adding new code rather than altering existing code.

Liskov Substitution Principle

Introduced by Barbara Liskov, the Liskov Substitution Principle is an object-oriented design principle stating that if a method in a parent class works correctly, then replacing it with a method in a subclass should work just as well without any issues.

Design patterns extract common design ideas into individual patterns and name each pattern, making communication easier when using them. The GoF categorized 23 common patterns into three types: Creational Patterns, Structural Patterns, and Behavioral Patterns, which we will discuss one by one in subsequent sections.

Learning Design Patterns

The key to learning design patterns is understanding the design philosophy behind them. It is important not to rigidly apply patterns without consideration or to over-engineer solutions just to use a design pattern. Striking a reasonable balance between design complexity and flexibility is essential, and it’s important to recognize that design patterns are not a one-size-fits-all solution.

Creational Patterns

Creational patterns focus on how to create objects. Their core idea is to separate the creation of objects from their usage, allowing the two to vary independently.

Creational patterns include:

  • Factory Method
  • Abstract Factory
  • Builder
  • Prototype
  • Singleton

Structural Patterns

Structural patterns primarily deal with how to compose various objects to obtain a more efficient and flexible structure. While object-oriented inheritance provides the basic functionality of subclass extending superclass, structural patterns go beyond simple inheritance. They achieve more flexible functionality through composition and dynamic combination at runtime.

Structural patterns include:

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

Behavioral Patterns

Behavioral patterns primarily involve the distribution of responsibilities among objects and the algorithms that define their interactions. By using object composition, behavioral patterns can describe how a group of objects should cooperate to accomplish a complete task.

Behavioral patterns include:

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

Summary

Design Patterns provide a standardized approach to solving common software design problems. By understanding and applying these patterns, developers can create more maintainable, scalable, and flexible code. It's essential to grasp the underlying design principles and apply patterns judiciously to balance complexity and flexibility in software development.

Design Patterns has loaded