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:
parent
f499f86cc8
commit
8df4293b68
|
@ -1,32 +1,31 @@
|
||||||
package com.baeldung.templatemethodpattern.model;
|
package com.baeldung.templatemethodpattern.model;
|
||||||
|
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.HashMap;
|
||||||
|
import java.util.List;
|
||||||
import java.util.HashMap;
|
import java.util.HashMap;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
|
|
||||||
public abstract class Computer {
|
public abstract class Computer {
|
||||||
|
|
||||||
protected Map<String, String> computerParts = new HashMap<>();
|
protected Map<String, String> computerParts = new HashMap<>();
|
||||||
|
protected List<String> moterboardSetupStatus = new ArrayList<>();
|
||||||
|
|
||||||
public final void buildComputer() {
|
public final void buildComputer() {
|
||||||
addMotherboard();
|
addMotherboard();
|
||||||
|
setupMotherboard();
|
||||||
addProcessor();
|
addProcessor();
|
||||||
addMemory();
|
|
||||||
addHardDrive();
|
|
||||||
addGraphicCard();
|
|
||||||
addSoundCard();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public abstract void addProcessor();
|
|
||||||
|
|
||||||
public abstract void addMotherboard();
|
public abstract void addMotherboard();
|
||||||
|
|
||||||
public abstract void addMemory();
|
public abstract void setupMotherboard();
|
||||||
|
|
||||||
public abstract void addHardDrive();
|
public abstract void addProcessor();
|
||||||
|
|
||||||
public abstract void addGraphicCard();
|
public List<String> getMotherboardSetupStatus() {
|
||||||
|
return moterboardSetupStatus;
|
||||||
public abstract void addSoundCard();
|
}
|
||||||
|
|
||||||
public Map<String, String> getComputerParts() {
|
public Map<String, String> getComputerParts() {
|
||||||
return computerParts;
|
return computerParts;
|
||||||
|
|
|
@ -2,33 +2,25 @@ package com.baeldung.templatemethodpattern.model;
|
||||||
|
|
||||||
public class HighEndComputer extends Computer {
|
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
|
@Override
|
||||||
public void addProcessor() {
|
public void addProcessor() {
|
||||||
computerParts.put("Processor", "High End Processor");
|
computerParts.put("Processor", "High-end Processor");
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void addMotherboard() {
|
public void addMotherboard() {
|
||||||
computerParts.put("Motherboard", "High End Motherboard");
|
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");
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -2,6 +2,17 @@ package com.baeldung.templatemethodpattern.model;
|
||||||
|
|
||||||
public class StandardComputer extends Computer {
|
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
|
@Override
|
||||||
public void addProcessor() {
|
public void addProcessor() {
|
||||||
computerParts.put("Processor", "Standard Processor");
|
computerParts.put("Processor", "Standard Processor");
|
||||||
|
@ -11,24 +22,4 @@ public class StandardComputer extends Computer {
|
||||||
public void addMotherboard() {
|
public void addMotherboard() {
|
||||||
computerParts.put("Motherboard", "Standard Motherboard");
|
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");
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -3,6 +3,7 @@ package com.baeldung.templatemethodpatterntest;
|
||||||
import com.baeldung.templatemethodpattern.model.HighEndComputer;
|
import com.baeldung.templatemethodpattern.model.HighEndComputer;
|
||||||
import com.baeldung.templatemethodpattern.model.StandardComputer;
|
import com.baeldung.templatemethodpattern.model.StandardComputer;
|
||||||
import org.junit.Assert;
|
import org.junit.Assert;
|
||||||
|
import static org.junit.Assert.assertEquals;
|
||||||
import org.junit.BeforeClass;
|
import org.junit.BeforeClass;
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
|
|
||||||
|
@ -21,100 +22,53 @@ public class TemplateMethodPatternTest {
|
||||||
highEndComputer = new HighEndComputer();
|
highEndComputer = new HighEndComputer();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
|
||||||
public void givenStandardProcessor_whenAddingProcessor_thenEqualAssertion() {
|
|
||||||
standardComputer.addProcessor();
|
|
||||||
Assert.assertEquals("Standard Processor", standardComputer
|
|
||||||
.getComputerParts().get("Processor"));
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void givenStandardMotherBoard_whenAddingMotherBoard_thenEqualAssertion() {
|
public void givenStandardMotherBoard_whenAddingMotherBoard_thenEqualAssertion() {
|
||||||
standardComputer.addMotherboard();
|
standardComputer.addMotherboard();
|
||||||
Assert.assertEquals("Standard Motherboard", standardComputer
|
assertEquals("Standard Motherboard", standardComputer.getComputerParts().get("Motherboard"));
|
||||||
.getComputerParts().get("Motherboard"));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void givenStandardMemory_whenAddingMemory_thenEqualAssertion() {
|
public void givenStandardMotheroboard_whenSetup_thenTwoEqualAssertions() {
|
||||||
standardComputer.addMemory();
|
standardComputer.setupMotherboard();
|
||||||
Assert.assertEquals("8GB", standardComputer
|
assertEquals("Screwing the standard motherboard to the case.", standardComputer.getMotherboardSetupStatus().get(0));
|
||||||
.getComputerParts().get("Memory"));
|
assertEquals("Plugin in the power supply connectors.", standardComputer.getMotherboardSetupStatus().get(1));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void givenStandardHardDrive_whenAddingHardDrive_thenEqualAssertion() {
|
public void givenStandardProcessor_whenAddingProcessor_thenEqualAssertion() {
|
||||||
standardComputer.addHardDrive();
|
standardComputer.addProcessor();
|
||||||
Assert.assertEquals("1TB Hard Drive", standardComputer
|
assertEquals("Standard Processor", standardComputer.getComputerParts().get("Processor"));
|
||||||
.getComputerParts().get("Hard Drive"));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void givenStandardGraphicaCard_whenAddingGraphicCard_thenEqualAssertion() {
|
public void givenAllStandardParts_whenBuildingComputer_thenTwoParts() {
|
||||||
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();
|
standardComputer.buildComputer();
|
||||||
Assert.assertEquals(6, standardComputer
|
assertEquals(2, standardComputer.getComputerParts().size());
|
||||||
.getComputerParts().size());
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
public void givenHightEndProcessor_whenAddingProcessor_thenEqualAssertion() {
|
|
||||||
highEndComputer.addProcessor();
|
|
||||||
Assert.assertEquals("High End Processor", highEndComputer
|
|
||||||
.getComputerParts().get("Processor"));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void givenHighEnddMotherBoard_whenAddingMotherBoard_thenEqualAssertion() {
|
public void givenHighEnddMotherBoard_whenAddingMotherBoard_thenEqualAssertion() {
|
||||||
highEndComputer.addMotherboard();
|
highEndComputer.addMotherboard();
|
||||||
Assert.assertEquals("High End Motherboard", highEndComputer
|
Assert.assertEquals("High-end Motherboard", highEndComputer.getComputerParts().get("Motherboard"));
|
||||||
.getComputerParts().get("Motherboard"));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void givenHighEndMemory_whenAddingMemory_thenEqualAssertion() {
|
public void givenHighEnddMotheroboard_whenSetup_thenTwoEqualAssertions() {
|
||||||
highEndComputer.addMemory();
|
highEndComputer.setupMotherboard();
|
||||||
Assert.assertEquals("16GB", highEndComputer
|
assertEquals("Screwing the high-end motherboard to the case.", highEndComputer.getMotherboardSetupStatus().get(0));
|
||||||
.getComputerParts().get("Memory"));
|
assertEquals("Plugin in the power supply connectors.", highEndComputer.getMotherboardSetupStatus().get(1));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void givenHighEndHardDrive_whenAddingHardDrive_thenEqualAssertion() {
|
public void givenHightEndProcessor_whenAddingProcessor_thenEqualAssertion() {
|
||||||
highEndComputer.addHardDrive();
|
highEndComputer.addProcessor();
|
||||||
Assert.assertEquals("2TB Hard Drive", highEndComputer
|
Assert.assertEquals("High-end Processor", highEndComputer.getComputerParts().get("Processor"));
|
||||||
.getComputerParts().get("Hard Drive"));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void givenHighEndGraphicCard_whenAddingGraphicCard_thenEqualAssertion() {
|
public void givenAllHighEnddParts_whenBuildingComputer_thenTwoParts() {
|
||||||
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();
|
highEndComputer.buildComputer();
|
||||||
Assert.assertEquals(6, highEndComputer.getComputerParts().size());
|
assertEquals(2, highEndComputer.getComputerParts().size());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue