Design Patterns - Creational


In Software Engineering, Creational patterns provide object creation mechanisms that increase flexibility and reuse of existing code.

  • Abstract Factory: Creates an instance of several families of classes
  • Builder-: Separates object construction from its representation
  • Factory Method: Creates an instance of several derived classes
  • Object Pool: Avoid expensive acquisition and release of resources by recycling objects that are no - longer in use
  • Prototype: A fully initialized instance to be copied or cloned
  • Singleton: A class of which only a single instance can exist

1. Abstract Factory

  • As a developer, I can create families of related objects without specifying their concrete classes, in order to seamlessly switch between different product variants (e.g., different UI themes).
  • As a user, I can benefit from consistent behavior across a family of products, in order to avoid compatibility issues when changing an entire subsystem (e.g., from MySQL to PostgreSQL).
  • As a product manager, I can request new product families (e.g., a mobile theme vs. a desktop theme) in order to maintain flexibility and accommodate evolving requirements.

Explanation: Abstract Factory defines an interface for creating families of related objects without specifying their concrete classes. This pattern helps ensure the client code remains unaware of the specific product classes it uses.

2. Builder

  • As a developer, I can construct complex objects step by step (e.g., building a car with various options), in order to produce different representations of the same object.
  • As a product manager, I can request different configurations (e.g., an “economy” vs. “luxury” car) without being concerned about the construction details, in order to match diverse customer needs.
  • As a user, I can specify only the features I care about (e.g., engine type, color), in order to receive a product tailored to my requirements.

Explanation: Builder separates the construction of a complex object from its representation. This enables you to build different complex objects using the same construction process.

3. Factory Method

  • As a developer, I can rely on a method in a base class that determines the actual class to instantiate (e.g., a createButton() method returning different types of Button), in order to avoid tight coupling to specific implementations.
  • As a user, I can invoke a general creation method (e.g., createDialog()) in order to let the application decide the most appropriate subtype (e.g., WindowsDialog, WebDialog).
  • As a product owner, I can add or modify product variants (subclasses) with minimal impact on existing code, in order to extend the application’s functionality.

Explanation: Factory Method defines an interface for creating an object, letting subclasses decide which class to instantiate. It helps decouple the code that uses the object from the code that actually creates the object.

4. Object Pool

  • As a developer, I can recycle expensive-to-create objects (e.g., database connections) in order to reduce overhead and improve performance.
  • As a system administrator, I can manage resource-intensive objects more efficiently (e.g., limit the maximum number of concurrent connections) in order to maintain system stability.
  • As a project lead, I can establish a mechanism to borrow and return objects to the pool in order to ensure a balanced and cost-effective use of system resources.

Explanation: Object Pool manages a set of reusable objects, allowing clients to borrow and return them instead of creating and destroying them repeatedly. This is useful for resources that are expensive to create or manage.

5. Prototype

  • As a developer, I can clone an existing object (e.g., a complex game character) in order to quickly produce variations with slight modifications.
  • As a user, I can start with a “template” object (a fully initialized instance) and create a copy, in order to adjust properties without altering the original configuration.
  • As a project manager, I can swiftly replicate objects with pre-set defaults, in order to reduce time spent on redundant initializations.

Explanation: Prototype lets you copy existing objects without making your code dependent on their classes. It’s particularly useful when objects are resource-heavy to initialize.

6. Singleton

  • As a developer, I can guarantee only one instance of a particular class (e.g., a global configuration manager) exists, in order to provide a single point of access to shared resources.
  • As a user, I can rely on consistent state provided by a central manager or registry, in order to avoid conflicting information across different parts of the application.
  • As a system architect, I can ensure global data integrity by restricting object creation, in order to prevent multiple conflicting instances from co-existing.

Explanation: Singleton restricts the instantiation of a class to one “global” object, which can be accessed from anywhere in the program. This pattern is often used for central or shared resources.

Done!

Thanks for reading! We hope this overview of creational design patterns will help you appreciate the usefulness and usecases of these patterns in software development.