Implemeting the template method pattern in Java - BAEL-1289 (#3012)

* Initial Commit

* Delete domain classes
This commit is contained in:
Alejandro Gervasio 2017-11-13 04:42:26 -03:00 committed by Grzegorz Piwowarek
parent 294a055ce5
commit 10ea5fa784
6 changed files with 145 additions and 44 deletions

View File

@ -1,18 +1,21 @@
package com.baeldung.templatemethodpattern.application;
import com.baeldung.templatemethodpattern.model.Computer;
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 {
public static void main(String[] args) {
Computer standardComputer = new StandardComputer();
standardComputer.buildComputer();
ComputerBuilder standardComputerBuilder = new StandardComputerBuilder();
Computer standardComputer = standardComputerBuilder.buildComputer();
standardComputer.getComputerParts().forEach((k, v) -> System.out.println("Part : " + k + " Value : " + v));
Computer highEndComputer = new HighEndComputer();
highEndComputer.buildComputer();
ComputerBuilder highEndComputerBuilder = new HighEndComputerBuilder();
Computer highEndComputer = highEndComputerBuilder.buildComputer();
highEndComputer.getComputerParts().forEach((k, v) -> System.out.println("Part : " + k + " Value : " + v));
}
}

View File

@ -1,20 +1,20 @@
package com.baeldung.templatemethodpattern.model;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.HashMap;
import java.util.Map;
import java.util.ArrayList;
import java.util.List;
public abstract class Computer {
public abstract class ComputerBuilder {
protected Map<String, String> computerParts = new HashMap<>();
protected List<String> moterboardSetupStatus = new ArrayList<>();
public final void buildComputer() {
addMotherboard();
setupMotherboard();
addProcessor();
public final Computer buildComputer() {
addMotherboard();
setupMotherboard();
addProcessor();
return getComputer();
}
public abstract void addMotherboard();
@ -29,5 +29,9 @@ public abstract class Computer {
public Map<String, String> getComputerParts() {
return computerParts;
}
}
private Computer getComputer() {
return new Computer(computerParts);
}
}

View File

@ -0,0 +1,37 @@
package com.baeldung.templatemethodpattern.model;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public abstract class ComputerBuilder {
protected Map<String, String> computerParts = new HashMap<>();
protected List<String> moterboardSetupStatus = new ArrayList<>();
public final Computer buildComputer() {
addMotherboard();
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() {
return computerParts;
}
private Computer getComputer() {
return new Computer(computerParts);
}
}

View File

@ -0,0 +1,21 @@
package com.baeldung.templatemethodpattern.model;
public class HighEndComputerBuilder extends ComputerBuilder {
@Override
public void addMotherboard() {
computerParts.put("Motherboard", "High-end Motherboard");
}
@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");
}
}

View File

@ -0,0 +1,21 @@
package com.baeldung.templatemethodpattern.model;
public class StandardComputerBuilder extends ComputerBuilder {
@Override
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");
}
}

View File

@ -1,25 +1,30 @@
package com.baeldung.templatemethodpatterntest;
import com.baeldung.templatemethodpattern.model.Computer;
import com.baeldung.templatemethodpattern.model.HighEndComputerBuilder;
import com.baeldung.templatemethodpattern.model.StandardComputerBuilder;
import com.baeldung.templatemethodpattern.model.HighEndComputer;
import com.baeldung.templatemethodpattern.model.StandardComputer;
import org.junit.Assert;
import static org.junit.Assert.assertEquals;
import org.junit.BeforeClass;
import org.junit.Test;
import static org.hamcrest.CoreMatchers.instanceOf;
import static org.junit.Assert.assertThat;
public class TemplateMethodPatternTest {
private static StandardComputer standardComputer;
private static HighEndComputer highEndComputer;
private static StandardComputerBuilder standardComputerBuilder;
private static HighEndComputerBuilder highEndComputerBuilder;
@BeforeClass
public static void setUpStandardComputerInstance() {
standardComputer = new StandardComputer();
public static void setUpStandardComputerBuilderInstance() {
standardComputerBuilder = new StandardComputerBuilder();
}
@BeforeClass
public static void setUpHighEndComputerInstance() {
highEndComputer = new HighEndComputer();
public static void setUpHighEndComputerBuilderInstance() {
highEndComputerBuilder = new HighEndComputerBuilder();
}
@Test
@ -30,45 +35,55 @@ public class TemplateMethodPatternTest {
@Test
public void givenStandardMotheroboard_whenSetup_thenTwoEqualAssertions() {
standardComputer.setupMotherboard();
assertEquals("Screwing the standard motherboard to the case.", standardComputer.getMotherboardSetupStatus().get(0));
assertEquals("Plugin in the power supply connectors.", standardComputer.getMotherboardSetupStatus().get(1));
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));
}
@Test
public void givenStandardProcessor_whenAddingProcessor_thenEqualAssertion() {
standardComputer.addProcessor();
assertEquals("Standard Processor", standardComputer.getComputerParts().get("Processor"));
standardComputerBuilder.addProcessor();
assertEquals("Standard Processor", standardComputerBuilder.getComputerParts().get("Processor"));
}
@Test
public void givenAllStandardParts_whenBuildingComputer_thenTwoParts() {
standardComputer.buildComputer();
assertEquals(2, standardComputer.getComputerParts().size());
standardComputerBuilder.buildComputer();
assertEquals(2, standardComputerBuilder.getComputerParts().size());
}
@Test
public void givenAllStandardParts_whenComputerisBuilt_thenComputerInstance() {
assertThat(standardComputerBuilder.buildComputer(), instanceOf(Computer.class));
}
@Test
public void givenHighEnddMotherBoard_whenAddingMotherBoard_thenEqualAssertion() {
highEndComputerBuilder.addMotherboard();
Assert.assertEquals("High-end Motherboard", highEndComputerBuilder.getComputerParts().get("Motherboard"));
}
@Test
public void givenHighEnddMotherBoard_whenAddingMotherBoard_thenEqualAssertion() {
highEndComputer.addMotherboard();
Assert.assertEquals("High-end Motherboard", highEndComputer.getComputerParts().get("Motherboard"));
}
@Test
public void givenHighEnddMotheroboard_whenSetup_thenTwoEqualAssertions() {
highEndComputer.setupMotherboard();
assertEquals("Screwing the high-end motherboard to the case.", highEndComputer.getMotherboardSetupStatus().get(0));
assertEquals("Plugin in the power supply connectors.", highEndComputer.getMotherboardSetupStatus().get(1));
highEndComputerBuilder.setupMotherboard();
assertEquals("Screwing the high-end motherboard to the case.", highEndComputerBuilder.getMotherboardSetupStatus().get(0));
assertEquals("Pluging in the power supply connectors.", highEndComputerBuilder.getMotherboardSetupStatus().get(1));
}
@Test
public void givenHightEndProcessor_whenAddingProcessor_thenEqualAssertion() {
highEndComputer.addProcessor();
Assert.assertEquals("High-end Processor", highEndComputer.getComputerParts().get("Processor"));
highEndComputerBuilder.addProcessor();
assertEquals("High-end Processor", highEndComputerBuilder.getComputerParts().get("Processor"));
}
@Test
public void givenAllHighEnddParts_whenBuildingComputer_thenTwoParts() {
highEndComputer.buildComputer();
assertEquals(2, highEndComputer.getComputerParts().size());
}
highEndComputerBuilder.buildComputer();
assertEquals(2, highEndComputerBuilder.getComputerParts().size());
}
@Test
public void givenAllHighEndParts_whenComputerisBuilt_thenComputerInstance() {
assertThat(standardComputerBuilder.buildComputer(), instanceOf(Computer.class));
}
}