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

Program 3

The document outlines a Java implementation of the Builder design pattern to create complex objects, specifically a House. It includes a House class with various attributes, a HouseBuilder interface, and two concrete builders for wooden and brick houses. A CivilEngineer class is used to construct the houses using the specified builder, demonstrating the flexibility of creating different house types.

Uploaded by

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

Program 3

The document outlines a Java implementation of the Builder design pattern to create complex objects, specifically a House. It includes a House class with various attributes, a HouseBuilder interface, and two concrete builders for wooden and brick houses. A CivilEngineer class is used to construct the houses using the specified builder, demonstrating the flexibility of creating different house types.

Uploaded by

bright31105
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Program3.

Design and implement a complex object like a House using a step-by-step Builder

pattern, allowing different representations of the house (wooden, brick, etc.).

public class Main {

// Product class

static class House {

private String foundation;

private String structure;

private String roof;

private String interior;

public void setFoundation(String foundation) {

[Link] = foundation;

public void setStructure(String structure) {

[Link] = structure;

public void setRoof(String roof) {

[Link] = roof;

public void setInterior(String interior) {

[Link] = interior;

}
public void showHouse() {

[Link]("House built with:");

[Link]("Foundation: " + foundation);

[Link]("Structure : " + structure);

[Link]("Roof : " + roof);

[Link]("Interior : " + interior);

[Link]("---------------------------------");

// Builder interface

interface HouseBuilder {

void buildFoundation();

void buildStructure();

void buildRoof();

void buildInterior();

House getHouse();

// Concrete Builder for Wooden House

static class WoodenHouseBuilder implements HouseBuilder {

private House house;

public WoodenHouseBuilder() {

[Link] = new House();

}
public void buildFoundation() {

[Link]("Wooden Poles Foundation");

public void buildStructure() {

[Link]("Wooden Walls");

public void buildRoof() {

[Link]("Wooden Shingles Roof");

public void buildInterior() {

[Link]("Wooden Interior");

public House getHouse() {

return house;

// Concrete Builder for Brick House

static class BrickHouseBuilder implements HouseBuilder {

private House house;

public BrickHouseBuilder() {

[Link] = new House();

public void buildFoundation() {

[Link]("Concrete & Stone Foundation"); }


public void buildStructure() {

[Link]("Brick Walls");

public void buildRoof() {

[Link]("Concrete Roof");

public void buildInterior() {

[Link]("Brick & Cement Interior");

public House getHouse() {

return house;

// Director class

static class CivilEngineer {

private HouseBuilder houseBuilder;

public CivilEngineer(HouseBuilder houseBuilder) {

[Link] = houseBuilder;

public House constructHouse() {

[Link]();

[Link]();

[Link]();

[Link]();
return [Link]();

// Main method

public static void main(String[] args) {

// Build a Wooden House

HouseBuilder woodenBuilder = new WoodenHouseBuilder();

CivilEngineer engineer1 = new CivilEngineer(woodenBuilder);

House woodenHouse = [Link]();

[Link]("Wooden House:");

[Link]();

// Build a Brick House

HouseBuilder brickBuilder = new BrickHouseBuilder();

CivilEngineer engineer2 = new CivilEngineer(brickBuilder);

House brickHouse = [Link]();

[Link]("Brick House:");

[Link]();

You might also like