Skip to content

Commit 9685875

Browse files
authored
Revert "Implementing the Template Method Pattern with Java - BAEL-1289 (eugenp#3038)" (eugenp#3042)
This reverts commit c0f9c00.
1 parent af50b1e commit 9685875

7 files changed

Lines changed: 88 additions & 16 deletions

File tree

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

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,36 @@
22

33
import java.util.HashMap;
44
import java.util.Map;
5+
import java.util.ArrayList;
6+
import java.util.List;
57

6-
public class Computer {
8+
public abstract class ComputerBuilder {
79

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

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

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+
1430
public Map<String, String> getComputerParts() {
1531
return computerParts;
1632
}
33+
34+
private Computer getComputer() {
35+
return new Computer(computerParts);
36+
}
1737
}

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

Lines changed: 3 additions & 3 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> motherboardSetupStatus = new ArrayList<>();
11+
protected List<String> moterboardSetupStatus = new ArrayList<>();
1212

1313
public final Computer buildComputer() {
1414
addMotherboard();
@@ -24,9 +24,9 @@ public final Computer buildComputer() {
2424
public abstract void addProcessor();
2525

2626
public List<String> getMotherboardSetupStatus() {
27-
return motherboardSetupStatus;
27+
return moterboardSetupStatus;
2828
}
29-
29+
3030
public Map<String, String> getComputerParts() {
3131
return computerParts;
3232
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package com.baeldung.templatemethodpattern.model;
2+
3+
public class HighEndComputer extends Computer {
4+
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+
}
21+
22+
@Override
23+
public void addMotherboard() {
24+
computerParts.put("Motherboard", "High End Motherboard");
25+
}
26+
}

‎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-
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));
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));
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: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package com.baeldung.templatemethodpattern.model;
2+
3+
public class StandardComputer extends Computer {
4+
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+
}
25+
}

‎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-
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));
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));
1515
}
1616

1717
@Override

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

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

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

3636
@Test
@@ -81,6 +81,7 @@ public void givenAllHighEnddParts_whenBuildingComputer_thenTwoParts() {
8181
highEndComputerBuilder.buildComputer();
8282
assertEquals(2, highEndComputerBuilder.getComputerParts().size());
8383
}
84+
8485
@Test
8586
public void givenAllHighEndParts_whenComputerisBuilt_thenComputerInstance() {
8687
assertThat(standardComputerBuilder.buildComputer(), instanceOf(Computer.class));

0 commit comments

Comments
 (0)