Skip to content

Commit 10ea5fa

Browse files
Alejandro Gervasiopivovarit
authored andcommitted
Implemeting the template method pattern in Java - BAEL-1289 (eugenp#3012)
* Initial Commit * Delete domain classes
1 parent 294a055 commit 10ea5fa

6 files changed

Lines changed: 143 additions & 42 deletions

File tree

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

33
import com.baeldung.templatemethodpattern.model.Computer;
4+
import com.baeldung.templatemethodpattern.model.ComputerBuilder;
5+
import com.baeldung.templatemethodpattern.model.HighEndComputerBuilder;
6+
import com.baeldung.templatemethodpattern.model.StandardComputerBuilder;
47
import com.baeldung.templatemethodpattern.model.HighEndComputer;
58
import com.baeldung.templatemethodpattern.model.StandardComputer;
69

710
public class Application {
811

912
public static void main(String[] args) {
10-
Computer standardComputer = new StandardComputer();
11-
standardComputer.buildComputer();
13+
ComputerBuilder standardComputerBuilder = new StandardComputerBuilder();
14+
Computer standardComputer = standardComputerBuilder.buildComputer();
1215
standardComputer.getComputerParts().forEach((k, v) -> System.out.println("Part : " + k + " Value : " + v));
1316

14-
Computer highEndComputer = new HighEndComputer();
15-
highEndComputer.buildComputer();
17+
ComputerBuilder highEndComputerBuilder = new HighEndComputerBuilder();
18+
Computer highEndComputer = highEndComputerBuilder.buildComputer();
1619
highEndComputer.getComputerParts().forEach((k, v) -> System.out.println("Part : " + k + " Value : " + v));
1720
}
1821
}
Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,20 @@
11
package com.baeldung.templatemethodpattern.model;
22

3-
import java.util.ArrayList;
4-
import java.util.HashMap;
5-
import java.util.List;
63
import java.util.HashMap;
74
import java.util.Map;
5+
import java.util.ArrayList;
6+
import java.util.List;
87

9-
public abstract class Computer {
8+
public abstract class ComputerBuilder {
109

1110
protected Map<String, String> computerParts = new HashMap<>();
1211
protected List<String> moterboardSetupStatus = new ArrayList<>();
1312

14-
public final void buildComputer() {
15-
addMotherboard();
16-
setupMotherboard();
17-
addProcessor();
13+
public final Computer buildComputer() {
14+
addMotherboard();
15+
setupMotherboard();
16+
addProcessor();
17+
return getComputer();
1818
}
1919

2020
public abstract void addMotherboard();
@@ -29,5 +29,9 @@ public List<String> getMotherboardSetupStatus() {
2929

3030
public Map<String, String> getComputerParts() {
3131
return computerParts;
32-
}
32+
}
33+
34+
private Computer getComputer() {
35+
return new Computer(computerParts);
36+
}
3337
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package com.baeldung.templatemethodpattern.model;
2+
3+
import java.util.ArrayList;
4+
import java.util.HashMap;
5+
import java.util.List;
6+
import java.util.Map;
7+
8+
public abstract class ComputerBuilder {
9+
10+
protected Map<String, String> computerParts = new HashMap<>();
11+
protected List<String> moterboardSetupStatus = new ArrayList<>();
12+
13+
public final Computer buildComputer() {
14+
addMotherboard();
15+
setupMotherboard();
16+
addProcessor();
17+
return getComputer();
18+
}
19+
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+
30+
public Map<String, String> getComputerParts() {
31+
return computerParts;
32+
}
33+
34+
private Computer getComputer() {
35+
return new Computer(computerParts);
36+
}
37+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package com.baeldung.templatemethodpattern.model;
2+
3+
public class HighEndComputerBuilder extends ComputerBuilder {
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+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package com.baeldung.templatemethodpattern.model;
2+
3+
public class StandardComputerBuilder extends ComputerBuilder {
4+
5+
@Override
6+
public void addMotherboard() {
7+
computerParts.put("Motherboard", "Standard Motherboard");
8+
}
9+
10+
@Override
11+
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));
15+
}
16+
17+
@Override
18+
public void addProcessor() {
19+
computerParts.put("Processor", "Standard Processor");
20+
}
21+
}
Lines changed: 44 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,30 @@
11
package com.baeldung.templatemethodpatterntest;
22

3+
import com.baeldung.templatemethodpattern.model.Computer;
4+
import com.baeldung.templatemethodpattern.model.HighEndComputerBuilder;
5+
import com.baeldung.templatemethodpattern.model.StandardComputerBuilder;
36
import com.baeldung.templatemethodpattern.model.HighEndComputer;
47
import com.baeldung.templatemethodpattern.model.StandardComputer;
58
import org.junit.Assert;
69
import static org.junit.Assert.assertEquals;
710
import org.junit.BeforeClass;
811
import org.junit.Test;
12+
import static org.hamcrest.CoreMatchers.instanceOf;
13+
import static org.junit.Assert.assertThat;
914

1015
public class TemplateMethodPatternTest {
1116

12-
private static StandardComputer standardComputer;
13-
private static HighEndComputer highEndComputer;
14-
17+
private static StandardComputerBuilder standardComputerBuilder;
18+
private static HighEndComputerBuilder highEndComputerBuilder;
19+
1520
@BeforeClass
16-
public static void setUpStandardComputerInstance() {
17-
standardComputer = new StandardComputer();
21+
public static void setUpStandardComputerBuilderInstance() {
22+
standardComputerBuilder = new StandardComputerBuilder();
1823
}
19-
24+
2025
@BeforeClass
21-
public static void setUpHighEndComputerInstance() {
22-
highEndComputer = new HighEndComputer();
26+
public static void setUpHighEndComputerBuilderInstance() {
27+
highEndComputerBuilder = new HighEndComputerBuilder();
2328
}
2429

2530
@Test
@@ -30,45 +35,55 @@ public void givenStandardMotherBoard_whenAddingMotherBoard_thenEqualAssertion()
3035

3136
@Test
3237
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));
38+
standardComputerBuilder.setupMotherboard();
39+
assertEquals("Screwing the standard motherboard to the case.", standardComputerBuilder.getMotherboardSetupStatus().get(0));
40+
assertEquals("Pluging in the power supply connectors.", standardComputerBuilder.getMotherboardSetupStatus().get(1));
3641
}
37-
42+
3843
@Test
3944
public void givenStandardProcessor_whenAddingProcessor_thenEqualAssertion() {
40-
standardComputer.addProcessor();
41-
assertEquals("Standard Processor", standardComputer.getComputerParts().get("Processor"));
45+
standardComputerBuilder.addProcessor();
46+
assertEquals("Standard Processor", standardComputerBuilder.getComputerParts().get("Processor"));
4247
}
4348

4449
@Test
4550
public void givenAllStandardParts_whenBuildingComputer_thenTwoParts() {
46-
standardComputer.buildComputer();
47-
assertEquals(2, standardComputer.getComputerParts().size());
51+
standardComputerBuilder.buildComputer();
52+
assertEquals(2, standardComputerBuilder.getComputerParts().size());
4853
}
49-
54+
5055
@Test
51-
public void givenHighEnddMotherBoard_whenAddingMotherBoard_thenEqualAssertion() {
52-
highEndComputer.addMotherboard();
53-
Assert.assertEquals("High-end Motherboard", highEndComputer.getComputerParts().get("Motherboard"));
56+
public void givenAllStandardParts_whenComputerisBuilt_thenComputerInstance() {
57+
assertThat(standardComputerBuilder.buildComputer(), instanceOf(Computer.class));
5458
}
5559

60+
@Test
61+
public void givenHighEnddMotherBoard_whenAddingMotherBoard_thenEqualAssertion() {
62+
highEndComputerBuilder.addMotherboard();
63+
Assert.assertEquals("High-end Motherboard", highEndComputerBuilder.getComputerParts().get("Motherboard"));
64+
}
65+
5666
@Test
5767
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));
68+
highEndComputerBuilder.setupMotherboard();
69+
assertEquals("Screwing the high-end motherboard to the case.", highEndComputerBuilder.getMotherboardSetupStatus().get(0));
70+
assertEquals("Pluging in the power supply connectors.", highEndComputerBuilder.getMotherboardSetupStatus().get(1));
6171
}
62-
72+
6373
@Test
6474
public void givenHightEndProcessor_whenAddingProcessor_thenEqualAssertion() {
65-
highEndComputer.addProcessor();
66-
Assert.assertEquals("High-end Processor", highEndComputer.getComputerParts().get("Processor"));
75+
highEndComputerBuilder.addProcessor();
76+
assertEquals("High-end Processor", highEndComputerBuilder.getComputerParts().get("Processor"));
6777
}
6878

6979
@Test
7080
public void givenAllHighEnddParts_whenBuildingComputer_thenTwoParts() {
71-
highEndComputer.buildComputer();
72-
assertEquals(2, highEndComputer.getComputerParts().size());
73-
}
81+
highEndComputerBuilder.buildComputer();
82+
assertEquals(2, highEndComputerBuilder.getComputerParts().size());
83+
}
84+
85+
@Test
86+
public void givenAllHighEndParts_whenComputerisBuilt_thenComputerInstance() {
87+
assertThat(standardComputerBuilder.buildComputer(), instanceOf(Computer.class));
88+
}
7489
}

0 commit comments

Comments
 (0)