diff --git a/patterns/template-method/src/main/java/com/baeldung/templatemethodpattern/model/Computer.java b/patterns/template-method/src/main/java/com/baeldung/templatemethodpattern/model/Computer.java index d422204b82..939071d843 100644 --- a/patterns/template-method/src/main/java/com/baeldung/templatemethodpattern/model/Computer.java +++ b/patterns/template-method/src/main/java/com/baeldung/templatemethodpattern/model/Computer.java @@ -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 computerParts = new HashMap<>(); - protected List moterboardSetupStatus = new ArrayList<>(); + private Map computerParts = new HashMap<>(); - public final Computer buildComputer() { - addMotherboard(); - setupMotherboard(); - addProcessor(); - return getComputer(); + public Computer(Map computerParts) { + this.computerParts = computerParts; } - public abstract void addMotherboard(); - - public abstract void setupMotherboard(); - - public abstract void addProcessor(); - - public List getMotherboardSetupStatus() { - return moterboardSetupStatus; - } - public Map getComputerParts() { return computerParts; } - - private Computer getComputer() { - return new Computer(computerParts); - } } diff --git a/patterns/template-method/src/main/java/com/baeldung/templatemethodpattern/model/ComputerBuilder.java b/patterns/template-method/src/main/java/com/baeldung/templatemethodpattern/model/ComputerBuilder.java index f264d33215..7526af4e52 100644 --- a/patterns/template-method/src/main/java/com/baeldung/templatemethodpattern/model/ComputerBuilder.java +++ b/patterns/template-method/src/main/java/com/baeldung/templatemethodpattern/model/ComputerBuilder.java @@ -8,7 +8,7 @@ import java.util.Map; public abstract class ComputerBuilder { protected Map computerParts = new HashMap<>(); - protected List moterboardSetupStatus = new ArrayList<>(); + protected List motherboardSetupStatus = new ArrayList<>(); public final Computer buildComputer() { addMotherboard(); @@ -24,9 +24,9 @@ public abstract class ComputerBuilder { public abstract void addProcessor(); public List getMotherboardSetupStatus() { - return moterboardSetupStatus; + return motherboardSetupStatus; } - + public Map getComputerParts() { return computerParts; } diff --git a/patterns/template-method/src/main/java/com/baeldung/templatemethodpattern/model/HighEndComputer.java b/patterns/template-method/src/main/java/com/baeldung/templatemethodpattern/model/HighEndComputer.java deleted file mode 100644 index 8d80e1e108..0000000000 --- a/patterns/template-method/src/main/java/com/baeldung/templatemethodpattern/model/HighEndComputer.java +++ /dev/null @@ -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"); - } -} diff --git a/patterns/template-method/src/main/java/com/baeldung/templatemethodpattern/model/HighEndComputerBuilder.java b/patterns/template-method/src/main/java/com/baeldung/templatemethodpattern/model/HighEndComputerBuilder.java index cf53a2ae6c..baa800ca8f 100644 --- a/patterns/template-method/src/main/java/com/baeldung/templatemethodpattern/model/HighEndComputerBuilder.java +++ b/patterns/template-method/src/main/java/com/baeldung/templatemethodpattern/model/HighEndComputerBuilder.java @@ -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"); } } diff --git a/patterns/template-method/src/main/java/com/baeldung/templatemethodpattern/model/StandardComputer.java b/patterns/template-method/src/main/java/com/baeldung/templatemethodpattern/model/StandardComputer.java deleted file mode 100644 index 8410ad88ee..0000000000 --- a/patterns/template-method/src/main/java/com/baeldung/templatemethodpattern/model/StandardComputer.java +++ /dev/null @@ -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"); - } -} diff --git a/patterns/template-method/src/main/java/com/baeldung/templatemethodpattern/model/StandardComputerBuilder.java b/patterns/template-method/src/main/java/com/baeldung/templatemethodpattern/model/StandardComputerBuilder.java index 1d9bd0e00f..78547dc38b 100644 --- a/patterns/template-method/src/main/java/com/baeldung/templatemethodpattern/model/StandardComputerBuilder.java +++ b/patterns/template-method/src/main/java/com/baeldung/templatemethodpattern/model/StandardComputerBuilder.java @@ -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 diff --git a/patterns/template-method/src/test/java/com/baeldung/templatemethodpatterntest/TemplateMethodPatternTest.java b/patterns/template-method/src/test/java/com/baeldung/templatemethodpatterntest/TemplateMethodPatternTest.java index df5751fb03..6dc62facc6 100644 --- a/patterns/template-method/src/test/java/com/baeldung/templatemethodpatterntest/TemplateMethodPatternTest.java +++ b/patterns/template-method/src/test/java/com/baeldung/templatemethodpatterntest/TemplateMethodPatternTest.java @@ -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));