mirror of
https://github.com/tiennm99/java-design-patterns.git
synced 2026-08-14 12:26:24 +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:
@@ -35,9 +35,9 @@ import lombok.extern.slf4j.Slf4j;
|
||||
* <p>In the given example {@link WeaponFactory} represents the factory kit, that contains four
|
||||
* {@link Builder}s for creating new objects of the classes implementing {@link Weapon} interface.
|
||||
*
|
||||
* <p>Each of them can be called with {@link WeaponFactory#create(WeaponType)} method, with
|
||||
* an input representing an instance of {@link WeaponType} that needs to be mapped explicitly with
|
||||
* desired class type in the factory instance.
|
||||
* <p>Each of them can be called with {@link WeaponFactory#create(WeaponType)} method, with an input
|
||||
* representing an instance of {@link WeaponType} that needs to be mapped explicitly with desired
|
||||
* class type in the factory instance.
|
||||
*/
|
||||
@Slf4j
|
||||
public class App {
|
||||
@@ -48,12 +48,14 @@ public class App {
|
||||
* @param args command line args
|
||||
*/
|
||||
public static void main(String[] args) {
|
||||
var factory = WeaponFactory.factory(builder -> {
|
||||
builder.add(WeaponType.SWORD, Sword::new);
|
||||
builder.add(WeaponType.AXE, Axe::new);
|
||||
builder.add(WeaponType.SPEAR, Spear::new);
|
||||
builder.add(WeaponType.BOW, Bow::new);
|
||||
});
|
||||
var factory =
|
||||
WeaponFactory.factory(
|
||||
builder -> {
|
||||
builder.add(WeaponType.SWORD, Sword::new);
|
||||
builder.add(WeaponType.AXE, Axe::new);
|
||||
builder.add(WeaponType.SPEAR, Spear::new);
|
||||
builder.add(WeaponType.BOW, Bow::new);
|
||||
});
|
||||
var list = new ArrayList<Weapon>();
|
||||
list.add(factory.create(WeaponType.AXE));
|
||||
list.add(factory.create(WeaponType.SPEAR));
|
||||
|
||||
@@ -24,9 +24,7 @@
|
||||
*/
|
||||
package com.iluwatar.factorykit;
|
||||
|
||||
/**
|
||||
* Class representing Axe.
|
||||
*/
|
||||
/** Class representing Axe. */
|
||||
public class Axe implements Weapon {
|
||||
@Override
|
||||
public String toString() {
|
||||
|
||||
@@ -24,9 +24,7 @@
|
||||
*/
|
||||
package com.iluwatar.factorykit;
|
||||
|
||||
/**
|
||||
* Class representing Bows.
|
||||
*/
|
||||
/** Class representing Bows. */
|
||||
public class Bow implements Weapon {
|
||||
@Override
|
||||
public String toString() {
|
||||
|
||||
@@ -26,9 +26,7 @@ package com.iluwatar.factorykit;
|
||||
|
||||
import java.util.function.Supplier;
|
||||
|
||||
/**
|
||||
* Functional interface that allows adding builder with name to the factory.
|
||||
*/
|
||||
/** Functional interface that allows adding builder with name to the factory. */
|
||||
public interface Builder {
|
||||
void add(WeaponType name, Supplier<Weapon> supplier);
|
||||
}
|
||||
|
||||
@@ -24,9 +24,7 @@
|
||||
*/
|
||||
package com.iluwatar.factorykit;
|
||||
|
||||
/**
|
||||
* Class representing Spear.
|
||||
*/
|
||||
/** Class representing Spear. */
|
||||
public class Spear implements Weapon {
|
||||
@Override
|
||||
public String toString() {
|
||||
|
||||
@@ -24,9 +24,7 @@
|
||||
*/
|
||||
package com.iluwatar.factorykit;
|
||||
|
||||
/**
|
||||
* Class representing Swords.
|
||||
*/
|
||||
/** Class representing Swords. */
|
||||
public class Sword implements Weapon {
|
||||
@Override
|
||||
public String toString() {
|
||||
|
||||
@@ -24,8 +24,5 @@
|
||||
*/
|
||||
package com.iluwatar.factorykit;
|
||||
|
||||
/**
|
||||
* Interface representing weapon.
|
||||
*/
|
||||
public interface Weapon {
|
||||
}
|
||||
/** Interface representing weapon. */
|
||||
public interface Weapon {}
|
||||
|
||||
@@ -29,11 +29,11 @@ import java.util.function.Consumer;
|
||||
import java.util.function.Supplier;
|
||||
|
||||
/**
|
||||
* Functional interface, an example of the factory-kit design pattern.
|
||||
* <br>Instance created locally gives an opportunity to strictly define
|
||||
* which objects types the instance of a factory will be able to create.
|
||||
* <br>Factory is a placeholder for {@link Builder}s
|
||||
* with {@link WeaponFactory#create(WeaponType)} method to initialize new objects.
|
||||
* Functional interface, an example of the factory-kit design pattern. <br>
|
||||
* Instance created locally gives an opportunity to strictly define which objects types the instance
|
||||
* of a factory will be able to create. <br>
|
||||
* Factory is a placeholder for {@link Builder}s with {@link WeaponFactory#create(WeaponType)}
|
||||
* method to initialize new objects.
|
||||
*/
|
||||
public interface WeaponFactory {
|
||||
|
||||
|
||||
@@ -24,9 +24,7 @@
|
||||
*/
|
||||
package com.iluwatar.factorykit;
|
||||
|
||||
/**
|
||||
* Enumerates {@link Weapon} types.
|
||||
*/
|
||||
/** Enumerates {@link Weapon} types. */
|
||||
public enum WeaponType {
|
||||
SWORD,
|
||||
AXE,
|
||||
|
||||
@@ -24,19 +24,16 @@
|
||||
*/
|
||||
package com.iluwatar.factorykit.app;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
|
||||
|
||||
import com.iluwatar.factorykit.App;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
|
||||
|
||||
/**
|
||||
* Application Test Entrypoint
|
||||
*/
|
||||
/** Application Test Entrypoint */
|
||||
class AppTest {
|
||||
|
||||
@Test
|
||||
void shouldExecuteApplicationWithoutException() {
|
||||
assertDoesNotThrow(() -> App.main(new String[]{}));
|
||||
assertDoesNotThrow(() -> App.main(new String[] {}));
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -35,20 +35,20 @@ import com.iluwatar.factorykit.WeaponType;
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
/**
|
||||
* Test Factory Kit Pattern
|
||||
*/
|
||||
/** Test Factory Kit Pattern */
|
||||
class FactoryKitTest {
|
||||
|
||||
private WeaponFactory factory;
|
||||
|
||||
@BeforeEach
|
||||
void init() {
|
||||
factory = WeaponFactory.factory(builder -> {
|
||||
builder.add(WeaponType.SPEAR, Spear::new);
|
||||
builder.add(WeaponType.AXE, Axe::new);
|
||||
builder.add(WeaponType.SWORD, Sword::new);
|
||||
});
|
||||
factory =
|
||||
WeaponFactory.factory(
|
||||
builder -> {
|
||||
builder.add(WeaponType.SPEAR, Spear::new);
|
||||
builder.add(WeaponType.AXE, Axe::new);
|
||||
builder.add(WeaponType.SWORD, Sword::new);
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -71,7 +71,6 @@ class FactoryKitTest {
|
||||
verifyWeapon(weapon, Axe.class);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Testing {@link WeaponFactory} to produce a SWORD asserting that the Weapon is an instance of
|
||||
* {@link Sword}
|
||||
@@ -86,7 +85,7 @@ class FactoryKitTest {
|
||||
* This method asserts that the weapon object that is passed is an instance of the clazz
|
||||
*
|
||||
* @param weapon weapon object which is to be verified
|
||||
* @param clazz expected class of the weapon
|
||||
* @param clazz expected class of the weapon
|
||||
*/
|
||||
private void verifyWeapon(Weapon weapon, Class<?> clazz) {
|
||||
assertTrue(clazz.isInstance(weapon), "Weapon must be an object of: " + clazz.getName());
|
||||
|
||||
Reference in New Issue
Block a user