Implementing the Template Method Pattern in Java - BAEL-1289 (#2976)

* Initial Commit

* Remove unit test methods

* Remove duplicated unit test methods
This commit is contained in:
Alejandro Gervasio 2017-11-09 07:14:05 -03:00 committed by adamd1985
parent f499f86cc8
commit 8df4293b68
4 changed files with 74 additions and 138 deletions

View File

@ -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<String, String> computerParts = new HashMap<>();
protected List<String> 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<String> getMotherboardSetupStatus() {
return moterboardSetupStatus;
}
public Map<String, String> getComputerParts() {
return computerParts;
}

View File

@ -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");
}
}

View File

@ -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");
}
}

View File

@ -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());
}
}