|
Object Creation
|
Singleton
|
Ensures only one instance of a class exists and provides a global access point.
|
Multiple instances may cause inconsistent shared state or unnecessary resource consumption.
|
When one shared instance is required throughout the application.
|
When independent instances or isolated state are required.
|
Configuration management, logging, shared services.
|
A Java configuration manager maintains one application-wide configuration instance.
|
|
Object Creation
|
Factory Method
|
Defines an interface for creating objects while allowing subclasses or implementations to decide which object to create.
|
Client code becomes tightly coupled to concrete classes.
|
When object creation depends on runtime conditions.
|
When object creation is simple and unlikely to change.
|
Payment processing, notification services, file processing.
|
A payment factory creates UPI, card, or net banking payment processors.
|
|
Object Creation
|
Abstract Factory
|
Creates families of related objects without exposing their concrete implementations.
|
Applications need compatible groups of objects without depending on implementation details.
|
When multiple related product families are required.
|
When only one simple object needs to be created.
|
Multi-platform applications, database providers, UI frameworks.
|
A banking application creates Oracle-specific or PostgreSQL-specific database components.
|
|
Object Construction
|
Builder
|
Constructs complex objects step by step using a flexible construction process.
|
Constructors with many parameters become difficult to maintain and understand.
|
When objects have many optional or mandatory properties.
|
When objects contain only a few simple properties.
|
API request objects, configuration objects, report generation.
|
A Java report builder creates customized financial reports with filters and formatting options.
|
|
Object Cloning
|
Prototype
|
Creates new objects by copying an existing object.
|
Creating complex objects repeatedly can be expensive.
|
When object creation is costly and similar objects are required.
|
When objects contain complex shared mutable references that cannot be safely copied.
|
Document templates, configuration snapshots, workflow templates.
|
A batch processing system clones a predefined job configuration for different business groups.
|