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:
parent
503fd2d24f
commit
c0f9c00a3b
|
@ -2,36 +2,16 @@ package com.baeldung.templatemethodpattern.model;
|
||||||
|
|
||||||
import java.util.HashMap;
|
import java.util.HashMap;
|
||||||
import java.util.Map;
|
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<>();
|
private Map<String, String> computerParts = new HashMap<>();
|
||||||
protected List<String> moterboardSetupStatus = new ArrayList<>();
|
|
||||||
|
|
||||||
public final Computer buildComputer() {
|
public Computer(Map<String, String> computerParts) {
|
||||||
addMotherboard();
|
this.computerParts = computerParts;
|
||||||
setupMotherboard();
|
|
||||||
addProcessor();
|
|
||||||
return getComputer();
|
|
||||||
}
|
|
||||||
|
|
||||||
public abstract void addMotherboard();
|
|
||||||
|
|
||||||
public abstract void setupMotherboard();
|
|
||||||
|
|
||||||
public abstract void addProcessor();
|
|
||||||
|
|
||||||
public List<String> getMotherboardSetupStatus() {
|
|
||||||
return moterboardSetupStatus;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public Map<String, String> getComputerParts() {
|
public Map<String, String> getComputerParts() {
|
||||||
return computerParts;
|
return computerParts;
|
||||||
}
|
}
|
||||||
|
|
||||||
private Computer getComputer() {
|
|
||||||
return new Computer(computerParts);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -8,7 +8,7 @@ import java.util.Map;
|
||||||
public abstract class ComputerBuilder {
|
public abstract class ComputerBuilder {
|
||||||
|
|
||||||
protected Map<String, String> computerParts = new HashMap<>();
|
protected Map<String, String> computerParts = new HashMap<>();
|
||||||
protected List<String> moterboardSetupStatus = new ArrayList<>();
|
protected List<String> motherboardSetupStatus = new ArrayList<>();
|
||||||
|
|
||||||
public final Computer buildComputer() {
|
public final Computer buildComputer() {
|
||||||
addMotherboard();
|
addMotherboard();
|
||||||
|
@ -24,7 +24,7 @@ public abstract class ComputerBuilder {
|
||||||
public abstract void addProcessor();
|
public abstract void addProcessor();
|
||||||
|
|
||||||
public List<String> getMotherboardSetupStatus() {
|
public List<String> getMotherboardSetupStatus() {
|
||||||
return moterboardSetupStatus;
|
return motherboardSetupStatus;
|
||||||
}
|
}
|
||||||
|
|
||||||
public Map<String, String> getComputerParts() {
|
public Map<String, String> getComputerParts() {
|
||||||
|
|
|
@ -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");
|
|
||||||
}
|
|
||||||
}
|
|
|
@ -9,9 +9,9 @@ public class HighEndComputerBuilder extends ComputerBuilder {
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void setupMotherboard() {
|
public void setupMotherboard() {
|
||||||
moterboardSetupStatus.add("Screwing the high-end motherboard to the case.");
|
motherboardSetupStatus.add("Screwing the high-end motherboard to the case.");
|
||||||
moterboardSetupStatus.add("Pluging in the power supply connectors.");
|
motherboardSetupStatus.add("Pluging in the power supply connectors.");
|
||||||
moterboardSetupStatus.forEach(step -> System.out.println(step));
|
motherboardSetupStatus.forEach(step -> System.out.println(step));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
|
|
@ -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");
|
|
||||||
}
|
|
||||||
}
|
|
|
@ -9,9 +9,9 @@ public class StandardComputerBuilder extends ComputerBuilder {
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void setupMotherboard() {
|
public void setupMotherboard() {
|
||||||
moterboardSetupStatus.add("Screwing the standard motherboard to the case.");
|
motherboardSetupStatus.add("Screwing the standard motherboard to the case.");
|
||||||
moterboardSetupStatus.add("Pluging in the power supply connectors.");
|
motherboardSetupStatus.add("Pluging in the power supply connectors.");
|
||||||
moterboardSetupStatus.forEach(step -> System.out.println(step));
|
motherboardSetupStatus.forEach(step -> System.out.println(step));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
|
|
@ -29,8 +29,8 @@ public class TemplateMethodPatternTest {
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void givenStandardMotherBoard_whenAddingMotherBoard_thenEqualAssertion() {
|
public void givenStandardMotherBoard_whenAddingMotherBoard_thenEqualAssertion() {
|
||||||
standardComputer.addMotherboard();
|
standardComputerBuilder.addMotherboard();
|
||||||
assertEquals("Standard Motherboard", standardComputer.getComputerParts().get("Motherboard"));
|
assertEquals("Standard Motherboard", standardComputerBuilder.getComputerParts().get("Motherboard"));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
|
@ -81,7 +81,6 @@ public class TemplateMethodPatternTest {
|
||||||
highEndComputerBuilder.buildComputer();
|
highEndComputerBuilder.buildComputer();
|
||||||
assertEquals(2, highEndComputerBuilder.getComputerParts().size());
|
assertEquals(2, highEndComputerBuilder.getComputerParts().size());
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void givenAllHighEndParts_whenComputerisBuilt_thenComputerInstance() {
|
public void givenAllHighEndParts_whenComputerisBuilt_thenComputerInstance() {
|
||||||
assertThat(standardComputerBuilder.buildComputer(), instanceOf(Computer.class));
|
assertThat(standardComputerBuilder.buildComputer(), instanceOf(Computer.class));
|
||||||
|
|
Loading…
Reference in New Issue