Initial Commit (#2896)
This commit is contained in:
parent
e22b382427
commit
00faa99afc
|
@ -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));
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,34 @@
|
||||||
|
package com.baeldung.templatemethodpattern.model;
|
||||||
|
|
||||||
|
import java.util.HashMap;
|
||||||
|
import java.util.Map;
|
||||||
|
|
||||||
|
public abstract class Computer {
|
||||||
|
|
||||||
|
protected Map<String, String> computerParts = new HashMap<>();
|
||||||
|
|
||||||
|
public final void buildComputer() {
|
||||||
|
addProcessor();
|
||||||
|
addMotherboard();
|
||||||
|
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<String, String> getComputerParts() {
|
||||||
|
return computerParts;
|
||||||
|
}
|
||||||
|
}
|
|
@ -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");
|
||||||
|
}
|
||||||
|
}
|
|
@ -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");
|
||||||
|
}
|
||||||
|
}
|
|
@ -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());
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue