Summary
Summary
Use Cases
Document creation
GUI creation
Meal ordering
Car manufacturing
Website creation
Game level design
Database Query
Terminologies
Product
Builder
Concrete Builder
Director
Client
When to Use
When you need to create a complex object that has multiple parts or
properties with different values or combination
When the construction process involves multiple steps or dependencies
that need to be resolved configure dynamically
When you want to isolate the construction code from the rest of the
codebase or client to improve modularity and maintainability
When you want to support multiple representations or variants of the same
object without duplication the construction logic
When you want to create objects using a fluent or readable syntax that
allows you to chain multiple method calls and set different options or
properties
Advantages
Decouples an abstraction from its implementation so that the two can vary
independently
Improves extensibility by allowing new implementations to be added easily.
Reduces the number of subclasses that need to be created for both the
abstraction and the implementation.
Provides a way to switch between different implementations at runtime.
Increases the flexibility and reusability of the code.
Disadvantages
Increases the overall complexity of the code by introducing a new layer of
abstraction
Can make the code more difficult to understand and maintain due to the
added complexity
Can lead to a performance overhead due to the extra layer of indirection.
Can make the code harder to test because of the added layer of
abstraction.
It's not always the best solution for all problems, it's important to evaluate
if it's the best pattern for the particular problem you're trying to solve.
-------------------------------------------------------------------------------------------------------------
When to Use
Object Creation
Decoupling
Extensibility
Variation in Object Creation
Testing and Mocking
Advantages
Loose Coupling
Extensibility
Single Responsibility
Reusability
Disadvantages
Complexity
Overhead
Subclasses Required
-------------------------------------------------------------------------------------------------------------
Terminologies
Singleton
Instance
getInstance()
Lazy Initialization
Thread Safety
When to Use
Shared Resources
Global State Management
Performance and Resource Optimization
Restricting Object Creation
Coordination across Systems
Avoiding Redundant Data
Advantages
Controlled Access
Global Access
Lazy Initialization
Memory Efficiency
Prevents Redundant Instances
Disadvantages
Limited Scalability
Global State
Testing Difficulties
Overuse Risk
Thread Safety Issues
-------------------------------------------------------------------------------------------------------------
Abstract Factory Design pattern
Use Cases
GUI Toolkits
Game Engines
Database connections
Document generators
Automobile Manufacturing
Payment Gateway
Logging Frameworks
Builders
Terminologies
Abstract Factory
Concrete Factory
Abstract Product
Concrete Product
Product Family
Factory Hierarchy
Client
When to Use
Multiple Families of Products
Consistent Object Creation
Enforce Interface Usage
Product Variations
Easily Extendable
Separation of Concerns
Advantages
Encourages loose coupling
Supports product family creation
Provides a consistent interface
Enables easy swapping of implementations
Facilitates extensibility
Disadvantages
Increased complexity
Limited flexibility in adding new product types
Increased code duplication
Tight coupling between the factory and product classes
Limited runtime flexibility
-------------------------------------------------------------------------------------------------------------
When to Use
When you want to reuse existing code that has a different interface from
what you need.
When you have a class with a complex interface that is difficult to use, and
you want to provide a simpler interface.
When you want to create a class that can work with multiple classes that
have different interfaces, but perform the same task.
When you want to create a class that can work with future classes that
have different interfaces, but perform the same task.
Advantages
Flexibility
Reusability
Maintainability
Scalability
Simplification
Disadvantages
Performance
Maintenance
Dependency Inversion
-------------------------------------------------------------------------------------------------------------
When to Use
You need to create objects that are similar but with slight variations in their
properties.
You want to avoid the cost of creating new objects by cloning existing ones.
You want to hide the complexity of creating new objects from the client.
You want to easily create new objects with complex initialization
requirements.
You want to create a template from which other objects can be derived.
Advantages
Reduce cost of creating new object
Improving the performance of application
Encapsulating object creation
Simplifying object creation
Dynamic object creation
Disadvantages
Increased Complexity
Object Identity