File tree Expand file tree Collapse file tree
patterns/template-method/src
main/java/com/baeldung/templatemethodpattern
test/java/com/baeldung/templatemethodpatterntest Expand file tree Collapse file tree Original file line number Diff line number Diff line change 11package com .baeldung .templatemethodpattern .application ;
22
33import com .baeldung .templatemethodpattern .model .Computer ;
4+ import com .baeldung .templatemethodpattern .model .StandardComputer ;
5+ import com .baeldung .templatemethodpattern .model .HighEndComputer ;
46import com .baeldung .templatemethodpattern .model .ComputerBuilder ;
57import com .baeldung .templatemethodpattern .model .HighEndComputerBuilder ;
68import com .baeldung .templatemethodpattern .model .StandardComputerBuilder ;
7- import com .baeldung .templatemethodpattern .model .HighEndComputer ;
8- import com .baeldung .templatemethodpattern .model .StandardComputer ;
99
1010public class Application {
1111
Original file line number Diff line number Diff line change 55import java .util .ArrayList ;
66import 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}
Original file line number Diff line number Diff line change 88public 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 }
Original file line number Diff line number Diff line change 11package 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}
Original file line number Diff line number Diff 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}
Original file line number Diff line number Diff line change 11package com .baeldung .templatemethodpattern .model ;
2-
2+
3+ import java .util .Map ;
4+
35public 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}
Original file line number Diff line number Diff 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
Original file line number Diff line number Diff 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 }
You can’t perform that action at this time.
0 commit comments