Untitled

mail@pastecode.io avatar
unknown
java
7 months ago
4.7 kB
1
Indexable
Never
interface HousePlan {
    public void setBasement(String basement);

    public void setStructure(String structure);

    public void setRoof(String roof);

    public void setInterior(String interior);

    public void buildBalcony(String balcony);
}

class House implements HousePlan {
    private String basement;
    private String structure;
    private String roof;
    private String interior;
    private String balcony;

    public void setBasement(String basement) {
        this.basement = basement;
    }

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

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

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

    public void buildBalcony(String balcony) {
        this.balcony = balcony;
    }

    @Override
    public String toString() {
        return "House{" +
                "basement='" + basement + '\'' +
                ", structure='" + structure + '\'' +
                ", roof='" + roof + '\'' +
                ", interior='" + interior + '\'' +
                ", balcony='" + balcony + '\'' +
                '}';
    }
}

interface HouseBuilder {
    public void buildBasement();

    public void buildStructure();

    public void buildRoof();

    public void buildInterior();

    public House getHouse();
}

class IglooHouseBuilder implements HouseBuilder {
    private House house;

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

    public void buildBasement() {
        house.setBasement("Ice Bars");
    }

    public void buildStructure() {
        house.setStructure("Ice Blocks");
    }

    public void buildInterior() {
        house.setInterior("Ice Carvings");
    }

    public void buildRoof() {
        house.setRoof("Ice Dome");
    }

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

class TipiHouseBuilder implements HouseBuilder {
    private House house;

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

    public void buildBasement() {
        house.setBasement("Wooden Poles");
    }

    public void buildStructure() {
        house.setStructure("Wood and Ice");
    }

    public void buildInterior() {
        house.setInterior("Fire Wood");
    }

    public void buildRoof() {
        house.setRoof("Wood, caribou and seal skins");
    }

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

class ModernHouseBuilder implements HouseBuilder {
    private House house;

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

    public void buildBasement() {
        house.setBasement("Concrete");
    }

    public void buildStructure() {
        house.setStructure("Steel and Glass");
    }

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

    public void buildRoof() {
        house.setRoof("Solar Panels");
    }

    public void buildBalcony(String balcony) {
        house.buildBalcony(balcony);
    }

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

class CivilEngineer {
    private HouseBuilder houseBuilder;

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

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

    public void constructHouse() {
        this.houseBuilder.buildBasement();
        this.houseBuilder.buildStructure();
        this.houseBuilder.buildRoof();
        this.houseBuilder.buildInterior();
    }
}

public class Builder {
    public static void main(String[] args) {
        // Igloo House
        HouseBuilder iglooBuilder = new IglooHouseBuilder();
        CivilEngineer iglooEngineer = new CivilEngineer(iglooBuilder);

        iglooEngineer.constructHouse();

        House iglooHouse = iglooEngineer.getHouse();

        System.out.println("Igloo Builder constructed: " + iglooHouse);

        // Tipi House
        HouseBuilder tipiBuilder = new TipiHouseBuilder();
        CivilEngineer tipiEngineer = new CivilEngineer(tipiBuilder);

        tipiEngineer.constructHouse();

        House tipiHouse = tipiEngineer.getHouse();

        System.out.println("Tipi Builder constructed: " + tipiHouse);

        // Modern House
        ModernHouseBuilder modernBuilder = new ModernHouseBuilder();
        CivilEngineer modernEngineer = new CivilEngineer(modernBuilder);

        modernEngineer.constructHouse();
        modernBuilder.buildBalcony("Glass Balcony");

        House modernHouse = modernEngineer.getHouse();

        System.out.println("Modern Builder constructed: " + modernHouse);
    }
}