From 1f5b0a97494ecf1935704ae68a02a16259e59130 Mon Sep 17 00:00:00 2001 From: Alejandro Gervasio Date: Thu, 2 Nov 2017 20:50:15 -0300 Subject: [PATCH] Implementing the Template Method Pattern in Java - BAEL-1289 (#2923) * Initial Commit * Edit pom.xml * Changed ArtifactId --- patterns/template-method/pom.xml | 21 +++ .../application/Application.java | 18 +++ .../templatemethodpattern/model/Computer.java | 34 +++++ .../model/HighEndComputer.java | 34 +++++ .../model/StandardComputer.java | 34 +++++ .../TemplateMethodPatternTest.java | 120 ++++++++++++++++++ 6 files changed, 261 insertions(+) create mode 100644 patterns/template-method/pom.xml create mode 100644 patterns/template-method/src/main/java/com/baeldung/templatemethodpattern/application/Application.java create mode 100644 patterns/template-method/src/main/java/com/baeldung/templatemethodpattern/model/Computer.java create mode 100644 patterns/template-method/src/main/java/com/baeldung/templatemethodpattern/model/HighEndComputer.java create mode 100644 patterns/template-method/src/main/java/com/baeldung/templatemethodpattern/model/StandardComputer.java create mode 100644 patterns/template-method/src/test/java/com/baeldung/templatemethodpatterntest/TemplateMethodPatternTest.java diff --git a/patterns/template-method/pom.xml b/patterns/template-method/pom.xml new file mode 100644 index 0000000000..c3b6a084ac --- /dev/null +++ b/patterns/template-method/pom.xml @@ -0,0 +1,21 @@ + + + 4.0.0 + com.baeldung.templatemethodpattern + templatemethodpattern + 1.0 + jar + + UTF-8 + 1.8 + 1.8 + + + + junit + junit + 4.12 + test + + + \ No newline at end of file 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 new file mode 100644 index 0000000000..581c774f52 --- /dev/null +++ b/patterns/template-method/src/main/java/com/baeldung/templatemethodpattern/application/Application.java @@ -0,0 +1,18 @@ +package com.baeldung.templatemethodpattern.application; + +import com.baeldung.templatemethodpattern.model.Computer; +import com.baeldung.templatemethodpattern.model.HighEndComputer; +import com.baeldung.templatemethodpattern.model.StandardComputer; + +public class Application { + + public static void main(String[] args) { + Computer standardComputer = new StandardComputer(); + standardComputer.buildComputer(); + standardComputer.getComputerParts().forEach((k, v) -> System.out.println("Part : " + k + " Value : " + v)); + + Computer highEndComputer = new HighEndComputer(); + highEndComputer.buildComputer(); + highEndComputer.getComputerParts().forEach((k, v) -> System.out.println("Part : " + k + " Value : " + v)); + } +} 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 new file mode 100644 index 0000000000..c5d1a2cde8 --- /dev/null +++ b/patterns/template-method/src/main/java/com/baeldung/templatemethodpattern/model/Computer.java @@ -0,0 +1,34 @@ +package com.baeldung.templatemethodpattern.model; + +import java.util.HashMap; +import java.util.Map; + +public abstract class Computer { + + protected Map computerParts = new HashMap<>(); + + public final void buildComputer() { + addMotherboard(); + addProcessor(); + addMemory(); + addHardDrive(); + addGraphicCard(); + addSoundCard(); + } + + public abstract void addProcessor(); + + public abstract void addMotherboard(); + + public abstract void addMemory(); + + public abstract void addHardDrive(); + + public abstract void addGraphicCard(); + + public abstract void addSoundCard(); + + 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 new file mode 100644 index 0000000000..11baeca6f7 --- /dev/null +++ b/patterns/template-method/src/main/java/com/baeldung/templatemethodpattern/model/HighEndComputer.java @@ -0,0 +1,34 @@ +package com.baeldung.templatemethodpattern.model; + +public class HighEndComputer extends Computer { + + @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 new file mode 100644 index 0000000000..22ff370203 --- /dev/null +++ b/patterns/template-method/src/main/java/com/baeldung/templatemethodpattern/model/StandardComputer.java @@ -0,0 +1,34 @@ +package com.baeldung.templatemethodpattern.model; + +public class StandardComputer extends Computer { + + @Override + public void addProcessor() { + computerParts.put("Processor", "Standard Processor"); + } + + @Override + 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 new file mode 100644 index 0000000000..afe66883ac --- /dev/null +++ b/patterns/template-method/src/test/java/com/baeldung/templatemethodpatterntest/TemplateMethodPatternTest.java @@ -0,0 +1,120 @@ +package com.baeldung.templatemethodpatterntest; + +import com.baeldung.templatemethodpattern.model.HighEndComputer; +import com.baeldung.templatemethodpattern.model.StandardComputer; +import org.junit.Assert; +import org.junit.BeforeClass; +import org.junit.Test; + +public class TemplateMethodPatternTest { + + private static StandardComputer standardComputer; + private static HighEndComputer highEndComputer; + + @BeforeClass + public static void setUpStandardComputerInstance() { + standardComputer = new StandardComputer(); + } + + @BeforeClass + public static void setUpHighEndComputerInstance() { + highEndComputer = new HighEndComputer(); + } + + @Test + public void givenStandardProcessor_whenAddingProcessor_thenEqualAssertion() { + standardComputer.addProcessor(); + Assert.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() { + standardComputer.buildComputer(); + Assert.assertEquals(6, standardComputer + .getComputerParts().size()); + } + + @Test + public void givenHightEndProcessor_whenAddingProcessor_thenEqualAssertion() { + highEndComputer.addProcessor(); + 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() { + highEndComputer.buildComputer(); + Assert.assertEquals(6, highEndComputer.getComputerParts().size()); + } +}