DesignPattern
DesignPattern
Design patterns can be broken down into three types, organized by their intent into creational design
patterns, structural design patterns, and behavioral design patterns.
1. Creational Design Patterns help in creating objects in a flexible and reusable way. They decide
how and when objects should be created.
• Factory Method: Creates objects using a common interface; subclasses decide which object to
create.
• Abstract Factory: Creates related objects as a group or family.
• Builder: Builds complex objects step by step.
• Prototype: Makes copies of existing objects.
• Singleton: Ensures only one object of a class is created.
2. Structural Design Patterns focus on how classes and objects are combined to form larger
structures.
• Adapter: Makes two incompatible interfaces work together.
• Bridge: Separates interface from its implementation.
• Composite: Treats a group of objects like a single object (tree structure).
• Decorator: Adds new features to objects without changing their code.
• Façade: Provides a simple interface for complex systems.
• Flyweight: Saves memory by sharing common data between objects.
• Proxy: Acts as a substitute for another object to control access or reduce cost.
3. Behavioral Design Patterns deal with how objects interact and communicate with each other.
• Chain of Responsibility: Passes a request along a chain of handlers.
• Command: Wraps a request as an object.
• Interpreter: Implements a grammar for a language.
• Iterator: Allows looping through elements in a collection.
• Mediator: Manages communication between multiple objects.
• Memento: Saves and restores an object’s state.
• Observer: Notifies other objects when something changes.
• State: Changes object behavior based on its state.
• Strategy: Selects an algorithm at runtime.
• Visitor: Adds new operations to a class without changing it.
• Template Method: Defines steps of an algorithm and lets subclasses customize some steps.
Singleton is a creational design pattern that ensures a class has only one instance and provides
global access to it.
For example, web developers may use a singleton for a sitemap to keep just one global version.
Other patterns like Factory Method, Builder, and Prototype can use singletons.
Also, Facade and State objects are often implemented as singletons.
2. Factory Method Design Pattern
Factory Method is a creational design pattern where object creation is handled by subclasses,
using a common interface.
It promotes loose coupling and code reuse, like a virtual constructor. Subclasses decide which
object to create, giving more flexibility.
New classes can be added easily to the factory.
However, it’s not ideal for simple cases—using it unnecessarily can over-complicate the code.
Facade is a structural design pattern that provides a simple interface to access complex systems
or multiple subsystems.
For example, in eCommerce, a customer uses one interface to place an order, while the facade
handles inventory, payment, and shipping behind the scenes.
It supports loose coupling, especially useful in microservices architecture.
4. Strategy Design Pattern
Strategy is a behavioral design pattern that lets you choose from a family of interchangeable algorithms
at runtime.
For example, an eCommerce site might sort products by size, color, or price based on user actions.
It’s powerful for personalized experiences, adapting behavior based on user inputs or location.
Observer is a behavioral design pattern where one object (subject) notifies many observers when
its state changes.
It’s useful in event-driven systems, like sending alerts for new comments or shipment updates.
6. Builder Design Pattern
Builder is a creational design pattern that separates object construction from its representation,
allowing step-by-step creation.
It gives more control over the build process and supports multiple object variations using the same
construction logic.
Adapter is a structural design pattern (a "wrapper") that converts one interface into another to
make incompatible classes work together.
It’s useful for creating a consistent API from mixed or legacy interfaces.
Popular Software Architectural Patterns