Design patterns and its types

Introduction

In this article, we will explore various design patterns and their practical applications in software development. These patterns are used to solve recurring design problems that help us create well-structured, maintainable, and extensible software systems. Let's examine their usage, benefits, and implementation techniques. By understanding and applying design patterns, developers can enhance their software design skills and build robust and adaptable applications.

Types of Design Patterns

In design patterns, we have more than 20+ patterns which are categorized into these three types of patterns

Creational patterns

In simple terms, a creational pattern is a design pattern that focuses on the process of creating objects. It provides guidelines and techniques for creating objects in a flexible and reusable way, decoupling the object creation process from the client code.

Creational patterns help address common challenges related to object creation, such as controlling the number of object instances and providing a consistent interface for creating objects or creating objects based on certain conditions or configurations.

Please find a few patterns which classified as creational patterns,

Singleton: Ensuring that only one instance of a class exists.

  • Example: Singleton pattern is a logging system where there is only one instance of the logger object that is shared across multiple components, ensuring consistent logging behavior and avoiding unnecessary resource consumption.

Factory Method pattern: Defining an interface for creating objects but letting subclasses decide which class to instantiate.

  • Example: Factory Method pattern is a vehicle manufacturing plant where different types of vehicles (cars, motorcycles, trucks) are produced using a common factory interface, allowing for flexible and extensible vehicle creation based on specific requirements.

Abstract Factory pattern: Creating families of related or dependent objects.

  • Example: Abstract Factory pattern is a user interface toolkit that provides different themes (e.g., light theme, dark theme) with consistent styles across multiple UI components. The abstract factory interface allows the creation of UI components based on the selected theme, ensuring a cohesive and unified user interface experience.

Builder pattern: Separating the construction of complex objects from their representation.

  • Example: Builder pattern in C# is the construction of a complex SQL query using a query builder object, where the builder separates the construction of the query's components (such as SELECT, FROM, and WHERE clauses) and allows flexible customization of the query structure and conditions.

Prototype pattern: Creating new objects by copying existing ones.

  • Example: Prototype pattern is creating multiple instances of a complex object, such as a customized form template, by cloning an existing prototype object rather than creating it from scratch each time, improving performance and maintaining consistency in form design.

Notes: The difference between Factory vs Abstract Factory is the Factory pattern focuses on creating a single type of object, while the Abstract Factory pattern provides an interface for creating families of related objects. The Factory pattern is simpler and more straightforward, whereas the Abstract Factory pattern adds an additional layer of abstraction to handle complex object creation scenarios with multiple related products.

Structural patterns

Structural patterns in software design focus on organizing and structuring classes and objects to form larger structures. They help establish relationships and define how objects work together to achieve the desired functionality.

Please find a few patterns which classified as a structural pattern

Adapter: It is used to bridge incompatible interfaces.

  • Example: An adapter pattern is a USB-to-Ethernet adapter that allows a computer with a USB port to connect to an Ethernet network, bridging the incompatibility between the two interfaces and enabling communication between the computer and the network.

Composite: This pattern is typically used to build tree-like structures.

  • Example: Composite pattern is a file system where directories and files are represented as a tree-like structure. Each directory can contain subdirectories and files, forming a hierarchical composition of objects. The Composite pattern allows treating individual files and directories uniformly, enabling operations like traversing the entire file system, performing actions on directories or files recursively, and representing complex nested structures in a unified manner.

Proxy: It's used to provide a placeholder for objects.

  • Example: The proxy pattern is used to handle client requests, perform authentication and authorization checks, and forward the requests to the file server, providing additional functionalities like caching, logging, or load balancing without the client being aware of the underlying server implementation.

Behavioral patterns

These patterns in software design focus on defining and managing the interaction and communication between objects to achieve specific behaviors. They provide solutions for handling complex control flows, managing relationships between objects, and encapsulating behaviors in a modular and reusable manner.

Few common behavioral patterns

Observer: It establishes a one-to-many dependency between objects. In this pattern, an object called the subject maintains a list of its dependent objects, known as observers, and automatically notifies them of any changes in its state. Observers can subscribe to or unsubscribe from the subject's notifications.

  • Example: Observer pattern is a news agency and its subscribers. The news agency (subject) collects news updates and maintains a list of subscribers (observers).

Strategy: It is used to encapsulate interchangeable strategies within separate classes, allowing them to be selected and used at runtime

  • Example: Strategy pattern is used to choose the strategy at run time. e.g., whether the campaign sending option via mail, SMS, or call.

Command: It is used to encapsulate requests as objects. It decouples the sender of a request from the receiver, providing flexibility and extensibility in command execution.

  • Example: Command pattern is a remote control for a home entertainment system. Each button on the remote control represents a command object that encapsulates a specific action, such as turning on the TV, changing the channel, or adjusting the volume. The remote control acts as the invoker, receiving and executing the commands. This pattern allows for easy customization of button mappings, queuing of commands, and the possibility of implementing features like undo/redo functionality.

Conclusion

In summary, this article highlights the importance of design patterns in improving code modularity, reusability, flexibility, and maintainability. It explains the concepts behind each pattern and provides real-time examples to illustrate their usage in practical scenarios. By understanding and applying design patterns, developers can enhance their software design skills, tackle common design challenges effectively, and create robust and well-structured software systems. Design patterns serve as proven solutions to recurring problems, allowing developers to leverage established best practices and design principles.

Overall, the article serves as a valuable resource for developers looking to expand their knowledge of design patterns and apply them to their software projects, ultimately leading to better-designed, more maintainable, and scalable software solutions.

I hope this was helpful and you enjoyed the content.