Rate this post

 

Design patterns are typical solutions to common problems in software design. Each pattern is like a blueprint that you can customize to solve a particular design problem in your code. The aim of Builder Pattern is to separate the construction logic of objects away from their representation. This can help us to create different representations of the object using the same construction logic.

  • Builder Desing Pattern is a type of creational design pattern which is used to create complex objects in a step-by-step approach. in another hand. A factory method pattern (also known as a factory pattern) offers developers a good alternative to subclass management.

1. What is the goal of the Builder design pattern

The Builder design pattern aims to separate the construction and representation of a complex object. Therefore, the same construction process can be used to create different representations this way.

2. What is the goal of the factory method pattern

The factory pattern aims to solve a fundamental problem in the instantiation, the creation of a concrete object of a class – in object-oriented programming. In principle, creating an object directly within the class that needs or should use this object is possible, but very inflexible. It binds the class to this object and makes it impossible to change the instantiation independently of the class.

Creating an object frequently necessitates complex procedures that are inappropriate for inclusion in a composing object. Therefore. the object’s creation could result in significant code duplication or necessitate information that isn’t available to the composing object. fail to provide a sufficient level of abstraction, or fall outside the scope of the composing object.

However, the factory method design pattern solves these issues by defining a separate method for creating objects. which subclasses can override to specify the type of derived product that will be created.

3. Advantages & Disadvantages

 Advantages Disadvantages
Factory design patternModular expandability of the application.

Good testability.

Significant method names
A high number of required classes.

Extension of the application is very elaborate
Builder design patternAllows you to vary a product’s internal representation.

Encapsulates code for construction and representation.

Provides control over steps of the construction process.
A distinct ConcreteBuilder must be created for each type of product.

Builder classes must be mutable.

May hamper/complicate dependency injection.

3 Structure

a. Builder design pattern

UML class and sequence diagram:

Design pattern
UML class and sequence diagram

Class diagram :

Design pattern

b.Factory design pattern

UML class diagram

4 Exemples

Basic Factory Example

// Factory
static class FruitFactory {
    static Fruit create(name, color, firmness) {
        // Additional logic
        return new Fruit(name, color, firmness);
    }
}

// Usage
Fruit fruit = FruitFactory.create("apple", "red", "crunchy");

Basic Builder Example

// Builder
class FruitBuilder {
    String name, color, firmness;
    FruitBuilder setName(name)         { this.name     = name;     return this; }
    FruitBuilder setColor(color)       { this.color    = color;    return this; }
    FruitBuilder setFirmness(firmness) { this.firmness = firmness; return this; }
    Fruit build() {
        return new Fruit(this); // Pass in the builder
    }
}

// Usage
Fruit fruit = new FruitBuilder()
        .setName("apple")
        .setColor("red")
        .setFirmness("crunchy")
        .build();

In conclusion , you can compare the code samples from these two Wikipedia pages:

https://en.wikipedia.org/wiki/Builder_pattern#Advantages

https://en.wikipedia.org/wiki/Factory_method_pattern


Like it? Share with your friends!

0

2 Comments

Your email address will not be published.