Skip to content

Commit c0f9c00

Browse files
Alejandro Gervasioadamd1985
authored andcommitted
Implementing the Template Method Pattern with Java - BAEL-1289 (eugenp#3038)
* Initial Commit * Update HighEndComputerBuilder.java * Update HighEndComputerBuilder.java * Update StandardComputerBuilder.java * Update TemplateMethodPatternTest.java * Update ComputerBuilder.java * Delete HighEndComputer.java * Delete StandardComputer.java
1 parent 503fd2d commit c0f9c00

7 files changed

Lines changed: 16 additions & 88 deletions

File tree

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

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

33
import java.util.HashMap;
44
import java.util.Map;
5-
import java.util.ArrayList;
6-
import java.util.List;
75

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

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

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

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-
3014
public Map<String, String> getComputerParts() {
3115
return computerParts;
3216
}
33-
34-
private Computer getComputer() {
35-
return new Computer(computerParts);
36-
}
3717
}

‎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> moterboardSetupStatus = new ArrayList<>();
11+
protected List<String> motherboardSetupStatus = 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 moterboardSetupStatus;
27+
return motherboardSetupStatus;
2828
}
29-
29+
3030
public Map<String, String> getComputerParts() {
3131
return computerParts;
3232
}

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

Lines changed: 0 additions & 26 deletions
This file was deleted.

‎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
}

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

Lines changed: 0 additions & 25 deletions
This file was deleted.

‎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: 2 additions & 3 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-
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
@@ -81,7 +81,6 @@ public void givenAllHighEnddParts_whenBuildingComputer_thenTwoParts() {
8181
highEndComputerBuilder.buildComputer();
8282
assertEquals(2, highEndComputerBuilder.getComputerParts().size());
8383
}
84-
8584
@Test
8685
public void givenAllHighEndParts_whenComputerisBuilt_thenComputerInstance() {
8786
assertThat(standardComputerBuilder.buildComputer(), instanceOf(Computer.class));

0 commit comments

Comments
 (0)