Builder and Prototype Patterns

 Builder Pattern

The Builder pattern is a creational pattern that allows for the construction of complex objects step by step. It separates the construction of a complex object from its representation, allowing the same construction process to create different representations.


Key Points

- Separates the construction of a complex object from its representation.

- Allows for step-by-step construction.

- Can produce different representations of the same object.


Implementation Example

// Product Class
public class House {
    private String foundation;
    private String structure;
    private String roof;
    private String interior;

    // Getters and Setters
    public void setFoundation(String foundation) {
        this.foundation = foundation;
    }

    public void setStructure(String structure) {
        this.structure = structure;
    }

    public void setRoof(String roof) {
        this.roof = roof;
    }

    public void setInterior(String interior) {
        this.interior = interior;
    }

    @Override
    public String toString() {
        return "House [foundation=" + foundation + ", structure=" + structure + ", roof=" + roof + ", interior=" + interior + "]";
    }
}

// Builder Interface
public interface HouseBuilder {
    void buildFoundation();
    void buildStructure();
    void buildRoof();
    void buildInterior();
    House getHouse();
}

// Concrete Builder
public class ConcreteHouseBuilder implements HouseBuilder {
    private House house;

    public ConcreteHouseBuilder() {
        this.house = new House();
    }

    public void buildFoundation() {
        house.setFoundation("Concrete Foundation");
    }

    public void buildStructure() {
        house.setStructure("Concrete Structure");
    }

    public void buildRoof() {
        house.setRoof("Concrete Roof");
    }

    public void buildInterior() {
        house.setInterior("Modern Interior");
    }

    public House getHouse() {
        return this.house;
    }
}

// Director
public class CivilEngineer {
    private HouseBuilder houseBuilder;

    public CivilEngineer(HouseBuilder houseBuilder) {
        this.houseBuilder = houseBuilder;
    }

    public House constructHouse() {
        houseBuilder.buildFoundation();
        houseBuilder.buildStructure();
        houseBuilder.buildRoof();
        houseBuilder.buildInterior();
        return houseBuilder.getHouse();
    }
}

// Usage
public class Client {
    public static void main(String[] args) {
        HouseBuilder builder = new ConcreteHouseBuilder();
        CivilEngineer engineer = new CivilEngineer(builder);
        House house = engineer.constructHouse();
        System.out.println(house);
    }
}


Prototype Pattern

The Prototype pattern is a creational pattern that allows cloning of objects, creating new instances by copying an existing instance. This is useful for avoiding the cost of creating a new instance from scratch and instead copying the properties of an existing object.


Key Points

- Allows cloning of existing objects.

- Avoids the overhead of creating new instances from scratch.

- Provides a mechanism for creating new objects based on an existing template.


Implementation Example

// Prototype Interface
public interface Prototype {
    Prototype clone();
}

// Concrete Prototype
public class ConcretePrototype implements Prototype {
    private String state;

    public ConcretePrototype(String state) {
        this.state = state;
    }

    public void setState(String state) {
        this.state = state;
    }

    public String getState() {
        return state;
    }

    public Prototype clone() {
        return new ConcretePrototype(this.state);
    }

    @Override
    public String toString() {
        return "ConcretePrototype [state=" + state + "]";
    }
}

// Usage
public class Client {
    public static void main(String[] args) {
        // Create an instance of ConcretePrototype
        ConcretePrototype original = new ConcretePrototype("Initial State");

        // Clone the original object
        ConcretePrototype clone = (ConcretePrototype) original.clone();
        
        // Modify the state of the clone
        clone.setState("Modified State");

        // Print both objects to show they are distinct
        System.out.println("Original: " + original);
        System.out.println("Clone: " + clone);
    }
}


Comparison

- Builder Pattern:

  - Focuses on constructing complex objects step by step.

  - Provides control over the construction process.

  - Typically involves a Director class that guides the construction process.

  - Useful for objects that require multiple steps to construct and can have different representations.


- Prototype Pattern:

  - Focuses on cloning existing objects.

  - Avoids the overhead of creating new instances from scratch.

  - Allows for the creation of new objects based on an existing template.

  - Useful when the cost of creating a new object is high or when objects are complex and time-consuming to create.


Both patterns are part of the creational design patterns family but solve different types of problems. The Builder pattern is ideal for constructing complex objects with a clear construction process, while the Prototype pattern is useful for creating new instances by copying existing objects.

Nenhum comentário:

Postar um comentário

Internet of Things (IoT) and Embedded Systems

The  Internet of Things (IoT)  and  Embedded Systems  are interconnected technologies that play a pivotal role in modern digital innovation....