BAEL-1289 - Merge (#3136)
* Initial Commit * Added Domain Classes * Update HighEndComputer.java * Update StandardComputer.java * Update TemplateMethodPatternTest.java * Update parent pom.xml * Update pom.xml in template-method submodule * Update readme.md * Update README.md * Initial Commit * Update HighEndComputer.java * Delete article folder * Initial Commit * Initial Commit * Initial Commit * Added Article Folder * Delete Article Folder * Added Article Folder
This commit is contained in:
37
patterns/behavioral-patterns/pom.xml
Normal file
37
patterns/behavioral-patterns/pom.xml
Normal file
@@ -0,0 +1,37 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
<groupId>com.baeldung.pattern.templatemethod</groupId>
|
||||
<artifactId>pattern.templatemethod</artifactId>
|
||||
<version>1.0</version>
|
||||
<packaging>jar</packaging>
|
||||
<parent>
|
||||
<groupId>com.baeldung.patterns</groupId>
|
||||
<artifactId>patterns-parent</artifactId>
|
||||
<version>1.0.0-SNAPSHOT</version>
|
||||
<relativePath>..</relativePath>
|
||||
</parent>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>junit</groupId>
|
||||
<artifactId>junit</artifactId>
|
||||
<version>4.12</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
<build>
|
||||
<plugins>
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-compiler-plugin</artifactId>
|
||||
<version>3.7.0</version>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
<properties>
|
||||
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
||||
<maven.compiler.source>1.8</maven.compiler.source>
|
||||
<maven.compiler.target>1.8</maven.compiler.target>
|
||||
</properties>
|
||||
</project>
|
||||
@@ -0,0 +1,21 @@
|
||||
package com.baeldung.pattern.templatemethod.application;
|
||||
|
||||
import com.baeldung.pattern.templatemethod.model.Computer;
|
||||
import com.baeldung.pattern.templatemethod.model.StandardComputer;
|
||||
import com.baeldung.pattern.templatemethod.model.HighEndComputer;
|
||||
import com.baeldung.pattern.templatemethod.model.ComputerBuilder;
|
||||
import com.baeldung.pattern.templatemethod.model.HighEndComputerBuilder;
|
||||
import com.baeldung.pattern.templatemethod.model.StandardComputerBuilder;
|
||||
|
||||
public class Application {
|
||||
|
||||
public static void main(String[] args) {
|
||||
ComputerBuilder standardComputerBuilder = new StandardComputerBuilder();
|
||||
Computer standardComputer = standardComputerBuilder.buildComputer();
|
||||
standardComputer.getComputerParts().forEach((k, v) -> System.out.println("Part : " + k + " Value : " + v));
|
||||
|
||||
ComputerBuilder highEndComputerBuilder = new HighEndComputerBuilder();
|
||||
Computer highEndComputer = highEndComputerBuilder.buildComputer();
|
||||
highEndComputer.getComputerParts().forEach((k, v) -> System.out.println("Part : " + k + " Value : " + v));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,17 @@
|
||||
package com.baeldung.pattern.templatemethod.model;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
public class Computer {
|
||||
|
||||
private Map<String, String> computerParts = new HashMap<>();
|
||||
|
||||
public Computer(Map<String, String> computerParts) {
|
||||
this.computerParts = computerParts;
|
||||
}
|
||||
|
||||
public Map<String, String> getComputerParts() {
|
||||
return computerParts;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,38 @@
|
||||
package com.baeldung.pattern.templatemethod.model;
|
||||
|
||||
import com.baeldung.pattern.templatemethod.model.Computer;
|
||||
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> motherboardSetupStatus = 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 motherboardSetupStatus;
|
||||
}
|
||||
|
||||
public Map<String, String> getComputerParts() {
|
||||
return computerParts;
|
||||
}
|
||||
|
||||
private Computer getComputer() {
|
||||
return new Computer(computerParts);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
package com.baeldung.pattern.templatemethod.model;
|
||||
|
||||
import com.baeldung.pattern.templatemethod.model.Computer;
|
||||
import java.util.Map;
|
||||
|
||||
public class HighEndComputer extends Computer {
|
||||
|
||||
public HighEndComputer(Map<String, String> computerParts) {
|
||||
super(computerParts);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,21 @@
|
||||
package com.baeldung.pattern.templatemethod.model;
|
||||
|
||||
public class HighEndComputerBuilder extends ComputerBuilder {
|
||||
|
||||
@Override
|
||||
public void addMotherboard() {
|
||||
computerParts.put("Motherboard", "High-end Motherboard");
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setupMotherboard() {
|
||||
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");
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
package com.baeldung.pattern.templatemethod.model;
|
||||
|
||||
import com.baeldung.pattern.templatemethod.model.Computer;
|
||||
import java.util.Map;
|
||||
|
||||
public class StandardComputer extends Computer {
|
||||
|
||||
public StandardComputer(Map<String, String> computerParts) {
|
||||
super(computerParts);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,21 @@
|
||||
package com.baeldung.pattern.templatemethod.model;
|
||||
|
||||
public class StandardComputerBuilder extends ComputerBuilder {
|
||||
|
||||
@Override
|
||||
public void addMotherboard() {
|
||||
computerParts.put("Motherboard", "Standard Motherboard");
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setupMotherboard() {
|
||||
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
|
||||
public void addProcessor() {
|
||||
computerParts.put("Processor", "Standard Processor");
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,87 @@
|
||||
package com.baeldung.pattern.templatemethod.test;
|
||||
|
||||
import com.baeldung.pattern.templatemethod.model.Computer;
|
||||
import com.baeldung.pattern.templatemethod.model.HighEndComputerBuilder;
|
||||
import com.baeldung.pattern.templatemethod.model.StandardComputerBuilder;
|
||||
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 StandardComputerBuilder standardComputerBuilder;
|
||||
private static HighEndComputerBuilder highEndComputerBuilder;
|
||||
|
||||
@BeforeClass
|
||||
public static void setUpStandardComputerBuilderInstance() {
|
||||
standardComputerBuilder = new StandardComputerBuilder();
|
||||
}
|
||||
|
||||
@BeforeClass
|
||||
public static void setUpHighEndComputerBuilderInstance() {
|
||||
highEndComputerBuilder = new HighEndComputerBuilder();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenStandardMotherBoard_whenAddingMotherBoard_thenEqualAssertion() {
|
||||
standardComputerBuilder.addMotherboard();
|
||||
assertEquals("Standard Motherboard", standardComputerBuilder.getComputerParts().get("Motherboard"));
|
||||
}
|
||||
|
||||
@Test
|
||||
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));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenStandardProcessor_whenAddingProcessor_thenEqualAssertion() {
|
||||
standardComputerBuilder.addProcessor();
|
||||
assertEquals("Standard Processor", standardComputerBuilder.getComputerParts().get("Processor"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenAllStandardParts_whenBuildingComputer_thenTwoParts() {
|
||||
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 givenHighEnddMotheroboard_whenSetup_thenTwoEqualAssertions() {
|
||||
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() {
|
||||
highEndComputerBuilder.addProcessor();
|
||||
assertEquals("High-end Processor", highEndComputerBuilder.getComputerParts().get("Processor"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenAllHighEnddParts_whenBuildingComputer_thenTwoParts() {
|
||||
highEndComputerBuilder.buildComputer();
|
||||
assertEquals(2, highEndComputerBuilder.getComputerParts().size());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenAllHighEndParts_whenComputerisBuilt_thenComputerInstance() {
|
||||
assertThat(standardComputerBuilder.buildComputer(), instanceOf(Computer.class));
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user