diff --git a/patterns/template-method/src/main/java/com/baeldung/templatemethodpattern/application/Application.java b/patterns/template-method/src/main/java/com/baeldung/templatemethodpattern/application/Application.java index 5570c2e1dd..bd383b4568 100644 --- a/patterns/template-method/src/main/java/com/baeldung/templatemethodpattern/application/Application.java +++ b/patterns/template-method/src/main/java/com/baeldung/templatemethodpattern/application/Application.java @@ -1,11 +1,11 @@ package com.baeldung.templatemethodpattern.application; import com.baeldung.templatemethodpattern.model.Computer; +import com.baeldung.templatemethodpattern.model.StandardComputer; +import com.baeldung.templatemethodpattern.model.HighEndComputer; import com.baeldung.templatemethodpattern.model.ComputerBuilder; import com.baeldung.templatemethodpattern.model.HighEndComputerBuilder; import com.baeldung.templatemethodpattern.model.StandardComputerBuilder; -import com.baeldung.templatemethodpattern.model.HighEndComputer; -import com.baeldung.templatemethodpattern.model.StandardComputer; public class Application { 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..128eec59ad 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 @@ -5,33 +5,15 @@ 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 9d8eae7f80..39052f4776 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(); @@ -22,11 +22,11 @@ public abstract class ComputerBuilder { public abstract void setupMotherboard(); public abstract void addProcessor(); - - public List getMotherboardSetupStatus() { - return moterboardSetupStatus; - } + public List getMotherboardSetupStatus() { + 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 index 8d80e1e108..16d89f1ad6 100644 --- 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 @@ -1,26 +1,10 @@ package com.baeldung.templatemethodpattern.model; -public class HighEndComputer extends Computer { +import java.util.Map; - @Override - public void addMotherboard() { - computerParts.put("Motherboard", "High-end Motherboard"); - } +public class HighEndComputer extends Computer { - @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"); - } + public HighEndComputer(Map computerParts) { + super(computerParts); + } } 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 index 8410ad88ee..14d32d7b64 100644 --- 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 @@ -1,25 +1,10 @@ package com.baeldung.templatemethodpattern.model; - + +import java.util.Map; + 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"); - } + public StandardComputer(Map computerParts) { + super(computerParts); + } } 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 39888d5ad5..1d608ff2c2 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,12 +29,12 @@ 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 - public void givenStandardMotheroboard_whenSetup_thenTwoEqualAssertions() { + public void givenStandardMotherboard_whenSetup_thenTwoEqualAssertions() { standardComputerBuilder.setupMotherboard(); assertEquals("Screwing the standard motherboard to the case.", standardComputerBuilder.getMotherboardSetupStatus().get(0)); assertEquals("Pluging in the power supply connectors.", standardComputerBuilder.getMotherboardSetupStatus().get(1)); @@ -77,7 +77,7 @@ public class TemplateMethodPatternTest { } @Test - public void givenAllHighEndParts_whenBuildingComputer_thenTwoParts() { + public void givenAllHighEnddParts_whenBuildingComputer_thenTwoParts() { highEndComputerBuilder.buildComputer(); assertEquals(2, highEndComputerBuilder.getComputerParts().size()); }