Which are different types of Design Patterns?

Free Coding Questions Catalog
Boost your coding skills with our essential coding questions catalog. Take a step towards a better tech career now!

Design patterns are categorized based on their purpose and the types of design challenges they address. The three main categories are Creational Patterns, Structural Patterns, and Behavioral Patterns. Each category serves a unique role in solving recurring software design problems, and within each category, there are specific patterns that help tackle various design needs.

1. Creational Patterns

Creational patterns deal with the creation of objects. They provide mechanisms to create objects in a way that enhances flexibility and reuse by controlling which objects are created, how they’re created, and how many are created.

Key Creational Patterns:

  • Singleton: Ensures a class has only one instance and provides a global point of access to that instance. Commonly used for configurations, logging, or database connections.
  • Factory Method: Defines an interface for creating an object but lets subclasses decide which class to instantiate. Useful when the exact type of object needed is determined at runtime.
  • Abstract Factory: Provides an interface to create families of related objects without specifying their concrete classes. It’s helpful in systems that need to support multiple types of related products.
  • Builder: Separates the construction of a complex object from its representation, allowing the same construction process to create different representations. It’s ideal for constructing objects with multiple configurations.
  • Prototype: Creates new objects by copying an existing object, called a prototype. This is useful when object creation is costly and new instances need to be similar to existing ones.

When to Use:

Creational patterns are ideal when:

  • You need control over the object creation process.
  • The object construction process is complex and involves many steps.
  • You want to decouple the client code from the specific classes it instantiates.

2. Structural Patterns

Structural patterns focus on organizing and composing objects and classes to form larger, cohesive structures. They help simplify complex relationships between objects, enabling flexible and efficient interactions among them.

Key Structural Patterns:

  • Adapter: Allows incompatible interfaces to work together by converting the interface of a class into another expected by the client. Useful when integrating new functionality with legacy code.
  • Composite: Composes objects into tree-like structures to represent part-whole hierarchies, allowing clients to treat individual objects and compositions uniformly. Commonly used in UI frameworks.
  • Decorator: Dynamically adds new responsibilities to an object without altering its structure, providing a flexible alternative to subclassing.
  • Facade: Provides a simplified interface to a complex subsystem, making it easier for clients to interact with the system without needing to understand its complexities.
  • Proxy: Provides a surrogate or placeholder for another object to control access to it, useful for lazy initialization, logging, or access control.
  • Bridge: Decouples an object’s abstraction from its implementation so that the two can vary independently. Often used when you need to support multiple implementations.
  • Flyweight: Reduces the cost of creating and managing a large number of similar objects by sharing common parts among them. Frequently used in memory-constrained environments.

When to Use:

Structural patterns are ideal when:

  • You need to integrate or modify relationships between classes without altering their internal structures.
  • Managing complex or hierarchical relationships among classes is necessary.
  • You want to add functionality dynamically or reduce memory usage.

3. Behavioral Patterns

Behavioral patterns are concerned with the communication between objects and the distribution of responsibilities. They help manage complex interactions between objects, ensuring loose coupling and flexibility in object collaboration.

Key Behavioral Patterns:

  • Observer: Defines a one-to-many dependency so that when one object changes state, all its dependents are notified and updated automatically. Common in event-driven systems.
  • Strategy: Encapsulates a family of algorithms, making them interchangeable. It allows the algorithm to vary independently from the clients that use it.
  • Command: Encapsulates a request as an object, allowing for parameterization of clients with different requests, queuing, logging, and undo functionality.
  • Iterator: Provides a way to access elements of a collection sequentially without exposing its underlying representation, commonly used in data structures.
  • State: Allows an object to change its behavior when its internal state changes, making it appear as if the object’s class has changed.
  • Mediator: Centralizes communication between objects, reducing the dependencies between interacting objects.
  • Chain of Responsibility: Passes a request along a chain of handlers, allowing multiple objects the chance to handle the request.
  • Template Method: Defines the skeleton of an algorithm, allowing subclasses to override specific steps without changing the overall structure.
  • Visitor: Adds further operations to objects without modifying them by separating the algorithm from the object structure.

When to Use:

Behavioral patterns are ideal when:

  • You need to manage complex interactions and communication between objects.
  • Dynamic behavior changes are required at runtime.
  • Responsibilities need to be distributed flexibly among objects.

Summary of Design Patterns Categories

CategoryPurposeKey Patterns
CreationalControl over the creation of objects, enhancing flexibility and reuseSingleton, Factory Method, Abstract Factory, Builder, Prototype
StructuralOrganization and composition of classes and objects to form larger structuresAdapter, Composite, Decorator, Facade, Proxy, Bridge, Flyweight
BehavioralManagement of communication, responsibilities, and interactions between objectsObserver, Strategy, Command, Iterator, State, Mediator, Chain of Responsibility, Template Method, Visitor

Additional Design Pattern Categories

While the three main categories above are the most widely recognized, there are also other specialized pattern categories that have emerged as software development has evolved:

  • Concurrency Patterns: Patterns that deal with multi-threading and concurrent operations. Examples include Thread Pool, Producer-Consumer, and Read-Write Lock.
  • Architectural Patterns: These are high-level patterns that define the structure of an application as a whole, such as Model-View-Controller (MVC), Microservices, and Event-Driven Architecture.

How to Choose the Right Design Pattern

Choosing the right design pattern depends on:

  • The specific problem you’re trying to solve.
  • The flexibility and scalability requirements of the application.
  • The interactions between objects (for behavioral patterns), object creation needs (for creational patterns), or class composition (for structural patterns).
  • Future requirements and maintainability considerations for the codebase.

Understanding the purpose and application of each pattern helps you select the appropriate one for your design challenge, ultimately leading to cleaner, more maintainable, and scalable code.

TAGS
System Design Interview
CONTRIBUTOR
Design Gurus Team

GET YOUR FREE

Coding Questions Catalog

Design Gurus Newsletter - Latest from our Blog
Boost your coding skills with our essential coding questions catalog.
Take a step towards a better tech career now!
Explore Answers
SQL vs. NoSQL
What happens in a Behavioural interview?
How hard is a Coinbase interview?
Related Courses
Image
Grokking the Coding Interview: Patterns for Coding Questions
Image
Grokking Data Structures & Algorithms for Coding Interviews
Image
Grokking Advanced Coding Patterns for Interviews
Image
One-Stop Portal For Tech Interviews.
Copyright © 2024 Designgurus, Inc. All rights reserved.