0% found this document useful (0 votes)
14 views5 pages

Builder Pattern

The Builder Pattern allows constructing complex objects step-by-step and provides a way to vary the type of product built. It defines a common interface for all builders to construct parts and lets a director orchestrate the building process. Concrete builders implement the building steps to construct a product, while the director controls the building process but does not know the concrete classes.

Uploaded by

Nourhane Ibrahim
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
14 views5 pages

Builder Pattern

The Builder Pattern allows constructing complex objects step-by-step and provides a way to vary the type of product built. It defines a common interface for all builders to construct parts and lets a director orchestrate the building process. Concrete builders implement the building steps to construct a product, while the director controls the building process but does not know the concrete classes.

Uploaded by

Nourhane Ibrahim
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 5

Observer Pattern

The Builder Pattern is a creational design pattern that provides a way to construct a complex
object step by step. It separates the construction of a complex object from its representation,
allowing the same construction process to create different representations.
This pattern is particularly useful when an object has a large number of parameters, and it
becomes cumbersome to pass them all to the constructor.
The Builder Pattern typically involves the following components:
1. Director:
The Director is responsible for orchestrating the construction of the product. It directs the builder
on how to build the various parts and when to assemble them to create the final product.
2. Builder:
The Builder is an interface or an abstract class that declares the construction steps for building
the product. It provides methods for constructing each part of the complex object.
3. Concrete Builder:
Concrete Builder classes implement the Builder interface. They provide specific implementations
for the construction steps, building the parts of the complex object.
4. Product:
The Product is the complex object being constructed. It may have a complex internal structure
made up of several parts.

Here's a simplified example to illustrate the Builder Pattern. Let's say we want to build a Meal
object that consists of a Burger, Drink, and Dessert. The Meal is our complex object.

// Product: Meal
public class Meal {
private String burger;
private String drink;
private String dessert;

// getters and setters...

@Override
public String toString() {
return "Meal{" +
"burger='" + burger + '\'' +
", drink='" + drink + '\'' +
", dessert='" + dessert + '\'' +
'}';
}
}

// Builder: MealBuilder
public interface MealBuilder {
MealBuilder addBurger();
MealBuilder addDrink();
MealBuilder addDessert();
Meal build();
}

// Concrete Builder: VegMealBuilder


public class VegMealBuilder implements MealBuilder {
private Meal meal;

public VegMealBuilder() {
this.meal = new Meal();
}

@Override
public MealBuilder addBurger() {
meal.setBurger("Veg Burger");
return this;
}

@Override
public MealBuilder addDrink() {
meal.setDrink("Coke");
return this;
}

@Override
public MealBuilder addDessert() {
meal.setDessert("Ice Cream");
return this;
}

@Override
public Meal build() {
return meal;
}
}

// Concrete Builder: NonVegMealBuilder


public class NonVegMealBuilder implements MealBuilder {
private Meal meal;
public NonVegMealBuilder() {
this.meal = new Meal();
}

@Override
public MealBuilder addBurger() {
meal.setBurger("Chicken Burger");
return this;
}

@Override
public MealBuilder addDrink() {
meal.setDrink("Pepsi");
return this;
}

@Override
public MealBuilder addDessert() {
meal.setDessert("Chocolate Cake");
return this;
}

@Override
public Meal build() {
return meal;
}
}

// Director: Waiter
public class Waiter {
private MealBuilder mealBuilder;

public Waiter(MealBuilder mealBuilder) {


this.mealBuilder = mealBuilder;
}

public Meal constructMeal() {


return mealBuilder.addBurger().addDrink().addDessert().build();
}
}

// Application
public class Application {
public static void main(String[] args) {
MealBuilder vegMealBuilder = new VegMealBuilder();
MealBuilder nonVegMealBuilder = new NonVegMealBuilder();

Waiter waiter = new Waiter(vegMealBuilder);


Meal vegMeal = waiter.constructMeal();
System.out.println("Veg Meal: " + vegMeal);

waiter = new Waiter(nonVegMealBuilder);


Meal nonVegMeal = waiter.constructMeal();
System.out.println("Non-Veg Meal: " + nonVegMeal);
}
}

You might also like