Thursday, 5 December 2013

Builder Design pattern - Real Time Example [Animal Toys]

Master Software Architecture & Clean Code!

Subscribe to Ram N Java for clean code, design pattern implementations, and practical Java tutorials!

SUBSCRIBE NOW

Understanding the Animal Toy Builder Architecture

The Builder Design Pattern is a foundational creational pattern designed to separate the construction of a complex object from its representation. When modeling real-world manufacturing pipelines—such as constructing customizable animal toys—creating individual objects through multi-argument constructors leads to unreadable, brittle code.

By delegating each assembly step (head, body, legs, and sound) to dedicated builder classes orchestrated by a director, you can construct distinct representations of an animal toy using a uniform, step-by-step process.

Complete Java Code Walkthrough

Below is a clean, practical Java implementation demonstrating how to build animal toys step by step using the Builder design pattern:

// 1. Product Class
class Toy {
    private String head;
    private String body;
    private String legs;
    private String soundModule;

    public void setHead(String head) { this.head = head; }
    public void setBody(String body) { this.body = body; }
    public void setLegs(String legs) { this.legs = legs; }
    public void setSoundModule(String soundModule) { this.soundModule = soundModule; }

    @Override
    public String toString() {
        return "Toy [Head=" + head + ", Body=" + body + ", Legs=" + legs + ", Sound=" + soundModule + "]";
    }
}

// 2. Builder Interface
interface ToyBuilder {
    void buildHead();
    void buildBody();
    void buildLegs();
    void buildSoundModule();
    Toy getToy();
}

// 3. Concrete Builder: Tiger Toy
class TigerToyBuilder implements ToyBuilder {
    private Toy toy = new Toy();

    @Override
    public void buildHead() { toy.setHead("Tiger Striped Head"); }

    @Override
    public void buildBody() { toy.setBody("Orange Striped Fur Body"); }

    @Override
    public void buildLegs() { toy.setLegs("4 Padded Paws"); }

    @Override
    public void buildSoundModule() { toy.setSoundModule("Growl Sound Module"); }

    @Override
    public Toy getToy() { return this.toy; }
}

// 4. Concrete Builder: Monkey Toy
class MonkeyToyBuilder implements ToyBuilder {
    private Toy toy = new Toy();

    @Override
    public void buildHead() { toy.setHead("Monkey Head with Ears"); }

    @Override
    public void buildBody() { toy.setBody("Brown Plush Body with Tail"); }

    @Override
    public void buildLegs() { toy.setLegs("2 Arms & 2 Legs"); }

    @Override
    public void buildSoundModule() { toy.setSoundModule("Chatter Sound Module"); }

    @Override
    public Toy getToy() { return this.toy; }
}

// 5. Director Class
class ToyManufacturer {
    private ToyBuilder toyBuilder;

    public ToyManufacturer(ToyBuilder toyBuilder) {
        this.toyBuilder = toyBuilder;
    }

    public void constructToy() {
        toyBuilder.buildHead();
        toyBuilder.buildBody();
        toyBuilder.buildLegs();
        toyBuilder.buildSoundModule();
    }

    public Toy getToy() {
        return toyBuilder.getToy();
    }
}

// 6. Client Application
public class AnimalToyBuilderDemo {
    public static void main(String[] args) {
        // Build a Tiger Toy
        ToyBuilder tigerBuilder = new TigerToyBuilder();
        ToyManufacturer manufacturer = new ToyManufacturer(tigerBuilder);
        manufacturer.constructToy();
        Toy tiger = manufacturer.getToy();
        System.out.println("Built: " + tiger);

        // Build a Monkey Toy
        ToyBuilder monkeyBuilder = new MonkeyToyBuilder();
        manufacturer = new ToyManufacturer(monkeyBuilder);
        manufacturer.constructToy();
        Toy monkey = manufacturer.getToy();
        System.out.println("Built: " + monkey);
    }
}

Key Architectural Advantages

  • Stepwise Construction: Encapsulates each step of object creation into explicit methods rather than huge constructor parameter lists.
  • Separation of Responsibilities: The Toy class only defines properties, the builders implement customization, and the ToyManufacturer orchestrates the sequence.
  • Extensibility: Adding new animal variants requires only creating another implementation of ToyBuilder without modifying existing code.

Watch More Related Design Pattern Tutorials


Click here to watch in Youtube : https://www.youtube.com/watch?v=RMO1q03GgPA

Click the below Image to Enlarge
Builder Design pattern - Real Time Example [Animal Toys]
























See also:

  • Builder Design Pattern - Introduction
  • Builder Design pattern - Real Time Example [Meal Package]
  • Builder Design pattern - Real Time Example [Beverage]
  • Builder Design Pattern - Class Diagram
  • Builder Design Pattern - Sequence Diagram
  • Builder Design Pattern - Implementation [Beverage]
  • Builder Design Pattern - Implementation [Animal Toy]
  • Builder Design Pattern - KeyPoints
  • All Design Patterns Links
  • No comments:

    Post a Comment

    Tutorials