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:
parent
656e89f089
commit
bdbee879af
@ -1,11 +1,11 @@
|
||||
package com.baeldung.templatemethodpattern.application;
|
||||
|
||||
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.HighEndComputerBuilder;
|
||||
import com.baeldung.templatemethodpattern.model.StandardComputerBuilder;
|
||||
import com.baeldung.templatemethodpattern.model.HighEndComputer;
|
||||
import com.baeldung.templatemethodpattern.model.StandardComputer;
|
||||
|
||||
public class Application {
|
||||
|
||||
|
@ -5,33 +5,15 @@ import java.util.Map;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
public abstract class ComputerBuilder {
|
||||
public class Computer {
|
||||
|
||||
protected Map<String, String> computerParts = new HashMap<>();
|
||||
protected List<String> moterboardSetupStatus = new ArrayList<>();
|
||||
private Map<String, String> computerParts = new HashMap<>();
|
||||
|
||||
public final Computer buildComputer() {
|
||||
addMotherboard();
|
||||
setupMotherboard();
|
||||
addProcessor();
|
||||
return getComputer();
|
||||
public Computer(Map<String, String> computerParts) {
|
||||
this.computerParts = computerParts;
|
||||
}
|
||||
|
||||
public abstract void addMotherboard();
|
||||
|
||||
public abstract void setupMotherboard();
|
||||
|
||||
public abstract void addProcessor();
|
||||
|
||||
public List<String> getMotherboardSetupStatus() {
|
||||
return moterboardSetupStatus;
|
||||
}
|
||||
|
||||
public Map<String, String> getComputerParts() {
|
||||
return computerParts;
|
||||
}
|
||||
|
||||
private Computer getComputer() {
|
||||
return new Computer(computerParts);
|
||||
}
|
||||
}
|
||||
|
@ -8,7 +8,7 @@ import java.util.Map;
|
||||
public abstract class ComputerBuilder {
|
||||
|
||||
protected Map<String, String> computerParts = new HashMap<>();
|
||||
protected List<String> moterboardSetupStatus = new ArrayList<>();
|
||||
protected List<String> motherboardSetupStatus = new ArrayList<>();
|
||||
|
||||
public final Computer buildComputer() {
|
||||
addMotherboard();
|
||||
@ -22,11 +22,11 @@ public abstract class ComputerBuilder {
|
||||
public abstract void setupMotherboard();
|
||||
|
||||
public abstract void addProcessor();
|
||||
|
||||
public List<String> getMotherboardSetupStatus() {
|
||||
return moterboardSetupStatus;
|
||||
}
|
||||
|
||||
public List<String> getMotherboardSetupStatus() {
|
||||
return motherboardSetupStatus;
|
||||
}
|
||||
|
||||
public Map<String, String> getComputerParts() {
|
||||
return computerParts;
|
||||
}
|
||||
|
@ -1,26 +1,10 @@
|
||||
package com.baeldung.templatemethodpattern.model;
|
||||
|
||||
public class HighEndComputer extends Computer {
|
||||
import java.util.Map;
|
||||
|
||||
@Override
|
||||
public void addMotherboard() {
|
||||
computerParts.put("Motherboard", "High-end Motherboard");
|
||||
}
|
||||
public class HighEndComputer extends Computer {
|
||||
|
||||
@Override
|
||||
public void setupMotherboard() {
|
||||
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");
|
||||
}
|
||||
public HighEndComputer(Map<String, String> computerParts) {
|
||||
super(computerParts);
|
||||
}
|
||||
}
|
||||
|
@ -9,13 +9,13 @@ public class HighEndComputerBuilder extends ComputerBuilder {
|
||||
|
||||
@Override
|
||||
public void setupMotherboard() {
|
||||
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));
|
||||
motherboardSetupStatus.add("Screwing the high-end motherboard to the case.");
|
||||
motherboardSetupStatus.add("Pluging in the power supply connectors.");
|
||||
motherboardSetupStatus.forEach(step -> System.out.println(step));
|
||||
}
|
||||
|
||||
@Override
|
||||
public void addProcessor() {
|
||||
computerParts.put("Processor", "High-end Processor");
|
||||
computerParts.put("Processor", "High-end Processor");
|
||||
}
|
||||
}
|
||||
|
@ -1,25 +1,10 @@
|
||||
package com.baeldung.templatemethodpattern.model;
|
||||
|
||||
|
||||
import java.util.Map;
|
||||
|
||||
public class StandardComputer extends Computer {
|
||||
|
||||
public void addMotherboard() {
|
||||
computerParts.put("Motherboard", "Standard Motherboard");
|
||||
}
|
||||
|
||||
@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");
|
||||
}
|
||||
public StandardComputer(Map<String, String> computerParts) {
|
||||
super(computerParts);
|
||||
}
|
||||
}
|
||||
|
@ -9,9 +9,9 @@ public class StandardComputerBuilder extends ComputerBuilder {
|
||||
|
||||
@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));
|
||||
motherboardSetupStatus.add("Screwing the standard motherboard to the case.");
|
||||
motherboardSetupStatus.add("Pluging in the power supply connectors.");
|
||||
motherboardSetupStatus.forEach(step -> System.out.println(step));
|
||||
}
|
||||
|
||||
@Override
|
||||
|
@ -29,12 +29,12 @@ public class TemplateMethodPatternTest {
|
||||
|
||||
@Test
|
||||
public void givenStandardMotherBoard_whenAddingMotherBoard_thenEqualAssertion() {
|
||||
standardComputer.addMotherboard();
|
||||
assertEquals("Standard Motherboard", standardComputer.getComputerParts().get("Motherboard"));
|
||||
standardComputerBuilder.addMotherboard();
|
||||
assertEquals("Standard Motherboard", standardComputerBuilder.getComputerParts().get("Motherboard"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenStandardMotheroboard_whenSetup_thenTwoEqualAssertions() {
|
||||
public void givenStandardMotherboard_whenSetup_thenTwoEqualAssertions() {
|
||||
standardComputerBuilder.setupMotherboard();
|
||||
assertEquals("Screwing the standard motherboard to the case.", standardComputerBuilder.getMotherboardSetupStatus().get(0));
|
||||
assertEquals("Pluging in the power supply connectors.", standardComputerBuilder.getMotherboardSetupStatus().get(1));
|
||||
@ -77,7 +77,7 @@ public class TemplateMethodPatternTest {
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenAllHighEndParts_whenBuildingComputer_thenTwoParts() {
|
||||
public void givenAllHighEnddParts_whenBuildingComputer_thenTwoParts() {
|
||||
highEndComputerBuilder.buildComputer();
|
||||
assertEquals(2, highEndComputerBuilder.getComputerParts().size());
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user