Day 4 - Builder, Prototype Design Patterns

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 2

Builder Design Pattern

Where is it used:

● Complex Object Creation: Building objects with many optional settings.


● Fluent Interface: Readable, chainable configuration code.
● Variability in Creation: Creating variations of objects.
● Document Generation: Constructing complex documents or reports.
● Configurable Components: Simplifying component configuration.

Key Components:
● Director: Responsible for orchestrating the construction process and delegates the construction steps
to the builder.
● Builder: Abstract interface or base class for creating parts of the complex object.
● ConcreteBuilder: Implements the builder interface and provides specific implementations for
constructing the parts and assembling the object.
● Product: Represents the complex object being constructed.

Advantages:
● Separation of Concerns: The Builder pattern separates the construction logic from the representation,
promoting a clean separation of concerns.
● Complex Object Creation: It simplifies the creation of complex objects with many optional parameters
by providing a step-by-step construction process.

Disadvantages:
● Code Overhead: Introducing a builder can lead to additional code and complexity, especially for
objects with only a few parameters.
● Complexity: For simple objects, using the Builder pattern can be overkill and make the code
unnecessarily complex.
● Increased Memory Usage: Builders can lead to increased memory usage, as they involve creating
multiple objects during construction.

Example Use Cases:


● Building complex documents (e.g., HTML, PDF) with various formatting options.
● Constructing complex meals with various components (e.g., fast-food order).

Prototype Design Pattern

● Object Copying: Creating objects by copying existing ones.


● Reducing Object Creation Overhead: Costly object initialization.
● Minimizing Subclassing: Avoiding a hierarchy of subclasses.
● Variations of Object Instances: Creating similar, but distinct, instances.
● Cloning Complex Structures: Deep copying of intricate objects.
● Prototype Registration: Managing a collection of prototypes.

Key Components:
a. Prototype: An abstract interface or base class that declares a method for cloning itself.
b. ConcretePrototype: Implements the prototype interface and provides a method for cloning itself.
c. Client: Initiates the cloning process by requesting the creation of new objects from prototypes.

Advantages:
● Reduced Object Creation Overhead: It eliminates the need to create objects from scratch, saving
time and resources.
● Dynamic Object Creation: Allows for the creation of new object instances with varying configurations
and data, making it suitable for scenarios where objects evolve.
● Simplifies Object Initialization: Objects can be created with a default state and customized as
needed, simplifying the initialization process.

Disadvantages:
● Cloning Complexity: Cloning objects may be complex, especially when dealing with deep copies of
objects with complex internal structures.
● Shallow Copy Limitations: By default, prototypes often perform shallow copies, which may not be
suitable for objects with complex or shared internal state.

Example Use Cases:


● Creating copies of graphical objects in a drawing application.
● Generating copies of documents or reports with different data.
● Cloning configurations for software components.

You might also like