Skip to content

Commit 8df4293

Browse files
Alejandro Gervasioadamd1985
authored andcommitted
Implementing the Template Method Pattern in Java - BAEL-1289 (eugenp#2976)
* Initial Commit * Remove unit test methods * Remove duplicated unit test methods
1 parent f499f86 commit 8df4293

4 files changed

Lines changed: 61 additions & 125 deletions

File tree

‎patterns/template-method/src/main/java/com/baeldung/templatemethodpattern/model/Computer.java‎

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,32 @@
11
package com.baeldung.templatemethodpattern.model;
22

3+
import java.util.ArrayList;
4+
import java.util.HashMap;
5+
import java.util.List;
36
import java.util.HashMap;
47
import java.util.Map;
58

69
public abstract class Computer {
710

811
protected Map<String, String> computerParts = new HashMap<>();
12+
protected List<String> moterboardSetupStatus = new ArrayList<>();
913

1014
public final void buildComputer() {
11-
addMotherboard();
15+
addMotherboard();
16+
setupMotherboard();
1217
addProcessor();
13-
addMemory();
14-
addHardDrive();
15-
addGraphicCard();
16-
addSoundCard();
1718
}
1819

19-
public abstract void addProcessor();
20-
2120
public abstract void addMotherboard();
2221

23-
public abstract void addMemory();
24-
25-
public abstract void addHardDrive();
22+
public abstract void setupMotherboard();
2623

27-
public abstract void addGraphicCard();
28-
29-
public abstract void addSoundCard();
24+
public abstract void addProcessor();
3025

26+
public List<String> getMotherboardSetupStatus() {
27+
return moterboardSetupStatus;
28+
}
29+
3130
public Map<String, String> getComputerParts() {
3231
return computerParts;
3332
}

‎patterns/template-method/src/main/java/com/baeldung/templatemethodpattern/model/HighEndComputer.java‎

Lines changed: 12 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -2,33 +2,25 @@
22

33
public class HighEndComputer extends Computer {
44

5-
@Override
6-
public void addProcessor() {
7-
computerParts.put("Processor", "High End Processor");
8-
}
9-
105
@Override
116
public void addMotherboard() {
12-
computerParts.put("Motherboard", "High End Motherboard");
7+
computerParts.put("Motherboard", "High-end Motherboard");
138
}
14-
9+
1510
@Override
16-
public void addMemory() {
17-
computerParts.put("Memory", "16GB");
11+
public void setupMotherboard() {
12+
moterboardSetupStatus.add("Screwing the high-end motherboard to the case.");
13+
moterboardSetupStatus.add("Pluging in the power supply connectors.");
14+
moterboardSetupStatus.forEach(step -> System.out.println(step));
1815
}
19-
16+
2017
@Override
21-
public void addHardDrive() {
22-
computerParts.put("Hard Drive", "2TB Hard Drive");
18+
public void addProcessor() {
19+
computerParts.put("Processor", "High-end Processor");
2320
}
24-
21+
2522
@Override
26-
public void addGraphicCard() {
27-
computerParts.put("Graphic Card", "High End Graphic Card");
23+
public void addMotherboard() {
24+
computerParts.put("Motherboard", "High End Motherboard");
2825
}
29-
30-
@Override
31-
public void addSoundCard() {
32-
computerParts.put("Sound Card", "High End Sound Card");
33-
}
3426
}
Lines changed: 11 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,25 @@
11
package com.baeldung.templatemethodpattern.model;
22

33
public class StandardComputer extends Computer {
4-
5-
@Override
6-
public void addProcessor() {
7-
computerParts.put("Processor", "Standard Processor");
8-
}
9-
10-
@Override
4+
115
public void addMotherboard() {
126
computerParts.put("Motherboard", "Standard Motherboard");
137
}
14-
15-
@Override
16-
public void addMemory() {
17-
computerParts.put("Memory", "8GB");
18-
}
19-
8+
209
@Override
21-
public void addHardDrive() {
22-
computerParts.put("Hard Drive", "1TB Hard Drive");
10+
public void setupMotherboard() {
11+
moterboardSetupStatus.add("Screwing the standard motherboard to the case.");
12+
moterboardSetupStatus.add("Pluging in the power supply connectors.");
13+
moterboardSetupStatus.forEach(step -> System.out.println(step));
2314
}
24-
15+
2516
@Override
26-
public void addGraphicCard() {
27-
computerParts.put("Graphic Card", "Standard Graphic Card");
17+
public void addProcessor() {
18+
computerParts.put("Processor", "Standard Processor");
2819
}
2920

3021
@Override
31-
public void addSoundCard() {
32-
computerParts.put("Sound Card", "Standard Sound Card");
22+
public void addMotherboard() {
23+
computerParts.put("Motherboard", "Standard Motherboard");
3324
}
3425
}

‎patterns/template-method/src/test/java/com/baeldung/templatemethodpatterntest/TemplateMethodPatternTest.java‎

Lines changed: 26 additions & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,12 @@
33
import com.baeldung.templatemethodpattern.model.HighEndComputer;
44
import com.baeldung.templatemethodpattern.model.StandardComputer;
55
import org.junit.Assert;
6+
import static org.junit.Assert.assertEquals;
67
import org.junit.BeforeClass;
78
import org.junit.Test;
89

910
public class TemplateMethodPatternTest {
10-
11+
1112
private static StandardComputer standardComputer;
1213
private static HighEndComputer highEndComputer;
1314

@@ -20,101 +21,54 @@ public static void setUpStandardComputerInstance() {
2021
public static void setUpHighEndComputerInstance() {
2122
highEndComputer = new HighEndComputer();
2223
}
23-
24-
@Test
25-
public void givenStandardProcessor_whenAddingProcessor_thenEqualAssertion() {
26-
standardComputer.addProcessor();
27-
Assert.assertEquals("Standard Processor", standardComputer
28-
.getComputerParts().get("Processor"));
29-
}
30-
24+
3125
@Test
3226
public void givenStandardMotherBoard_whenAddingMotherBoard_thenEqualAssertion() {
3327
standardComputer.addMotherboard();
34-
Assert.assertEquals("Standard Motherboard", standardComputer
35-
.getComputerParts().get("Motherboard"));
28+
assertEquals("Standard Motherboard", standardComputer.getComputerParts().get("Motherboard"));
3629
}
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-
30+
5231
@Test
53-
public void givenStandardGraphicaCard_whenAddingGraphicCard_thenEqualAssertion() {
54-
standardComputer.addGraphicCard();
55-
Assert.assertEquals("Standard Graphic Card", standardComputer
56-
.getComputerParts().get("Graphic Card"));
32+
public void givenStandardMotheroboard_whenSetup_thenTwoEqualAssertions() {
33+
standardComputer.setupMotherboard();
34+
assertEquals("Screwing the standard motherboard to the case.", standardComputer.getMotherboardSetupStatus().get(0));
35+
assertEquals("Plugin in the power supply connectors.", standardComputer.getMotherboardSetupStatus().get(1));
5736
}
5837

5938
@Test
60-
public void givenStandardSoundCard_whenAddingSoundCard_thenEqualAssertion() {
61-
standardComputer.addSoundCard();
62-
Assert.assertEquals("Standard Sound Card", standardComputer
63-
.getComputerParts().get("Sound Card"));
39+
public void givenStandardProcessor_whenAddingProcessor_thenEqualAssertion() {
40+
standardComputer.addProcessor();
41+
assertEquals("Standard Processor", standardComputer.getComputerParts().get("Processor"));
6442
}
65-
43+
6644
@Test
67-
public void givenAllStandardParts_whenBuildingComputer_thenSixParts() {
45+
public void givenAllStandardParts_whenBuildingComputer_thenTwoParts() {
6846
standardComputer.buildComputer();
69-
Assert.assertEquals(6, standardComputer
70-
.getComputerParts().size());
47+
assertEquals(2, standardComputer.getComputerParts().size());
7148
}
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-
49+
8050
@Test
8151
public void givenHighEnddMotherBoard_whenAddingMotherBoard_thenEqualAssertion() {
8252
highEndComputer.addMotherboard();
83-
Assert.assertEquals("High End Motherboard", highEndComputer
84-
.getComputerParts().get("Motherboard"));
53+
Assert.assertEquals("High-end Motherboard", highEndComputer.getComputerParts().get("Motherboard"));
8554
}
8655

8756
@Test
88-
public void givenHighEndMemory_whenAddingMemory_thenEqualAssertion() {
89-
highEndComputer.addMemory();
90-
Assert.assertEquals("16GB", highEndComputer
91-
.getComputerParts().get("Memory"));
57+
public void givenHighEnddMotheroboard_whenSetup_thenTwoEqualAssertions() {
58+
highEndComputer.setupMotherboard();
59+
assertEquals("Screwing the high-end motherboard to the case.", highEndComputer.getMotherboardSetupStatus().get(0));
60+
assertEquals("Plugin in the power supply connectors.", highEndComputer.getMotherboardSetupStatus().get(1));
9261
}
9362

9463
@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"));
64+
public void givenHightEndProcessor_whenAddingProcessor_thenEqualAssertion() {
65+
highEndComputer.addProcessor();
66+
Assert.assertEquals("High-end Processor", highEndComputer.getComputerParts().get("Processor"));
11367
}
11468

11569
@Test
116-
public void givenAllHighEndParts_whenBuildingComputer_thenSixParts() {
70+
public void givenAllHighEnddParts_whenBuildingComputer_thenTwoParts() {
11771
highEndComputer.buildComputer();
118-
Assert.assertEquals(6, highEndComputer.getComputerParts().size());
72+
assertEquals(2, highEndComputer.getComputerParts().size());
11973
}
12074
}

0 commit comments

Comments
 (0)