mirror of
https://github.com/tiennm99/java-design-patterns.git
synced 2026-05-19 15:27:13 +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:
@@ -34,10 +34,9 @@ import lombok.extern.slf4j.Slf4j;
|
||||
* derived classes—rather than by calling a constructor.
|
||||
*
|
||||
* <p>In this Factory Method example we have an interface ({@link Blacksmith}) with a method for
|
||||
* creating objects ({@link Blacksmith#manufactureWeapon}). The concrete subclasses (
|
||||
* {@link OrcBlacksmith}, {@link ElfBlacksmith}) then override the method to produce objects of
|
||||
* their liking.
|
||||
*
|
||||
* creating objects ({@link Blacksmith#manufactureWeapon}). The concrete subclasses ( {@link
|
||||
* OrcBlacksmith}, {@link ElfBlacksmith}) then override the method to produce objects of their
|
||||
* liking.
|
||||
*/
|
||||
@Slf4j
|
||||
public class App {
|
||||
@@ -46,6 +45,7 @@ public class App {
|
||||
|
||||
/**
|
||||
* Program entry point.
|
||||
*
|
||||
* @param args command line args
|
||||
*/
|
||||
public static void main(String[] args) {
|
||||
|
||||
@@ -24,11 +24,8 @@
|
||||
*/
|
||||
package com.iluwatar.factory.method;
|
||||
|
||||
/**
|
||||
* The interface containing method for producing objects.
|
||||
*/
|
||||
/** The interface containing method for producing objects. */
|
||||
public interface Blacksmith {
|
||||
|
||||
Weapon manufactureWeapon(WeaponType weaponType);
|
||||
|
||||
}
|
||||
|
||||
@@ -28,9 +28,7 @@ import java.util.Arrays;
|
||||
import java.util.EnumMap;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* Concrete subclass for creating new objects.
|
||||
*/
|
||||
/** Concrete subclass for creating new objects. */
|
||||
public class ElfBlacksmith implements Blacksmith {
|
||||
|
||||
private static final Map<WeaponType, ElfWeapon> ELFARSENAL;
|
||||
|
||||
@@ -24,12 +24,7 @@
|
||||
*/
|
||||
package com.iluwatar.factory.method;
|
||||
|
||||
import lombok.Getter;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
|
||||
/**
|
||||
* ElfWeapon.
|
||||
*/
|
||||
/** ElfWeapon. */
|
||||
public record ElfWeapon(WeaponType weaponType) implements Weapon {
|
||||
|
||||
@Override
|
||||
|
||||
@@ -28,9 +28,7 @@ import java.util.Arrays;
|
||||
import java.util.EnumMap;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* Concrete subclass for creating new objects.
|
||||
*/
|
||||
/** Concrete subclass for creating new objects. */
|
||||
public class OrcBlacksmith implements Blacksmith {
|
||||
|
||||
private static final Map<WeaponType, OrcWeapon> ORCARSENAL;
|
||||
|
||||
@@ -24,12 +24,7 @@
|
||||
*/
|
||||
package com.iluwatar.factory.method;
|
||||
|
||||
import lombok.Getter;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
|
||||
/**
|
||||
* OrcWeapon.
|
||||
*/
|
||||
/** OrcWeapon. */
|
||||
public record OrcWeapon(WeaponType weaponType) implements Weapon {
|
||||
|
||||
@Override
|
||||
|
||||
@@ -24,11 +24,8 @@
|
||||
*/
|
||||
package com.iluwatar.factory.method;
|
||||
|
||||
/**
|
||||
* Weapon interface.
|
||||
*/
|
||||
/** Weapon interface. */
|
||||
public interface Weapon {
|
||||
|
||||
WeaponType weaponType();
|
||||
|
||||
}
|
||||
|
||||
@@ -26,12 +26,9 @@ package com.iluwatar.factory.method;
|
||||
|
||||
import lombok.RequiredArgsConstructor;
|
||||
|
||||
/**
|
||||
* WeaponType enumeration.
|
||||
*/
|
||||
/** WeaponType enumeration. */
|
||||
@RequiredArgsConstructor
|
||||
public enum WeaponType {
|
||||
|
||||
SHORT_SWORD("short sword"),
|
||||
SPEAR("spear"),
|
||||
AXE("axe"),
|
||||
|
||||
@@ -24,17 +24,15 @@
|
||||
*/
|
||||
package com.iluwatar.factory.method;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
|
||||
|
||||
/**
|
||||
* Tests that Factory Method example runs without errors.
|
||||
*/
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
/** Tests that Factory Method example runs without errors. */
|
||||
class AppTest {
|
||||
|
||||
@Test
|
||||
void shouldExecuteWithoutException() {
|
||||
assertDoesNotThrow(() -> App.main(new String[]{}));
|
||||
assertDoesNotThrow(() -> App.main(new String[] {}));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -36,10 +36,8 @@ import org.junit.jupiter.api.Test;
|
||||
* and implemented by child classes, or implemented in a base class and optionally overridden by
|
||||
* derived classes—rather than by calling a constructor.
|
||||
*
|
||||
* <p>Factory produces the object of its liking.
|
||||
* The weapon {@link Weapon} manufactured by the blacksmith depends on the kind of factory
|
||||
* implementation it is referring to.
|
||||
* </p>
|
||||
* <p>Factory produces the object of its liking. The weapon {@link Weapon} manufactured by the
|
||||
* blacksmith depends on the kind of factory implementation it is referring to.
|
||||
*/
|
||||
class FactoryMethodTest {
|
||||
|
||||
@@ -91,13 +89,15 @@ class FactoryMethodTest {
|
||||
* This method asserts that the weapon object that is passed is an instance of the clazz and the
|
||||
* weapon is of type expectedWeaponType.
|
||||
*
|
||||
* @param weapon weapon object which is to be verified
|
||||
* @param weapon weapon object which is to be verified
|
||||
* @param expectedWeaponType expected WeaponType of the weapon
|
||||
* @param clazz expected class of the weapon
|
||||
* @param clazz expected class of the weapon
|
||||
*/
|
||||
private void verifyWeapon(Weapon weapon, WeaponType expectedWeaponType, Class<?> clazz) {
|
||||
assertTrue(clazz.isInstance(weapon), "Weapon must be an object of: " + clazz.getName());
|
||||
assertEquals(expectedWeaponType, weapon
|
||||
.weaponType(), "Weapon must be of weaponType: " + expectedWeaponType);
|
||||
assertEquals(
|
||||
expectedWeaponType,
|
||||
weapon.weaponType(),
|
||||
"Weapon must be of weaponType: " + expectedWeaponType);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user