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

View File

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