Types of Design Patterns

Learn via video courses
Topics Covered

Overview

Before discussing the types of patterns in design, let's briefly discuss design patterns themselves.

Essentially, software design patterns are solutions of the design level of a software that are created for repetitive problems that software engineers come across often. Design patterns are not code, but instead are like descriptions on tackling certain software problems and designing a solution. Design patterns are, in layman terms, walkthroughs of recurring problems that were worth writing down. They do not suggest a direct solution implementation for the software problem at hand, but are an outline of a structure that could be used as per the needs of the problem.

Our goal will be to comprehend the usage, and the purpose of each of the design patterns and pick the most suitable one and implement it as a solution to the software problem at hand.

Types of Patterns in Design

Types of Patterns in Design Now, there is obviously not one design pattern for every problem. We have 3 broad categories that design patterns are divided into, as you can see in the diagram above - creational patterns, behavioral patterns and structural patterns. Each one of them is best suited for a specific situation. If they're used in the wrong scenario, they may do more harm than good. Let's take a look at the 3 categories of design patterns.

1. Creational patterns

As the name suggests, creational patterns have a lot to do with creation of objects. These patterns are designed for the instantiation of classes. Creating the objects required in the conventional format may result in additional complexity to the software design or even design problems. Creational design patterns solve this problem by controlling the object creation.

When should you use creational patterns for your software design problem? Creational patterns are most useful when instances of multiple different classes are available. To be more precise, if your software leverages polymorphism, and requires to select between various classes at runtime rather than at compile time.

Some of the creational design patterns are - Abstract Factory, Builder, Factory Method, Object Pool, Prototype, and Singleton.

  1. Abstract Factory: Instantiates various families of classes
  2. Builder: Separates the object construction from it's representations
  3. Factory Method: Instantiates different derived classes
  4. Object Pool: Used when initialization cost of class is high
  5. Prototype: Used when time taken to create object is large and costly. Objects are created with existing object itself.
  6. Singleton: Useful when coordination of actions across the system can be done by exactly one object.

Let's take an example of the singleton design pattern.

example of the singleton design pattern Say you're a developer who wants to create a basic DBConnection class that aids connection to the database. You want to access the DB at various points in your code. Your first thought would be to create an instance of the DBConnection class and make use of it for all the operations wherever required. Due to this, multiple connections to the database are created since every instance of the class will have a different connection to your database. To reduce these connections, we're going to make use of the singleton pattern -- we create the DBConnection class as a singleton class. This way, only one single connection to the database is established, thereby avoiding too many connections. Here's the most classic implementation of the Singleton Design Pattern:

In this example, get_instance() has been declared as static so that it can be called without the instantiation of the class. The first time that get_instance() is called, a singleton object is created, but every other time that it is called, it returns the same object.

2. Behavioral patterns

Behavioral patterns are those design patterns that describe and identify the interactions between different objects. Making use of behavioral design patterns can reduce complicated flowcharts of the software design into simpler interconnections between various objects of classes.

Some of the behavioral patterns are:

  1. Chain of responsibility: An approach to passing a request between a chain of objects.
  2. Command: Data driven behavioral pattern, which encapculates all the information that is required for performing a "command" or action, or to trigger an event at a later time.
  3. Interpreter: Using this design pattern we can incorporate language elements in programs.
  4. Iterator: This pattern is utilized to get a method for getting to the components of a collection object in a sequential way with no requirement to know its fundamental representation.
  5. Mediator: Simplifies the communication between objects.
  6. Memento: Restores and captures objects internal states.
  7. Null Object: This design pattern is to create a null object that encapsulates the absence of an object.
  8. Observer: This design pattern acts as a notifier of change to multiple classes.
  9. State: The state design pattern is used when objects change their behavior based on their internal states.
  10. Strategy: Strategy design pattern is also known as the policy pattern, that helps selecting algorithms at runtime.
  11. Template Method: Template method, as the name suggests, defines the framework of an algorithm in the superclass, however it lets the subclasses override certain specific steps in the algorithm without making a change to the fundamental structure.
  12. Visitor: This design pattern, separates an algorithm from the object structure on which it operates. It essentially results in the addition of new operations to the existing classes without any changes.

Let's take an example of the "Command" design pattern. Once you're done eating at a dinner and you pay. The order that you place at the dinner is an example of the command design pattern. How? Once you have placed an order, Your waiter or waitress takes on order, or a "command" you could say from you. Post that, they "encapsulate" that order by writing it on the check. Your order gets queued to the cook. This "command" chain, as you can see in the diagram below is an example of the command design pattern.

example of the Command design pattern This diagram above explains the command design pattern.

3. Structural patterns

Structural patterns are useful in creating larger structures from smaller, individual parts that are in general, of different classes. These patterns can also be used to ease the design by recognizing a basic method for recognizing relationships among entities. The main idea behind using structural patterns is to increase the functionality of the classes involved in the software without making many changes to its fundamental composition.

The types of structural patterns are:

  1. Adapter: Useful in matching the interfaces of various classes
  2. Bridge: Bridge design pattern is used to create a separation between the interface and its implementation.
  3. Composite: Helpful in creating a proper tree like structure of simple and composite objects. This helps as you can represent the software design in hierarchies and each node in this tree structure can be designed to perform a specific task.
  4. Decorator: This pattern helps in the addition of responsibility to objects dynamically.
  5. Facade: The facade design pattern maintains a cover by providing an interface to the client, while hiding the complexities of the system.
  6. Flyweight: This pattern helps in saving memory (essentially) by sharing the properties of objects.
  7. Private Class Data: Restricts the mutator or accessor access.
  8. Proxy: This design pattern has objects representing other objects, like one student giving attendance for another student in college.

Moving on to a very basic example of the structural design pattern, we have arithmetic expressions. They are composite design patterns. How? Arithmetic expressions are composed of operands and operators (- + * /). This operand can be either just a number, or it can also be another arithmetic expression. We can say that (1 + 8) * (6 / 2) is an arithmetic expression which can be represented using a tree like structure. In the diagram below, we can see that some of the nodes are arithmetic operators and perform tasks such as addition, multiplication and division. This diagram also shows the basic hierarchy of an arithmetic expression.

example of the structural design pattern You now know the basic types of design patterns, and further, you'll learn about each of them in detail.

Summary

  • Design patterns are solutions of design level created for repetitive problems faces by software engineers.They are not code, but descriptions of solutions to certain problems.
  • There are 3 types of design patterns:
    • Creational
    • Structural
    • Behavioral

Transform your design aspirations into reality with our Instagram System Design course, a stepping stone toward greatness.