DEV Community

Cover image for Builder Pattern-Pattern
Sukma Rizki
Sukma Rizki

Posted on

Builder Pattern-Pattern

The Builder Pattern is a creational design pattern that allows for the step-by-step creation of complex objects. It separates the construction of an object from its representation, enabling the same construction process to create various representations.
Purpose
Construct a complex object step by step.
Separate the construction process from the actual representation.
Structure
Builder Interface: Specifies the abstract steps for creating a product.
Concrete Builder: Implements the builder interface to create specific parts of the product.
Product: The complex object that is being built.
Director: Controls the building process.
Example: Custom Card Builder
We’ll create a CustomCard widget using the Builder Pattern.

Image description

Explanation
Adaptee: SquarePeg is the existing class that we want to adapt.
Target: RoundHole is the interface that SquarePeg needs to adapt to.
Adapter: SquarePegAdapter converts the SquarePeg interface to RoundHole.
Use Case
In this example, we are adapting a SquarePeg to fit into a RoundHole using an adapter that converts the width of the square peg to the radius of the round hole. This allows us to determine if the square peg can fit into the round hole.

The Adapter Pattern is quite helpful when you need to integrate classes that have incompatible interfaces but you want them to work together seamlessly.

Top comments (0)