Skip to content

Commit 1f5b0a9

Browse files
Alejandro Gervasioadamd1985
authored andcommitted
Implementing the Template Method Pattern in Java - BAEL-1289 (eugenp#2923)
* Initial Commit * Edit pom.xml * Changed ArtifactId
1 parent f98e868 commit 1f5b0a9

6 files changed

Lines changed: 261 additions & 0 deletions

File tree

‎patterns/template-method/pom.xml‎

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
3+
<modelVersion>4.0.0</modelVersion>
4+
<groupId>com.baeldung.templatemethodpattern</groupId>
5+
<artifactId>templatemethodpattern</artifactId>
6+
<version>1.0</version>
7+
<packaging>jar</packaging>
8+
<properties>
9+
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
10+
<maven.compiler.source>1.8</maven.compiler.source>
11+
<maven.compiler.target>1.8</maven.compiler.target>
12+
</properties>
13+
<dependencies>
14+
<dependency>
15+
<groupId>junit</groupId>
16+
<artifactId>junit</artifactId>
17+
<version>4.12</version>
18+
<scope>test</scope>
19+
</dependency>
20+
</dependencies>
21+
</project>
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package com.baeldung.templatemethodpattern.application;
2+
3+
import com.baeldung.templatemethodpattern.model.Computer;
4+
import com.baeldung.templatemethodpattern.model.HighEndComputer;
5+
import com.baeldung.templatemethodpattern.model.StandardComputer;
6+
7+
public class Application {
8+
9+
public static void main(String[] args) {
10+
Computer standardComputer = new StandardComputer();
11+
standardComputer.buildComputer();
12+
standardComputer.getComputerParts().forEach((k, v) -> System.out.println("Part : " + k + " Value : " + v));
13+
14+
Computer highEndComputer = new HighEndComputer();
15+
highEndComputer.buildComputer();
16+
highEndComputer.getComputerParts().forEach((k, v) -> System.out.println("Part : " + k + " Value : " + v));
17+
}
18+
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package com.baeldung.templatemethodpattern.model;
2+
3+
import java.util.HashMap;
4+
import java.util.Map;
5+
6+
public abstract class Computer {
7+
8+
protected Map<String, String> computerParts = new HashMap<>();
9+
10+
public final void buildComputer() {
11+
addMotherboard();
12+
addProcessor();
13+
addMemory();
14+
addHardDrive();
15+
addGraphicCard();
16+
addSoundCard();
17+
}
18+
19+
public abstract void addProcessor();
20+
21+
public abstract void addMotherboard();
22+
23+
public abstract void addMemory();
24+
25+
public abstract void addHardDrive();
26+
27+
public abstract void addGraphicCard();
28+
29+
public abstract void addSoundCard();
30+
31+
public Map<String, String> getComputerParts() {
32+
return computerParts;
33+
}
34+
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package com.baeldung.templatemethodpattern.model;
2+
3+
public class HighEndComputer extends Computer {
4+
5+
@Override
6+
public void addProcessor() {
7+
computerParts.put("Processor", "High End Processor");
8+
}
9+
10+
@Override
11+
public void addMotherboard() {
12+
computerParts.put("Motherboard", "High End Motherboard");
13+
}
14+
15+
@Override
16+
public void addMemory() {
17+
computerParts.put("Memory", "16GB");
18+
}
19+
20+
@Override
21+
public void addHardDrive() {
22+
computerParts.put("Hard Drive", "2TB Hard Drive");
23+
}
24+
25+
@Override
26+
public void addGraphicCard() {
27+
computerParts.put("Graphic Card", "High End Graphic Card");
28+
}
29+
30+
@Override
31+
public void addSoundCard() {
32+
computerParts.put("Sound Card", "High End Sound Card");
33+
}
34+
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package com.baeldung.templatemethodpattern.model;
2+
3+
public class StandardComputer extends Computer {
4+
5+
@Override
6+
public void addProcessor() {
7+
computerParts.put("Processor", "Standard Processor");
8+
}
9+
10+
@Override
11+
public void addMotherboard() {
12+
computerParts.put("Motherboard", "Standard Motherboard");
13+
}
14+
15+
@Override
16+
public void addMemory() {
17+
computerParts.put("Memory", "8GB");
18+
}
19+
20+
@Override
21+
public void addHardDrive() {
22+
computerParts.put("Hard Drive", "1TB Hard Drive");
23+
}
24+
25+
@Override
26+
public void addGraphicCard() {
27+
computerParts.put("Graphic Card", "Standard Graphic Card");
28+
}
29+
30+
@Override
31+
public void addSoundCard() {
32+
computerParts.put("Sound Card", "Standard Sound Card");
33+
}
34+
}
Lines changed: 120 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,120 @@
1+
package com.baeldung.templatemethodpatterntest;
2+
3+
import com.baeldung.templatemethodpattern.model.HighEndComputer;
4+
import com.baeldung.templatemethodpattern.model.StandardComputer;
5+
import org.junit.Assert;
6+
import org.junit.BeforeClass;
7+
import org.junit.Test;
8+
9+
public class TemplateMethodPatternTest {
10+
11+
private static StandardComputer standardComputer;
12+
private static HighEndComputer highEndComputer;
13+
14+
@BeforeClass
15+
public static void setUpStandardComputerInstance() {
16+
standardComputer = new StandardComputer();
17+
}
18+
19+
@BeforeClass
20+
public static void setUpHighEndComputerInstance() {
21+
highEndComputer = new HighEndComputer();
22+
}
23+
24+
@Test
25+
public void givenStandardProcessor_whenAddingProcessor_thenEqualAssertion() {
26+
standardComputer.addProcessor();
27+
Assert.assertEquals("Standard Processor", standardComputer
28+
.getComputerParts().get("Processor"));
29+
}
30+
31+
@Test
32+
public void givenStandardMotherBoard_whenAddingMotherBoard_thenEqualAssertion() {
33+
standardComputer.addMotherboard();
34+
Assert.assertEquals("Standard Motherboard", standardComputer
35+
.getComputerParts().get("Motherboard"));
36+
}
37+
38+
@Test
39+
public void givenStandardMemory_whenAddingMemory_thenEqualAssertion() {
40+
standardComputer.addMemory();
41+
Assert.assertEquals("8GB", standardComputer
42+
.getComputerParts().get("Memory"));
43+
}
44+
45+
@Test
46+
public void givenStandardHardDrive_whenAddingHardDrive_thenEqualAssertion() {
47+
standardComputer.addHardDrive();
48+
Assert.assertEquals("1TB Hard Drive", standardComputer
49+
.getComputerParts().get("Hard Drive"));
50+
}
51+
52+
@Test
53+
public void givenStandardGraphicaCard_whenAddingGraphicCard_thenEqualAssertion() {
54+
standardComputer.addGraphicCard();
55+
Assert.assertEquals("Standard Graphic Card", standardComputer
56+
.getComputerParts().get("Graphic Card"));
57+
}
58+
59+
@Test
60+
public void givenStandardSoundCard_whenAddingSoundCard_thenEqualAssertion() {
61+
standardComputer.addSoundCard();
62+
Assert.assertEquals("Standard Sound Card", standardComputer
63+
.getComputerParts().get("Sound Card"));
64+
}
65+
66+
@Test
67+
public void givenAllStandardParts_whenBuildingComputer_thenSixParts() {
68+
standardComputer.buildComputer();
69+
Assert.assertEquals(6, standardComputer
70+
.getComputerParts().size());
71+
}
72+
73+
@Test
74+
public void givenHightEndProcessor_whenAddingProcessor_thenEqualAssertion() {
75+
highEndComputer.addProcessor();
76+
Assert.assertEquals("High End Processor", highEndComputer
77+
.getComputerParts().get("Processor"));
78+
}
79+
80+
@Test
81+
public void givenHighEnddMotherBoard_whenAddingMotherBoard_thenEqualAssertion() {
82+
highEndComputer.addMotherboard();
83+
Assert.assertEquals("High End Motherboard", highEndComputer
84+
.getComputerParts().get("Motherboard"));
85+
}
86+
87+
@Test
88+
public void givenHighEndMemory_whenAddingMemory_thenEqualAssertion() {
89+
highEndComputer.addMemory();
90+
Assert.assertEquals("16GB", highEndComputer
91+
.getComputerParts().get("Memory"));
92+
}
93+
94+
@Test
95+
public void givenHighEndHardDrive_whenAddingHardDrive_thenEqualAssertion() {
96+
highEndComputer.addHardDrive();
97+
Assert.assertEquals("2TB Hard Drive", highEndComputer
98+
.getComputerParts().get("Hard Drive"));
99+
}
100+
101+
@Test
102+
public void givenHighEndGraphicCard_whenAddingGraphicCard_thenEqualAssertion() {
103+
highEndComputer.addGraphicCard();
104+
Assert.assertEquals("High End Graphic Card", highEndComputer
105+
.getComputerParts().get("Graphic Card"));
106+
}
107+
108+
@Test
109+
public void givenHighEndSoundCard_whenAddingSoundCard_thenEqualAssertion() {
110+
highEndComputer.addSoundCard();
111+
Assert.assertEquals("High End Sound Card", highEndComputer
112+
.getComputerParts().get("Sound Card"));
113+
}
114+
115+
@Test
116+
public void givenAllHighEndParts_whenBuildingComputer_thenSixParts() {
117+
highEndComputer.buildComputer();
118+
Assert.assertEquals(6, highEndComputer.getComputerParts().size());
119+
}
120+
}

0 commit comments

Comments
 (0)