Implementing the Template Method Pattern with Java - BAEL-1289 (#3038)

* Initial Commit

* Update HighEndComputerBuilder.java

* Update HighEndComputerBuilder.java

* Update StandardComputerBuilder.java

* Update TemplateMethodPatternTest.java

* Update ComputerBuilder.java

* Delete HighEndComputer.java

* Delete StandardComputer.java
This commit is contained in:
Alejandro Gervasio 2017-11-14 04:39:46 -03:00 committed by adamd1985
parent 503fd2d24f
commit c0f9c00a3b
7 changed files with 16 additions and 88 deletions

View File

@ -2,36 +2,16 @@ package com.baeldung.templatemethodpattern.model;
import java.util.HashMap;
import java.util.Map;
import java.util.ArrayList;
import java.util.List;
public abstract class ComputerBuilder {
public class Computer {
protected Map<String, String> computerParts = new HashMap<>();
protected List<String> moterboardSetupStatus = new ArrayList<>();
private Map<String, String> computerParts = new HashMap<>();
public final Computer buildComputer() {
addMotherboard();
setupMotherboard();
addProcessor();
return getComputer();
}
public abstract void addMotherboard();
public abstract void setupMotherboard();
public abstract void addProcessor();
public List<String> getMotherboardSetupStatus() {
return moterboardSetupStatus;
public Computer(Map<String, String> computerParts) {
this.computerParts = computerParts;
}
public Map<String, String> getComputerParts() {
return computerParts;
}
private Computer getComputer() {
return new Computer(computerParts);
}
}

View File

@ -8,7 +8,7 @@ import java.util.Map;
public abstract class ComputerBuilder {
protected Map<String, String> computerParts = new HashMap<>();
protected List<String> moterboardSetupStatus = new ArrayList<>();
protected List<String> motherboardSetupStatus = new ArrayList<>();
public final Computer buildComputer() {
addMotherboard();
@ -24,7 +24,7 @@ public abstract class ComputerBuilder {
public abstract void addProcessor();
public List<String> getMotherboardSetupStatus() {
return moterboardSetupStatus;
return motherboardSetupStatus;
}
public Map<String, String> getComputerParts() {

View File

@ -1,26 +0,0 @@
package com.baeldung.templatemethodpattern.model;
public class HighEndComputer extends Computer {
@Override
public void addMotherboard() {
computerParts.put("Motherboard", "High-end Motherboard");
}
@Override
public void setupMotherboard() {
moterboardSetupStatus.add("Screwing the high-end motherboard to the case.");
moterboardSetupStatus.add("Pluging in the power supply connectors.");
moterboardSetupStatus.forEach(step -> System.out.println(step));
}
@Override
public void addProcessor() {
computerParts.put("Processor", "High-end Processor");
}
@Override
public void addMotherboard() {
computerParts.put("Motherboard", "High End Motherboard");
}
}

View File

@ -9,13 +9,13 @@ public class HighEndComputerBuilder extends ComputerBuilder {
@Override
public void setupMotherboard() {
moterboardSetupStatus.add("Screwing the high-end motherboard to the case.");
moterboardSetupStatus.add("Pluging in the power supply connectors.");
moterboardSetupStatus.forEach(step -> System.out.println(step));
motherboardSetupStatus.add("Screwing the high-end motherboard to the case.");
motherboardSetupStatus.add("Pluging in the power supply connectors.");
motherboardSetupStatus.forEach(step -> System.out.println(step));
}
@Override
public void addProcessor() {
computerParts.put("Processor", "High-end Processor");
computerParts.put("Processor", "High-end Processor");
}
}

View File

@ -1,25 +0,0 @@
package com.baeldung.templatemethodpattern.model;
public class StandardComputer extends Computer {
public void addMotherboard() {
computerParts.put("Motherboard", "Standard Motherboard");
}
@Override
public void setupMotherboard() {
moterboardSetupStatus.add("Screwing the standard motherboard to the case.");
moterboardSetupStatus.add("Pluging in the power supply connectors.");
moterboardSetupStatus.forEach(step -> System.out.println(step));
}
@Override
public void addProcessor() {
computerParts.put("Processor", "Standard Processor");
}
@Override
public void addMotherboard() {
computerParts.put("Motherboard", "Standard Motherboard");
}
}

View File

@ -9,9 +9,9 @@ public class StandardComputerBuilder extends ComputerBuilder {
@Override
public void setupMotherboard() {
moterboardSetupStatus.add("Screwing the standard motherboard to the case.");
moterboardSetupStatus.add("Pluging in the power supply connectors.");
moterboardSetupStatus.forEach(step -> System.out.println(step));
motherboardSetupStatus.add("Screwing the standard motherboard to the case.");
motherboardSetupStatus.add("Pluging in the power supply connectors.");
motherboardSetupStatus.forEach(step -> System.out.println(step));
}
@Override

View File

@ -29,8 +29,8 @@ public class TemplateMethodPatternTest {
@Test
public void givenStandardMotherBoard_whenAddingMotherBoard_thenEqualAssertion() {
standardComputer.addMotherboard();
assertEquals("Standard Motherboard", standardComputer.getComputerParts().get("Motherboard"));
standardComputerBuilder.addMotherboard();
assertEquals("Standard Motherboard", standardComputerBuilder.getComputerParts().get("Motherboard"));
}
@Test
@ -81,7 +81,6 @@ public class TemplateMethodPatternTest {
highEndComputerBuilder.buildComputer();
assertEquals(2, highEndComputerBuilder.getComputerParts().size());
}
@Test
public void givenAllHighEndParts_whenComputerisBuilt_thenComputerInstance() {
assertThat(standardComputerBuilder.buildComputer(), instanceOf(Computer.class));