Implementing the Template Method Pattern with Java - BAEL-1289 (#3065)

* Initial Commit

* Added Domain Classes

* Update HighEndComputer.java

* Update StandardComputer.java

* Update TemplateMethodPatternTest.java
This commit is contained in:
Alejandro Gervasio 2017-11-16 17:28:55 -03:00 committed by adamd1985
parent 656e89f089
commit bdbee879af
8 changed files with 33 additions and 82 deletions

View File

@ -1,11 +1,11 @@
package com.baeldung.templatemethodpattern.application; package com.baeldung.templatemethodpattern.application;
import com.baeldung.templatemethodpattern.model.Computer; import com.baeldung.templatemethodpattern.model.Computer;
import com.baeldung.templatemethodpattern.model.StandardComputer;
import com.baeldung.templatemethodpattern.model.HighEndComputer;
import com.baeldung.templatemethodpattern.model.ComputerBuilder; import com.baeldung.templatemethodpattern.model.ComputerBuilder;
import com.baeldung.templatemethodpattern.model.HighEndComputerBuilder; import com.baeldung.templatemethodpattern.model.HighEndComputerBuilder;
import com.baeldung.templatemethodpattern.model.StandardComputerBuilder; import com.baeldung.templatemethodpattern.model.StandardComputerBuilder;
import com.baeldung.templatemethodpattern.model.HighEndComputer;
import com.baeldung.templatemethodpattern.model.StandardComputer;
public class Application { public class Application {

View File

@ -5,33 +5,15 @@ import java.util.Map;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
public abstract class ComputerBuilder { public class Computer {
protected Map<String, String> computerParts = new HashMap<>(); private Map<String, String> computerParts = new HashMap<>();
protected List<String> moterboardSetupStatus = new ArrayList<>();
public final Computer buildComputer() { public Computer(Map<String, String> computerParts) {
addMotherboard(); this.computerParts = computerParts;
setupMotherboard();
addProcessor();
return getComputer();
} }
public abstract void addMotherboard();
public abstract void setupMotherboard();
public abstract void addProcessor();
public List<String> getMotherboardSetupStatus() {
return moterboardSetupStatus;
}
public Map<String, String> getComputerParts() { public Map<String, String> getComputerParts() {
return computerParts; return computerParts;
} }
private Computer getComputer() {
return new Computer(computerParts);
}
} }

View File

@ -8,7 +8,7 @@ import java.util.Map;
public abstract class ComputerBuilder { public abstract class ComputerBuilder {
protected Map<String, String> computerParts = new HashMap<>(); protected Map<String, String> computerParts = new HashMap<>();
protected List<String> moterboardSetupStatus = new ArrayList<>(); protected List<String> motherboardSetupStatus = new ArrayList<>();
public final Computer buildComputer() { public final Computer buildComputer() {
addMotherboard(); addMotherboard();
@ -22,11 +22,11 @@ public abstract class ComputerBuilder {
public abstract void setupMotherboard(); public abstract void setupMotherboard();
public abstract void addProcessor(); public abstract void addProcessor();
public List<String> getMotherboardSetupStatus() {
return moterboardSetupStatus;
}
public List<String> getMotherboardSetupStatus() {
return motherboardSetupStatus;
}
public Map<String, String> getComputerParts() { public Map<String, String> getComputerParts() {
return computerParts; return computerParts;
} }

View File

@ -1,26 +1,10 @@
package com.baeldung.templatemethodpattern.model; package com.baeldung.templatemethodpattern.model;
public class HighEndComputer extends Computer { import java.util.Map;
@Override public class HighEndComputer extends Computer {
public void addMotherboard() {
computerParts.put("Motherboard", "High-end Motherboard");
}
@Override public HighEndComputer(Map<String, String> computerParts) {
public void setupMotherboard() { super(computerParts);
moterboardSetupStatus.add("Screwing the high-end motherboard to the case."); }
moterboardSetupStatus.add("Pluging in the power supply connectors.");
moterboardSetupStatus.forEach(step -> System.out.println(step));
}
@Override
public void addProcessor() {
computerParts.put("Processor", "High-end Processor");
}
@Override
public void addMotherboard() {
computerParts.put("Motherboard", "High End Motherboard");
}
} }

View File

@ -9,13 +9,13 @@ public class HighEndComputerBuilder extends ComputerBuilder {
@Override @Override
public void setupMotherboard() { public void setupMotherboard() {
moterboardSetupStatus.add("Screwing the high-end motherboard to the case."); motherboardSetupStatus.add("Screwing the high-end motherboard to the case.");
moterboardSetupStatus.add("Pluging in the power supply connectors."); motherboardSetupStatus.add("Pluging in the power supply connectors.");
moterboardSetupStatus.forEach(step -> System.out.println(step)); motherboardSetupStatus.forEach(step -> System.out.println(step));
} }
@Override @Override
public void addProcessor() { public void addProcessor() {
computerParts.put("Processor", "High-end Processor"); computerParts.put("Processor", "High-end Processor");
} }
} }

View File

@ -1,25 +1,10 @@
package com.baeldung.templatemethodpattern.model; package com.baeldung.templatemethodpattern.model;
import java.util.Map;
public class StandardComputer extends Computer { public class StandardComputer extends Computer {
public void addMotherboard() { public StandardComputer(Map<String, String> computerParts) {
computerParts.put("Motherboard", "Standard Motherboard"); super(computerParts);
} }
@Override
public void setupMotherboard() {
moterboardSetupStatus.add("Screwing the standard motherboard to the case.");
moterboardSetupStatus.add("Pluging in the power supply connectors.");
moterboardSetupStatus.forEach(step -> System.out.println(step));
}
@Override
public void addProcessor() {
computerParts.put("Processor", "Standard Processor");
}
@Override
public void addMotherboard() {
computerParts.put("Motherboard", "Standard Motherboard");
}
} }

View File

@ -9,9 +9,9 @@ public class StandardComputerBuilder extends ComputerBuilder {
@Override @Override
public void setupMotherboard() { public void setupMotherboard() {
moterboardSetupStatus.add("Screwing the standard motherboard to the case."); motherboardSetupStatus.add("Screwing the standard motherboard to the case.");
moterboardSetupStatus.add("Pluging in the power supply connectors."); motherboardSetupStatus.add("Pluging in the power supply connectors.");
moterboardSetupStatus.forEach(step -> System.out.println(step)); motherboardSetupStatus.forEach(step -> System.out.println(step));
} }
@Override @Override

View File

@ -29,12 +29,12 @@ public class TemplateMethodPatternTest {
@Test @Test
public void givenStandardMotherBoard_whenAddingMotherBoard_thenEqualAssertion() { public void givenStandardMotherBoard_whenAddingMotherBoard_thenEqualAssertion() {
standardComputer.addMotherboard(); standardComputerBuilder.addMotherboard();
assertEquals("Standard Motherboard", standardComputer.getComputerParts().get("Motherboard")); assertEquals("Standard Motherboard", standardComputerBuilder.getComputerParts().get("Motherboard"));
} }
@Test @Test
public void givenStandardMotheroboard_whenSetup_thenTwoEqualAssertions() { public void givenStandardMotherboard_whenSetup_thenTwoEqualAssertions() {
standardComputerBuilder.setupMotherboard(); standardComputerBuilder.setupMotherboard();
assertEquals("Screwing the standard motherboard to the case.", standardComputerBuilder.getMotherboardSetupStatus().get(0)); assertEquals("Screwing the standard motherboard to the case.", standardComputerBuilder.getMotherboardSetupStatus().get(0));
assertEquals("Pluging in the power supply connectors.", standardComputerBuilder.getMotherboardSetupStatus().get(1)); assertEquals("Pluging in the power supply connectors.", standardComputerBuilder.getMotherboardSetupStatus().get(1));
@ -77,7 +77,7 @@ public class TemplateMethodPatternTest {
} }
@Test @Test
public void givenAllHighEndParts_whenBuildingComputer_thenTwoParts() { public void givenAllHighEnddParts_whenBuildingComputer_thenTwoParts() {
highEndComputerBuilder.buildComputer(); highEndComputerBuilder.buildComputer();
assertEquals(2, highEndComputerBuilder.getComputerParts().size()); assertEquals(2, highEndComputerBuilder.getComputerParts().size());
} }