mirror of
https://github.com/tiennm99/java-design-patterns.git
synced 2026-05-24 14:25:33 +00:00
deps: Refactor dependencies (#3224)
* remove spring dep move junit, logging, mockito under dep mgmt * upgrade anti-corruption-layer deps * async method invocation * balking, bloc * bridge to bytecode * caching * callback - cqrs * component - health check * hexagonal - metadata mapping * rest of the patterns * remove checkstyle, take spotless into use
This commit is contained in:
@@ -31,30 +31,28 @@ import lombok.extern.slf4j.Slf4j;
|
||||
*
|
||||
* <p><b>Intent</b> <br>
|
||||
* An extension of the Builder pattern that fully guides the user through the creation of the object
|
||||
* with no chances of confusion. <br> The user experience will be much more improved by the fact
|
||||
* that he will only see the next step methods available, NO build method until is the right time to
|
||||
* build the object.
|
||||
* with no chances of confusion. <br>
|
||||
* The user experience will be much more improved by the fact that he will only see the next step
|
||||
* methods available, NO build method until is the right time to build the object.
|
||||
*
|
||||
* <p><b>Implementation</b> <br>
|
||||
* The concept is simple:
|
||||
*
|
||||
* <ul>
|
||||
*
|
||||
* <li>Write creational steps inner classes or interfaces where each method knows what can be
|
||||
* displayed next.</li>
|
||||
*
|
||||
* <li>Implement all your steps interfaces in an inner static class.</li>
|
||||
*
|
||||
* <li>Last step is the BuildStep, in charge of creating the object you need to build.</li>
|
||||
* <li>Write creational steps inner classes or interfaces where each method knows what can be
|
||||
* displayed next.
|
||||
* <li>Implement all your steps interfaces in an inner static class.
|
||||
* <li>Last step is the BuildStep, in charge of creating the object you need to build.
|
||||
* </ul>
|
||||
*
|
||||
* <p><b>Applicability</b> <br>
|
||||
* Use the Step Builder pattern when the algorithm for creating a complex object should be
|
||||
* independent of the parts that make up the object and how they're assembled the construction
|
||||
* process must allow different representations for the object that's constructed when in the
|
||||
* process of constructing the order is important.
|
||||
* <br>
|
||||
* process of constructing the order is important. <br>
|
||||
*
|
||||
* @see <a href="http://rdafbn.blogspot.co.uk/2012/07/step-builder-pattern_28.html">http://rdafbn.blogspot.co.uk/2012/07/step-builder-pattern_28.html</a>
|
||||
* @see <a
|
||||
* href="http://rdafbn.blogspot.co.uk/2012/07/step-builder-pattern_28.html">http://rdafbn.blogspot.co.uk/2012/07/step-builder-pattern_28.html</a>
|
||||
*/
|
||||
@Slf4j
|
||||
public class App {
|
||||
@@ -66,34 +64,30 @@ public class App {
|
||||
*/
|
||||
public static void main(String[] args) {
|
||||
|
||||
var warrior = CharacterStepBuilder
|
||||
.newBuilder()
|
||||
.name("Amberjill")
|
||||
.fighterClass("Paladin")
|
||||
.withWeapon("Sword")
|
||||
.noAbilities()
|
||||
.build();
|
||||
var warrior =
|
||||
CharacterStepBuilder.newBuilder()
|
||||
.name("Amberjill")
|
||||
.fighterClass("Paladin")
|
||||
.withWeapon("Sword")
|
||||
.noAbilities()
|
||||
.build();
|
||||
|
||||
LOGGER.info(warrior.toString());
|
||||
|
||||
var mage = CharacterStepBuilder
|
||||
.newBuilder()
|
||||
.name("Riobard")
|
||||
.wizardClass("Sorcerer")
|
||||
.withSpell("Fireball")
|
||||
.withAbility("Fire Aura")
|
||||
.withAbility("Teleport")
|
||||
.noMoreAbilities()
|
||||
.build();
|
||||
var mage =
|
||||
CharacterStepBuilder.newBuilder()
|
||||
.name("Riobard")
|
||||
.wizardClass("Sorcerer")
|
||||
.withSpell("Fireball")
|
||||
.withAbility("Fire Aura")
|
||||
.withAbility("Teleport")
|
||||
.noMoreAbilities()
|
||||
.build();
|
||||
|
||||
LOGGER.info(mage.toString());
|
||||
|
||||
var thief = CharacterStepBuilder
|
||||
.newBuilder()
|
||||
.name("Desmond")
|
||||
.fighterClass("Rogue")
|
||||
.noWeapon()
|
||||
.build();
|
||||
var thief =
|
||||
CharacterStepBuilder.newBuilder().name("Desmond").fighterClass("Rogue").noWeapon().build();
|
||||
|
||||
LOGGER.info(thief.toString());
|
||||
}
|
||||
|
||||
@@ -28,11 +28,7 @@ import java.util.List;
|
||||
import lombok.Getter;
|
||||
import lombok.Setter;
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* The class with many parameters.
|
||||
*/
|
||||
/** The class with many parameters. */
|
||||
@Getter
|
||||
@Setter
|
||||
public class Character {
|
||||
@@ -48,7 +44,6 @@ public class Character {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return new StringBuilder()
|
||||
|
||||
@@ -27,21 +27,16 @@ package com.iluwatar.stepbuilder;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* The Step Builder class.
|
||||
*/
|
||||
/** The Step Builder class. */
|
||||
public final class CharacterStepBuilder {
|
||||
|
||||
private CharacterStepBuilder() {
|
||||
}
|
||||
private CharacterStepBuilder() {}
|
||||
|
||||
public static NameStep newBuilder() {
|
||||
return new CharacterSteps();
|
||||
}
|
||||
|
||||
/**
|
||||
* First Builder Step in charge of the Character name. Next Step available : ClassStep
|
||||
*/
|
||||
/** First Builder Step in charge of the Character name. Next Step available : ClassStep */
|
||||
public interface NameStep {
|
||||
ClassStep name(String name);
|
||||
}
|
||||
@@ -76,9 +71,7 @@ public final class CharacterStepBuilder {
|
||||
BuildStep noSpell();
|
||||
}
|
||||
|
||||
/**
|
||||
* This step is in charge of abilities. Next Step available : BuildStep
|
||||
*/
|
||||
/** This step is in charge of abilities. Next Step available : BuildStep */
|
||||
public interface AbilityStep {
|
||||
AbilityStep withAbility(String ability);
|
||||
|
||||
@@ -94,12 +87,9 @@ public final class CharacterStepBuilder {
|
||||
Character build();
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Step Builder implementation.
|
||||
*/
|
||||
private static class CharacterSteps implements NameStep, ClassStep, WeaponStep, SpellStep,
|
||||
AbilityStep, BuildStep {
|
||||
/** Step Builder implementation. */
|
||||
private static class CharacterSteps
|
||||
implements NameStep, ClassStep, WeaponStep, SpellStep, AbilityStep, BuildStep {
|
||||
|
||||
private String name;
|
||||
private String fighterClass;
|
||||
|
||||
@@ -24,17 +24,15 @@
|
||||
*/
|
||||
package com.iluwatar.stepbuilder;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
|
||||
|
||||
/**
|
||||
* Application test
|
||||
*/
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
/** Application test */
|
||||
class AppTest {
|
||||
|
||||
@Test
|
||||
void shouldExecuteWithoutException() {
|
||||
assertDoesNotThrow(() -> App.main(new String[]{}));
|
||||
assertDoesNotThrow(() -> App.main(new String[] {}));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -31,25 +31,21 @@ import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
/**
|
||||
* CharacterStepBuilderTest
|
||||
*
|
||||
*/
|
||||
/** CharacterStepBuilderTest */
|
||||
class CharacterStepBuilderTest {
|
||||
|
||||
/**
|
||||
* Build a new wizard {@link Character} and verify if it has the expected attributes
|
||||
*/
|
||||
/** Build a new wizard {@link Character} and verify if it has the expected attributes */
|
||||
@Test
|
||||
void testBuildWizard() {
|
||||
final var character = CharacterStepBuilder.newBuilder()
|
||||
.name("Merlin")
|
||||
.wizardClass("alchemist")
|
||||
.withSpell("poison")
|
||||
.withAbility("invisibility")
|
||||
.withAbility("wisdom")
|
||||
.noMoreAbilities()
|
||||
.build();
|
||||
final var character =
|
||||
CharacterStepBuilder.newBuilder()
|
||||
.name("Merlin")
|
||||
.wizardClass("alchemist")
|
||||
.withSpell("poison")
|
||||
.withAbility("invisibility")
|
||||
.withAbility("wisdom")
|
||||
.noMoreAbilities()
|
||||
.build();
|
||||
|
||||
assertEquals("Merlin", character.getName());
|
||||
assertEquals("alchemist", character.getWizardClass());
|
||||
@@ -61,7 +57,6 @@ class CharacterStepBuilderTest {
|
||||
assertEquals(2, abilities.size());
|
||||
assertTrue(abilities.contains("invisibility"));
|
||||
assertTrue(abilities.contains("wisdom"));
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -70,53 +65,46 @@ class CharacterStepBuilderTest {
|
||||
*/
|
||||
@Test
|
||||
void testBuildPoorWizard() {
|
||||
final var character = CharacterStepBuilder.newBuilder()
|
||||
.name("Merlin")
|
||||
.wizardClass("alchemist")
|
||||
.noSpell()
|
||||
.build();
|
||||
final var character =
|
||||
CharacterStepBuilder.newBuilder().name("Merlin").wizardClass("alchemist").noSpell().build();
|
||||
|
||||
assertEquals("Merlin", character.getName());
|
||||
assertEquals("alchemist", character.getWizardClass());
|
||||
assertNull(character.getSpell());
|
||||
assertNull(character.getAbilities());
|
||||
assertNotNull(character.toString());
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Build a new wizard {@link Character} and verify if it has the expected attributes
|
||||
*/
|
||||
/** Build a new wizard {@link Character} and verify if it has the expected attributes */
|
||||
@Test
|
||||
void testBuildWeakWizard() {
|
||||
final var character = CharacterStepBuilder.newBuilder()
|
||||
.name("Merlin")
|
||||
.wizardClass("alchemist")
|
||||
.withSpell("poison")
|
||||
.noAbilities()
|
||||
.build();
|
||||
final var character =
|
||||
CharacterStepBuilder.newBuilder()
|
||||
.name("Merlin")
|
||||
.wizardClass("alchemist")
|
||||
.withSpell("poison")
|
||||
.noAbilities()
|
||||
.build();
|
||||
|
||||
assertEquals("Merlin", character.getName());
|
||||
assertEquals("alchemist", character.getWizardClass());
|
||||
assertEquals("poison", character.getSpell());
|
||||
assertNull(character.getAbilities());
|
||||
assertNotNull(character.toString());
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Build a new warrior {@link Character} and verify if it has the expected attributes
|
||||
*/
|
||||
/** Build a new warrior {@link Character} and verify if it has the expected attributes */
|
||||
@Test
|
||||
void testBuildWarrior() {
|
||||
final var character = CharacterStepBuilder.newBuilder()
|
||||
.name("Cuauhtemoc")
|
||||
.fighterClass("aztec")
|
||||
.withWeapon("spear")
|
||||
.withAbility("speed")
|
||||
.withAbility("strength")
|
||||
.noMoreAbilities()
|
||||
.build();
|
||||
final var character =
|
||||
CharacterStepBuilder.newBuilder()
|
||||
.name("Cuauhtemoc")
|
||||
.fighterClass("aztec")
|
||||
.withWeapon("spear")
|
||||
.withAbility("speed")
|
||||
.withAbility("strength")
|
||||
.noMoreAbilities()
|
||||
.build();
|
||||
|
||||
assertEquals("Cuauhtemoc", character.getName());
|
||||
assertEquals("aztec", character.getFighterClass());
|
||||
@@ -128,7 +116,6 @@ class CharacterStepBuilderTest {
|
||||
assertEquals(2, abilities.size());
|
||||
assertTrue(abilities.contains("speed"));
|
||||
assertTrue(abilities.contains("strength"));
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -137,18 +124,18 @@ class CharacterStepBuilderTest {
|
||||
*/
|
||||
@Test
|
||||
void testBuildPoorWarrior() {
|
||||
final var character = CharacterStepBuilder.newBuilder()
|
||||
.name("Poor warrior")
|
||||
.fighterClass("none")
|
||||
.noWeapon()
|
||||
.build();
|
||||
final var character =
|
||||
CharacterStepBuilder.newBuilder()
|
||||
.name("Poor warrior")
|
||||
.fighterClass("none")
|
||||
.noWeapon()
|
||||
.build();
|
||||
|
||||
assertEquals("Poor warrior", character.getName());
|
||||
assertEquals("none", character.getFighterClass());
|
||||
assertNull(character.getWeapon());
|
||||
assertNull(character.getAbilities());
|
||||
assertNotNull(character.toString());
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -157,19 +144,18 @@ class CharacterStepBuilderTest {
|
||||
*/
|
||||
@Test
|
||||
void testBuildWeakWarrior() {
|
||||
final var character = CharacterStepBuilder.newBuilder()
|
||||
.name("Weak warrior")
|
||||
.fighterClass("none")
|
||||
.withWeapon("Slingshot")
|
||||
.noAbilities()
|
||||
.build();
|
||||
final var character =
|
||||
CharacterStepBuilder.newBuilder()
|
||||
.name("Weak warrior")
|
||||
.fighterClass("none")
|
||||
.withWeapon("Slingshot")
|
||||
.noAbilities()
|
||||
.build();
|
||||
|
||||
assertEquals("Weak warrior", character.getName());
|
||||
assertEquals("none", character.getFighterClass());
|
||||
assertEquals("Slingshot", character.getWeapon());
|
||||
assertNull(character.getAbilities());
|
||||
assertNotNull(character.toString());
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user