Skip to content

Commit bdbee87

Browse files
Alejandro Gervasioadamd1985
authored andcommitted
Implementing the Template Method Pattern with Java - BAEL-1289 (eugenp#3065)
* Initial Commit * Added Domain Classes * Update HighEndComputer.java * Update StandardComputer.java * Update TemplateMethodPatternTest.java
1 parent 656e89f commit bdbee87

8 files changed

Lines changed: 32 additions & 81 deletions

File tree

‎patterns/template-method/src/main/java/com/baeldung/templatemethodpattern/application/Application.java‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
package com.baeldung.templatemethodpattern.application;
22

33
import com.baeldung.templatemethodpattern.model.Computer;
4+
import com.baeldung.templatemethodpattern.model.StandardComputer;
5+
import com.baeldung.templatemethodpattern.model.HighEndComputer;
46
import com.baeldung.templatemethodpattern.model.ComputerBuilder;
57
import com.baeldung.templatemethodpattern.model.HighEndComputerBuilder;
68
import com.baeldung.templatemethodpattern.model.StandardComputerBuilder;
7-
import com.baeldung.templatemethodpattern.model.HighEndComputer;
8-
import com.baeldung.templatemethodpattern.model.StandardComputer;
99

1010
public class Application {
1111

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

Lines changed: 4 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -5,33 +5,15 @@
55
import java.util.ArrayList;
66
import java.util.List;
77

8-
public abstract class ComputerBuilder {
8+
public class Computer {
99

10-
protected Map<String, String> computerParts = new HashMap<>();
11-
protected List<String> moterboardSetupStatus = new ArrayList<>();
10+
private Map<String, String> computerParts = new HashMap<>();
1211

13-
public final Computer buildComputer() {
14-
addMotherboard();
15-
setupMotherboard();
16-
addProcessor();
17-
return getComputer();
12+
public Computer(Map<String, String> computerParts) {
13+
this.computerParts = computerParts;
1814
}
1915

20-
public abstract void addMotherboard();
21-
22-
public abstract void setupMotherboard();
23-
24-
public abstract void addProcessor();
25-
26-
public List<String> getMotherboardSetupStatus() {
27-
return moterboardSetupStatus;
28-
}
29-
3016
public Map<String, String> getComputerParts() {
3117
return computerParts;
3218
}
33-
34-
private Computer getComputer() {
35-
return new Computer(computerParts);
36-
}
3719
}

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
public abstract class ComputerBuilder {
99

1010
protected Map<String, String> computerParts = new HashMap<>();
11-
protected List<String> moterboardSetupStatus = new ArrayList<>();
11+
protected List<String> motherboardSetupStatus = new ArrayList<>();
1212

1313
public final Computer buildComputer() {
1414
addMotherboard();
@@ -22,11 +22,11 @@ public final Computer buildComputer() {
2222
public abstract void setupMotherboard();
2323

2424
public abstract void addProcessor();
25-
25+
2626
public List<String> getMotherboardSetupStatus() {
27-
return moterboardSetupStatus;
27+
return motherboardSetupStatus;
2828
}
29-
29+
3030
public Map<String, String> getComputerParts() {
3131
return computerParts;
3232
}
Lines changed: 5 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,10 @@
11
package com.baeldung.templatemethodpattern.model;
22

3-
public class HighEndComputer extends Computer {
3+
import java.util.Map;
44

5-
@Override
6-
public void addMotherboard() {
7-
computerParts.put("Motherboard", "High-end Motherboard");
8-
}
9-
10-
@Override
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));
15-
}
16-
17-
@Override
18-
public void addProcessor() {
19-
computerParts.put("Processor", "High-end Processor");
20-
}
5+
public class HighEndComputer extends Computer {
216

22-
@Override
23-
public void addMotherboard() {
24-
computerParts.put("Motherboard", "High End Motherboard");
25-
}
7+
public HighEndComputer(Map<String, String> computerParts) {
8+
super(computerParts);
9+
}
2610
}

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,13 @@ public void addMotherboard() {
99

1010
@Override
1111
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));
12+
motherboardSetupStatus.add("Screwing the high-end motherboard to the case.");
13+
motherboardSetupStatus.add("Pluging in the power supply connectors.");
14+
motherboardSetupStatus.forEach(step -> System.out.println(step));
1515
}
1616

1717
@Override
1818
public void addProcessor() {
19-
computerParts.put("Processor", "High-end Processor");
19+
computerParts.put("Processor", "High-end Processor");
2020
}
2121
}
Lines changed: 6 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,10 @@
11
package com.baeldung.templatemethodpattern.model;
2-
2+
3+
import java.util.Map;
4+
35
public class StandardComputer extends Computer {
46

5-
public void addMotherboard() {
6-
computerParts.put("Motherboard", "Standard Motherboard");
7-
}
8-
9-
@Override
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));
14-
}
15-
16-
@Override
17-
public void addProcessor() {
18-
computerParts.put("Processor", "Standard Processor");
19-
}
20-
21-
@Override
22-
public void addMotherboard() {
23-
computerParts.put("Motherboard", "Standard Motherboard");
24-
}
7+
public StandardComputer(Map<String, String> computerParts) {
8+
super(computerParts);
9+
}
2510
}

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,9 @@ public void addMotherboard() {
99

1010
@Override
1111
public void setupMotherboard() {
12-
moterboardSetupStatus.add("Screwing the standard motherboard to the case.");
13-
moterboardSetupStatus.add("Pluging in the power supply connectors.");
14-
moterboardSetupStatus.forEach(step -> System.out.println(step));
12+
motherboardSetupStatus.add("Screwing the standard motherboard to the case.");
13+
motherboardSetupStatus.add("Pluging in the power supply connectors.");
14+
motherboardSetupStatus.forEach(step -> System.out.println(step));
1515
}
1616

1717
@Override

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,12 +29,12 @@ public static void setUpHighEndComputerBuilderInstance() {
2929

3030
@Test
3131
public void givenStandardMotherBoard_whenAddingMotherBoard_thenEqualAssertion() {
32-
standardComputer.addMotherboard();
33-
assertEquals("Standard Motherboard", standardComputer.getComputerParts().get("Motherboard"));
32+
standardComputerBuilder.addMotherboard();
33+
assertEquals("Standard Motherboard", standardComputerBuilder.getComputerParts().get("Motherboard"));
3434
}
3535

3636
@Test
37-
public void givenStandardMotheroboard_whenSetup_thenTwoEqualAssertions() {
37+
public void givenStandardMotherboard_whenSetup_thenTwoEqualAssertions() {
3838
standardComputerBuilder.setupMotherboard();
3939
assertEquals("Screwing the standard motherboard to the case.", standardComputerBuilder.getMotherboardSetupStatus().get(0));
4040
assertEquals("Pluging in the power supply connectors.", standardComputerBuilder.getMotherboardSetupStatus().get(1));
@@ -77,7 +77,7 @@ public void givenHightEndProcessor_whenAddingProcessor_thenEqualAssertion() {
7777
}
7878

7979
@Test
80-
public void givenAllHighEndParts_whenBuildingComputer_thenTwoParts() {
80+
public void givenAllHighEnddParts_whenBuildingComputer_thenTwoParts() {
8181
highEndComputerBuilder.buildComputer();
8282
assertEquals(2, highEndComputerBuilder.getComputerParts().size());
8383
}

0 commit comments

Comments
 (0)