Last active
June 27, 2017 08:50
-
-
Save AdelinGhanaem/fae568cd695612d50dbf9e29275b683d to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| public abstract class House { | |
| protected Floor floor; | |
| protected Walls walls; | |
| protected Roof roof; | |
| //getters and setters | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| public abstract class HouseBuilder { | |
| protected House house; | |
| protected Floor floor; | |
| protected Walls walls; | |
| protected Roof roof; | |
| public abstract House createHouse(); | |
| public abstract Floor createFloor(); | |
| public abstract Walls createWalls(); | |
| public abstract Roof createRoof(); | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| public class HouseClient { | |
| public static void main(String[] args) { | |
| HouseDirector director = new HouseDirector(); | |
| HouseBuilder woodBuilder = new WoodBuilder(); | |
| BrickBuilder brickBuilder = new BrickBuilder(); | |
| // Build a wooden house | |
| House woodHouse = director.construcHouse(woodBuilder); | |
| System.out.println(); | |
| // Build a brick house | |
| House brickHouse = director.construcHouse(brickBuilder); | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| public class HouseDirector { | |
| public House construcHouse(HouseBuilder builder) { | |
| House house = builder.createHouse(); | |
| System.out.println(house.getRepresentation()); | |
| house.setFloor(builder.createFloor()); | |
| System.out.println(house.getFloor().getRepresentation()); | |
| house.setWalls(builder.createWalls()); | |
| System.out.println(house.getWalls().getRepresentation()); | |
| house.setRoof(builder.createRoof()); | |
| System.out.println(house.getRoof().getRepresentation()); | |
| return house; | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| public class WoodBuilder extends HouseBuilder { | |
| public Floor createFloor() { | |
| floor = new WoodFloor(); | |
| return floor; | |
| } | |
| public House createHouse() { | |
| house = new WoodHouse(); | |
| return house; | |
| } | |
| public Roof createRoof() { | |
| roof = new WoodRoof(); | |
| return roof; | |
| } | |
| public Walls createWalls() { | |
| walls = new WoodWalls(); | |
| return walls; | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment