From 8df4293b6830dccaa3457b54d21351afc345c23a Mon Sep 17 00:00:00 2001 From: Alejandro Gervasio Date: Thu, 9 Nov 2017 07:14:05 -0300 Subject: [PATCH] Implementing the Template Method Pattern in Java - BAEL-1289 (#2976) * Initial Commit * Remove unit test methods * Remove duplicated unit test methods --- .../templatemethodpattern/model/Computer.java | 25 ++-- .../model/HighEndComputer.java | 38 +++--- .../model/StandardComputer.java | 33 ++--- .../TemplateMethodPatternTest.java | 116 ++++++------------ 4 files changed, 74 insertions(+), 138 deletions(-) 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 c5d1a2cde8..f18485428f 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 @@ -1,33 +1,32 @@ package com.baeldung.templatemethodpattern.model; +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; import java.util.HashMap; import java.util.Map; public abstract class Computer { protected Map computerParts = new HashMap<>(); + protected List moterboardSetupStatus = new ArrayList<>(); public final void buildComputer() { - addMotherboard(); + addMotherboard(); + setupMotherboard(); addProcessor(); - addMemory(); - addHardDrive(); - addGraphicCard(); - addSoundCard(); } - public abstract void addProcessor(); - public abstract void addMotherboard(); - public abstract void addMemory(); + public abstract void setupMotherboard(); - public abstract void addHardDrive(); - - public abstract void addGraphicCard(); - - public abstract void addSoundCard(); + public abstract void addProcessor(); + public List getMotherboardSetupStatus() { + return moterboardSetupStatus; + } + 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 11baeca6f7..8d80e1e108 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 @@ -3,32 +3,24 @@ package com.baeldung.templatemethodpattern.model; public class HighEndComputer extends Computer { @Override - public void addProcessor() { - computerParts.put("Processor", "High End Processor"); + 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"); } - - @Override - public void addMemory() { - computerParts.put("Memory", "16GB"); - } - - @Override - public void addHardDrive() { - computerParts.put("Hard Drive", "2TB Hard Drive"); - } - - @Override - public void addGraphicCard() { - computerParts.put("Graphic Card", "High End Graphic Card"); - } - - @Override - public void addSoundCard() { - computerParts.put("Sound Card", "High End Sound Card"); - } } 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 22ff370203..8410ad88ee 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,7 +1,18 @@ 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"); @@ -11,24 +22,4 @@ public class StandardComputer extends Computer { public void addMotherboard() { computerParts.put("Motherboard", "Standard Motherboard"); } - - @Override - public void addMemory() { - computerParts.put("Memory", "8GB"); - } - - @Override - public void addHardDrive() { - computerParts.put("Hard Drive", "1TB Hard Drive"); - } - - @Override - public void addGraphicCard() { - computerParts.put("Graphic Card", "Standard Graphic Card"); - } - - @Override - public void addSoundCard() { - computerParts.put("Sound Card", "Standard Sound Card"); - } } 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 afe66883ac..526873c4f2 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 @@ -3,11 +3,12 @@ package com.baeldung.templatemethodpatterntest; import com.baeldung.templatemethodpattern.model.HighEndComputer; import com.baeldung.templatemethodpattern.model.StandardComputer; import org.junit.Assert; +import static org.junit.Assert.assertEquals; import org.junit.BeforeClass; import org.junit.Test; public class TemplateMethodPatternTest { - + private static StandardComputer standardComputer; private static HighEndComputer highEndComputer; @@ -20,101 +21,54 @@ public class TemplateMethodPatternTest { public static void setUpHighEndComputerInstance() { highEndComputer = new HighEndComputer(); } + + @Test + public void givenStandardMotherBoard_whenAddingMotherBoard_thenEqualAssertion() { + standardComputer.addMotherboard(); + assertEquals("Standard Motherboard", standardComputer.getComputerParts().get("Motherboard")); + } + + @Test + public void givenStandardMotheroboard_whenSetup_thenTwoEqualAssertions() { + standardComputer.setupMotherboard(); + assertEquals("Screwing the standard motherboard to the case.", standardComputer.getMotherboardSetupStatus().get(0)); + assertEquals("Plugin in the power supply connectors.", standardComputer.getMotherboardSetupStatus().get(1)); + } @Test public void givenStandardProcessor_whenAddingProcessor_thenEqualAssertion() { standardComputer.addProcessor(); - Assert.assertEquals("Standard Processor", standardComputer - .getComputerParts().get("Processor")); + assertEquals("Standard Processor", standardComputer.getComputerParts().get("Processor")); } - + @Test - public void givenStandardMotherBoard_whenAddingMotherBoard_thenEqualAssertion() { - standardComputer.addMotherboard(); - Assert.assertEquals("Standard Motherboard", standardComputer - .getComputerParts().get("Motherboard")); - } - - @Test - public void givenStandardMemory_whenAddingMemory_thenEqualAssertion() { - standardComputer.addMemory(); - Assert.assertEquals("8GB", standardComputer - .getComputerParts().get("Memory")); - } - - @Test - public void givenStandardHardDrive_whenAddingHardDrive_thenEqualAssertion() { - standardComputer.addHardDrive(); - Assert.assertEquals("1TB Hard Drive", standardComputer - .getComputerParts().get("Hard Drive")); - } - - @Test - public void givenStandardGraphicaCard_whenAddingGraphicCard_thenEqualAssertion() { - standardComputer.addGraphicCard(); - Assert.assertEquals("Standard Graphic Card", standardComputer - .getComputerParts().get("Graphic Card")); - } - - @Test - public void givenStandardSoundCard_whenAddingSoundCard_thenEqualAssertion() { - standardComputer.addSoundCard(); - Assert.assertEquals("Standard Sound Card", standardComputer - .getComputerParts().get("Sound Card")); - } - - @Test - public void givenAllStandardParts_whenBuildingComputer_thenSixParts() { + public void givenAllStandardParts_whenBuildingComputer_thenTwoParts() { standardComputer.buildComputer(); - Assert.assertEquals(6, standardComputer - .getComputerParts().size()); + assertEquals(2, standardComputer.getComputerParts().size()); + } + + @Test + public void givenHighEnddMotherBoard_whenAddingMotherBoard_thenEqualAssertion() { + highEndComputer.addMotherboard(); + Assert.assertEquals("High-end Motherboard", highEndComputer.getComputerParts().get("Motherboard")); + } + + @Test + public void givenHighEnddMotheroboard_whenSetup_thenTwoEqualAssertions() { + highEndComputer.setupMotherboard(); + assertEquals("Screwing the high-end motherboard to the case.", highEndComputer.getMotherboardSetupStatus().get(0)); + assertEquals("Plugin in the power supply connectors.", highEndComputer.getMotherboardSetupStatus().get(1)); } @Test public void givenHightEndProcessor_whenAddingProcessor_thenEqualAssertion() { highEndComputer.addProcessor(); - Assert.assertEquals("High End Processor", highEndComputer - .getComputerParts().get("Processor")); + Assert.assertEquals("High-end Processor", highEndComputer.getComputerParts().get("Processor")); } @Test - public void givenHighEnddMotherBoard_whenAddingMotherBoard_thenEqualAssertion() { - highEndComputer.addMotherboard(); - Assert.assertEquals("High End Motherboard", highEndComputer - .getComputerParts().get("Motherboard")); - } - - @Test - public void givenHighEndMemory_whenAddingMemory_thenEqualAssertion() { - highEndComputer.addMemory(); - Assert.assertEquals("16GB", highEndComputer - .getComputerParts().get("Memory")); - } - - @Test - public void givenHighEndHardDrive_whenAddingHardDrive_thenEqualAssertion() { - highEndComputer.addHardDrive(); - Assert.assertEquals("2TB Hard Drive", highEndComputer - .getComputerParts().get("Hard Drive")); - } - - @Test - public void givenHighEndGraphicCard_whenAddingGraphicCard_thenEqualAssertion() { - highEndComputer.addGraphicCard(); - Assert.assertEquals("High End Graphic Card", highEndComputer - .getComputerParts().get("Graphic Card")); - } - - @Test - public void givenHighEndSoundCard_whenAddingSoundCard_thenEqualAssertion() { - highEndComputer.addSoundCard(); - Assert.assertEquals("High End Sound Card", highEndComputer - .getComputerParts().get("Sound Card")); - } - - @Test - public void givenAllHighEndParts_whenBuildingComputer_thenSixParts() { + public void givenAllHighEnddParts_whenBuildingComputer_thenTwoParts() { highEndComputer.buildComputer(); - Assert.assertEquals(6, highEndComputer.getComputerParts().size()); + assertEquals(2, highEndComputer.getComputerParts().size()); } }