This reverts commit c0f9c00a3b
.
This commit is contained in:
parent
af50b1e59c
commit
9685875496
|
@ -2,16 +2,36 @@ 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 class Computer {
|
public abstract class ComputerBuilder {
|
||||||
|
|
||||||
private Map<String, String> computerParts = new HashMap<>();
|
protected Map<String, String> computerParts = new HashMap<>();
|
||||||
|
protected List<String> moterboardSetupStatus = new ArrayList<>();
|
||||||
|
|
||||||
public Computer(Map<String, String> computerParts) {
|
public final Computer buildComputer() {
|
||||||
this.computerParts = computerParts;
|
addMotherboard();
|
||||||
|
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> motherboardSetupStatus = new ArrayList<>();
|
protected List<String> moterboardSetupStatus = 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 motherboardSetupStatus;
|
return moterboardSetupStatus;
|
||||||
}
|
}
|
||||||
|
|
||||||
public Map<String, String> getComputerParts() {
|
public Map<String, String> getComputerParts() {
|
||||||
|
|
|
@ -0,0 +1,26 @@
|
||||||
|
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,13 +9,13 @@ public class HighEndComputerBuilder extends ComputerBuilder {
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void setupMotherboard() {
|
public void setupMotherboard() {
|
||||||
motherboardSetupStatus.add("Screwing the high-end motherboard to the case.");
|
moterboardSetupStatus.add("Screwing the high-end motherboard to the case.");
|
||||||
motherboardSetupStatus.add("Pluging in the power supply connectors.");
|
moterboardSetupStatus.add("Pluging in the power supply connectors.");
|
||||||
motherboardSetupStatus.forEach(step -> System.out.println(step));
|
moterboardSetupStatus.forEach(step -> System.out.println(step));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void addProcessor() {
|
public void addProcessor() {
|
||||||
computerParts.put("Processor", "High-end Processor");
|
computerParts.put("Processor", "High-end Processor");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -0,0 +1,25 @@
|
||||||
|
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() {
|
||||||
motherboardSetupStatus.add("Screwing the standard motherboard to the case.");
|
moterboardSetupStatus.add("Screwing the standard motherboard to the case.");
|
||||||
motherboardSetupStatus.add("Pluging in the power supply connectors.");
|
moterboardSetupStatus.add("Pluging in the power supply connectors.");
|
||||||
motherboardSetupStatus.forEach(step -> System.out.println(step));
|
moterboardSetupStatus.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() {
|
||||||
standardComputerBuilder.addMotherboard();
|
standardComputer.addMotherboard();
|
||||||
assertEquals("Standard Motherboard", standardComputerBuilder.getComputerParts().get("Motherboard"));
|
assertEquals("Standard Motherboard", standardComputer.getComputerParts().get("Motherboard"));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
|
@ -81,6 +81,7 @@ 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