Design Patterns

The Design Patterns originate from the all-time classic book Design Patterns: Elements of Reusable Object-Oriented Software written by the notorious Gang of Four, a team of four computer scientists, in 1994. The impact of their work cannot be overstated, as till this day, there is much to learn from them and much to be actively applied.

What is a Design Pattern?

A Design Pattern describes a problem which reoccurs in a Software Development process and then describes the core of the solution to that problem. The solution is described in a way that is possible to use it over and over again and in different ways. In simple terms, we can say that a Design Pattern is a tested optimal solution for a design problem that developers regularly stumble upon.

Describing a Design Pattern

Four essential elements can be assigned to a Pattern:

The Design Pattern Name is the name of the pattern (obviously). We can say it’s a handle we can use to describe a design problem and its solution. Imagine the time before the Gang of Four presented the 23 common Design Patterns and named them. I guess that even if someone had already used one of these patterns, without a name for it, soon it would be forgotten.

The Problem describes when to use the Pattern.  Either specific design problems, or class or object structures that are known for their inflexible design. As a young developer, this is a significant point to focus. Through experience, you will start recognizing these reoccurring problems easier as you progress.

The Solution defines all the elements that make up the pattern. The classes, objects, and structures as well their relationships and responsibilities. The solution is not a specific implementation but more of a template that can be applied on different occasions. Depending on the situation and the environment, you should be able to recognize the design problem and apply the solution by implementing the pattern.

The Outcomes are the pros and cons of applying the pattern. And yes, there are cons too. Though consequences are often neglected and not mentioned, they are crucial for the evaluation of design alternatives. Space and time trade-offs, flexibility, extensibility and maintainability are some of the results that must be assessed.

Purpose

The Design Patterns are classified according to their purpose, into creationalstructural, and behavioral patterns. Creational patterns deal with the mechanisms of object creation. Structural patterns concern the composition of classes or objects. Behavioral patterns describe how classes or objects interact and distribute responsibility.

Creational

  • Singleton – Ensure a class only has one instance, and provide a global point of access to it.
  • Abstract Factory – Provide an interface for creating families of related or dependent objects without specifying their concrete classes.
  • Factory Method – Define an interface for creating an object, but let subclasses decide which class to instantiate. Factory Method lets a class defer instantiation to subclasses.
  • Builder – Separate the construction of a complex object from its representation
    so that the same construction process can create different representations.
  • Prototype – Specify the kinds of objects to create using a prototypical instance,
    and create new objects by copying this prototype.

Structural

  • Adapter – Convert the interface of a class into another interface clients expect. Adapter lets classes work together that couldn’t otherwise because of incompatible interfaces.
  • Bridge – Decouple an abstraction from its implementation so that the two can vary independently.
  • Composite – Compose objects into tree structures to represent part-whole
    hierarchies. Composite lets clients treat individual objects and
    compositions of objects uniformly.
  • Decorator – Attach additional responsibilities to an object dynamically. Decorators provide a flexible alternative to subclassing for extending functionality.
  • Facade – Provide a unified interface to a set of interfaces in a subsystem. Facade defines a higher-level interface that makes the subsystem easier to use.
  • Flyweight – Use sharing to support large numbers of fine-grained objects
    efficiently.
  • Proxy – Provide a surrogate or placeholder for another object to control access to it.

Behavioral

  • Chain of Responsibility – 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 the chain until an object handles it.
  • Command – Encapsulate a request as an object, thereby letting you parameterize clients with different requests, queue or log requests, and support undoable operations.
  • Interpreter – Given a language, define a representation for its grammar along with an interpreter that uses the representation to interpret sentences in the language.
  • Iterator – Provide a way to access the elements of an aggregate object sequentially without exposing its underlying representation.
  • Mediator – Define an object that encapsulates how a set of objects interact.
    Mediator promotes loose coupling by keeping objects from referring to each
    other explicitly, and it lets you vary their interaction independently.
  • Memento – Without violating encapsulation, capture and externalize an object’s internal state so that the object can be restored to this state later.
  • Observer – Define a one-to-many dependency between objects so that when one object changes state, all its dependents are notified and updated automatically.
  • State – Allow an object to alter its behavior when its internal state changes.
    The object will appear to change its class.
  • Strategy – Define a family of algorithms, encapsulate each one, and make them interchangeable. Strategy lets the algorithm vary independently from
    clients that use it.
  • Template Method – Define the skeleton of an algorithm in an operation, deferring some steps to subclasses. Template Method lets subclasses redefine certain steps of an algorithm without changing the algorithm’s structure.
  • Visitor – 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.

 

2 comments

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.