mirror of
https://github.com/tiennm99/java-design-patterns.git
synced 2026-05-20 01:25:41 +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:
@@ -28,19 +28,18 @@ import java.awt.event.KeyEvent;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
|
||||
/**
|
||||
* The component design pattern is a common game design structure. This pattern is often
|
||||
* used to reduce duplication of code as well as to improve maintainability.
|
||||
* In this implementation, component design pattern has been used to provide two game
|
||||
* objects with varying component interfaces (features). As opposed to copying and
|
||||
* pasting same code for the two game objects, the component interfaces allow game
|
||||
* objects to inherit these components from the component classes.
|
||||
* The component design pattern is a common game design structure. This pattern is often used to
|
||||
* reduce duplication of code as well as to improve maintainability. In this implementation,
|
||||
* component design pattern has been used to provide two game objects with varying component
|
||||
* interfaces (features). As opposed to copying and pasting same code for the two game objects, the
|
||||
* component interfaces allow game objects to inherit these components from the component classes.
|
||||
*
|
||||
* <p>The implementation has decoupled graphic, physics and input components from
|
||||
* the player and NPC objects. As a result, it avoids the creation of monolithic java classes.
|
||||
* <p>The implementation has decoupled graphic, physics and input components from the player and NPC
|
||||
* objects. As a result, it avoids the creation of monolithic java classes.
|
||||
*
|
||||
* <p>The below example in this App class demonstrates the use of the component interfaces
|
||||
* for separate objects (player & NPC) and updating of these components as per the
|
||||
* implementations in GameObject class and the component classes.
|
||||
* <p>The below example in this App class demonstrates the use of the component interfaces for
|
||||
* separate objects (player & NPC) and updating of these components as per the implementations in
|
||||
* GameObject class and the component classes.
|
||||
*/
|
||||
@Slf4j
|
||||
public final class App {
|
||||
@@ -53,7 +52,6 @@ public final class App {
|
||||
final var player = GameObject.createPlayer();
|
||||
final var npc = GameObject.createNpc();
|
||||
|
||||
|
||||
LOGGER.info("Player Update:");
|
||||
player.update(KeyEvent.KEY_LOCATION_LEFT);
|
||||
LOGGER.info("NPC Update:");
|
||||
|
||||
@@ -35,8 +35,8 @@ import lombok.Getter;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
|
||||
/**
|
||||
* The GameObject class has three component class instances that allow
|
||||
* the creation of different game objects based on the game design requirements.
|
||||
* The GameObject class has three component class instances that allow the creation of different
|
||||
* game objects based on the game design requirements.
|
||||
*/
|
||||
@Getter
|
||||
@RequiredArgsConstructor
|
||||
@@ -55,13 +55,13 @@ public class GameObject {
|
||||
* @return player object
|
||||
*/
|
||||
public static GameObject createPlayer() {
|
||||
return new GameObject(new PlayerInputComponent(),
|
||||
return new GameObject(
|
||||
new PlayerInputComponent(),
|
||||
new ObjectPhysicComponent(),
|
||||
new ObjectGraphicComponent(),
|
||||
"player");
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Creates a NPC game object.
|
||||
*
|
||||
@@ -69,16 +69,12 @@ public class GameObject {
|
||||
*/
|
||||
public static GameObject createNpc() {
|
||||
return new GameObject(
|
||||
new DemoInputComponent(),
|
||||
new ObjectPhysicComponent(),
|
||||
new ObjectGraphicComponent(),
|
||||
"npc");
|
||||
new DemoInputComponent(), new ObjectPhysicComponent(), new ObjectGraphicComponent(), "npc");
|
||||
}
|
||||
|
||||
/**
|
||||
* Updates the three components of the NPC object used in the demo in App.java
|
||||
* note that this is simply a duplicate of update() without the key event for
|
||||
* demonstration purposes.
|
||||
* Updates the three components of the NPC object used in the demo in App.java note that this is
|
||||
* simply a duplicate of update() without the key event for demonstration purposes.
|
||||
*
|
||||
* <p>This method is usually used in games if the player becomes inactive.
|
||||
*/
|
||||
@@ -108,10 +104,7 @@ public class GameObject {
|
||||
this.velocity += acceleration;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Set the c based on the current velocity.
|
||||
*/
|
||||
/** Set the c based on the current velocity. */
|
||||
public void updateCoordinate() {
|
||||
this.coordinate += this.velocity;
|
||||
}
|
||||
|
||||
+1
-3
@@ -26,9 +26,7 @@ package com.iluwatar.component.component.graphiccomponent;
|
||||
|
||||
import com.iluwatar.component.GameObject;
|
||||
|
||||
/**
|
||||
* Generic GraphicComponent interface.
|
||||
*/
|
||||
/** Generic GraphicComponent interface. */
|
||||
public interface GraphicComponent {
|
||||
void update(GameObject gameObject);
|
||||
}
|
||||
|
||||
+1
-3
@@ -27,9 +27,7 @@ package com.iluwatar.component.component.graphiccomponent;
|
||||
import com.iluwatar.component.GameObject;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
|
||||
/**
|
||||
* ObjectGraphicComponent class mimics the graphic component of the Game Object.
|
||||
*/
|
||||
/** ObjectGraphicComponent class mimics the graphic component of the Game Object. */
|
||||
@Slf4j
|
||||
public class ObjectGraphicComponent implements GraphicComponent {
|
||||
|
||||
|
||||
+5
-5
@@ -28,11 +28,11 @@ import com.iluwatar.component.GameObject;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
|
||||
/**
|
||||
* Take this component class to control player or the NPC for demo mode.
|
||||
* and implemented the InputComponent interface.
|
||||
* Take this component class to control player or the NPC for demo mode. and implemented the
|
||||
* InputComponent interface.
|
||||
*
|
||||
* <p>Essentially, the demo mode is utilised during a game if the user become inactive.
|
||||
* Please see: http://gameprogrammingpatterns.com/component.html
|
||||
* <p>Essentially, the demo mode is utilised during a game if the user become inactive. Please see:
|
||||
* http://gameprogrammingpatterns.com/component.html
|
||||
*/
|
||||
@Slf4j
|
||||
public class DemoInputComponent implements InputComponent {
|
||||
@@ -42,7 +42,7 @@ public class DemoInputComponent implements InputComponent {
|
||||
* Redundant method in the demo mode.
|
||||
*
|
||||
* @param gameObject the gameObject instance
|
||||
* @param e key event instance
|
||||
* @param e key event instance
|
||||
*/
|
||||
@Override
|
||||
public void update(GameObject gameObject, int e) {
|
||||
|
||||
+1
-3
@@ -26,9 +26,7 @@ package com.iluwatar.component.component.inputcomponent;
|
||||
|
||||
import com.iluwatar.component.GameObject;
|
||||
|
||||
/**
|
||||
* Generic InputComponent interface.
|
||||
*/
|
||||
/** Generic InputComponent interface. */
|
||||
public interface InputComponent {
|
||||
void update(GameObject gameObject, int e);
|
||||
}
|
||||
|
||||
+3
-3
@@ -29,8 +29,8 @@ import java.awt.event.KeyEvent;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
|
||||
/**
|
||||
* PlayerInputComponent is used to handle user key event inputs,
|
||||
* and thus it implements the InputComponent interface.
|
||||
* PlayerInputComponent is used to handle user key event inputs, and thus it implements the
|
||||
* InputComponent interface.
|
||||
*/
|
||||
@Slf4j
|
||||
public class PlayerInputComponent implements InputComponent {
|
||||
@@ -40,7 +40,7 @@ public class PlayerInputComponent implements InputComponent {
|
||||
* The update method to change the velocity based on the input key event.
|
||||
*
|
||||
* @param gameObject the gameObject instance
|
||||
* @param e key event instance
|
||||
* @param e key event instance
|
||||
*/
|
||||
@Override
|
||||
public void update(GameObject gameObject, int e) {
|
||||
|
||||
+1
-3
@@ -27,9 +27,7 @@ package com.iluwatar.component.component.physiccomponent;
|
||||
import com.iluwatar.component.GameObject;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
|
||||
/**
|
||||
* Take this component class to update the x coordinate for the Game Object instance.
|
||||
*/
|
||||
/** Take this component class to update the x coordinate for the Game Object instance. */
|
||||
@Slf4j
|
||||
public class ObjectPhysicComponent implements PhysicComponent {
|
||||
|
||||
|
||||
+1
-3
@@ -26,9 +26,7 @@ package com.iluwatar.component.component.physiccomponent;
|
||||
|
||||
import com.iluwatar.component.GameObject;
|
||||
|
||||
/**
|
||||
* Generic PhysicComponent interface.
|
||||
*/
|
||||
/** Generic PhysicComponent interface. */
|
||||
public interface PhysicComponent {
|
||||
void update(GameObject gameObject);
|
||||
}
|
||||
|
||||
@@ -24,18 +24,18 @@
|
||||
*/
|
||||
package com.iluwatar.component;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
/**
|
||||
* Tests App class : src/main/java/com/iluwatar/component/App.java
|
||||
* General execution test of the application.
|
||||
* Tests App class : src/main/java/com/iluwatar/component/App.java General execution test of the
|
||||
* application.
|
||||
*/
|
||||
class AppTest {
|
||||
|
||||
@Test
|
||||
void shouldExecuteComponentWithoutException() {
|
||||
assertDoesNotThrow(() -> App.main(new String[]{}));
|
||||
}
|
||||
@Test
|
||||
void shouldExecuteComponentWithoutException() {
|
||||
assertDoesNotThrow(() -> App.main(new String[] {}));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -24,72 +24,64 @@
|
||||
*/
|
||||
package com.iluwatar.component;
|
||||
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
|
||||
import java.awt.event.KeyEvent;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
/**
|
||||
* Tests GameObject class.
|
||||
* src/main/java/com/iluwatar/component/GameObject.java
|
||||
*/
|
||||
/** Tests GameObject class. src/main/java/com/iluwatar/component/GameObject.java */
|
||||
@Slf4j
|
||||
class GameObjectTest {
|
||||
GameObject playerTest;
|
||||
GameObject npcTest;
|
||||
@BeforeEach
|
||||
public void initEach() {
|
||||
//creates player & npc objects for testing
|
||||
//note that velocity and coordinates are initialised to 0 in GameObject.java
|
||||
playerTest = GameObject.createPlayer();
|
||||
npcTest = GameObject.createNpc();
|
||||
}
|
||||
GameObject playerTest;
|
||||
GameObject npcTest;
|
||||
|
||||
/**
|
||||
* Tests the create methods - createPlayer() and createNPC().
|
||||
*/
|
||||
@Test
|
||||
void objectTest(){
|
||||
LOGGER.info("objectTest:");
|
||||
assertEquals("player",playerTest.getName());
|
||||
assertEquals("npc",npcTest.getName());
|
||||
}
|
||||
@BeforeEach
|
||||
public void initEach() {
|
||||
// creates player & npc objects for testing
|
||||
// note that velocity and coordinates are initialised to 0 in GameObject.java
|
||||
playerTest = GameObject.createPlayer();
|
||||
npcTest = GameObject.createNpc();
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests the input component with varying key event inputs.
|
||||
* Targets the player game object.
|
||||
*/
|
||||
@Test
|
||||
void eventInputTest(){
|
||||
LOGGER.info("eventInputTest:");
|
||||
playerTest.update(KeyEvent.KEY_LOCATION_LEFT);
|
||||
assertEquals(-1, playerTest.getVelocity());
|
||||
assertEquals(-1, playerTest.getCoordinate());
|
||||
/** Tests the create methods - createPlayer() and createNPC(). */
|
||||
@Test
|
||||
void objectTest() {
|
||||
LOGGER.info("objectTest:");
|
||||
assertEquals("player", playerTest.getName());
|
||||
assertEquals("npc", npcTest.getName());
|
||||
}
|
||||
|
||||
playerTest.update(KeyEvent.KEY_LOCATION_RIGHT);
|
||||
playerTest.update(KeyEvent.KEY_LOCATION_RIGHT);
|
||||
assertEquals(1, playerTest.getVelocity());
|
||||
assertEquals(0, playerTest.getCoordinate());
|
||||
/** Tests the input component with varying key event inputs. Targets the player game object. */
|
||||
@Test
|
||||
void eventInputTest() {
|
||||
LOGGER.info("eventInputTest:");
|
||||
playerTest.update(KeyEvent.KEY_LOCATION_LEFT);
|
||||
assertEquals(-1, playerTest.getVelocity());
|
||||
assertEquals(-1, playerTest.getCoordinate());
|
||||
|
||||
LOGGER.info(Integer.toString(playerTest.getCoordinate()));
|
||||
LOGGER.info(Integer.toString(playerTest.getVelocity()));
|
||||
playerTest.update(KeyEvent.KEY_LOCATION_RIGHT);
|
||||
playerTest.update(KeyEvent.KEY_LOCATION_RIGHT);
|
||||
assertEquals(1, playerTest.getVelocity());
|
||||
assertEquals(0, playerTest.getCoordinate());
|
||||
|
||||
GameObject p2 = GameObject.createPlayer();
|
||||
p2.update(KeyEvent.KEY_LOCATION_LEFT);
|
||||
//in the case of an unknown, object stats are set to default
|
||||
p2.update(KeyEvent.KEY_LOCATION_UNKNOWN);
|
||||
assertEquals(-1, p2.getVelocity());
|
||||
}
|
||||
LOGGER.info(Integer.toString(playerTest.getCoordinate()));
|
||||
LOGGER.info(Integer.toString(playerTest.getVelocity()));
|
||||
|
||||
/**
|
||||
* Tests the demo component interface.
|
||||
*/
|
||||
@Test
|
||||
void npcDemoTest(){
|
||||
LOGGER.info("npcDemoTest:");
|
||||
npcTest.demoUpdate();
|
||||
assertEquals(2, npcTest.getVelocity());
|
||||
assertEquals(2, npcTest.getCoordinate());
|
||||
}
|
||||
GameObject p2 = GameObject.createPlayer();
|
||||
p2.update(KeyEvent.KEY_LOCATION_LEFT);
|
||||
// in the case of an unknown, object stats are set to default
|
||||
p2.update(KeyEvent.KEY_LOCATION_UNKNOWN);
|
||||
assertEquals(-1, p2.getVelocity());
|
||||
}
|
||||
|
||||
/** Tests the demo component interface. */
|
||||
@Test
|
||||
void npcDemoTest() {
|
||||
LOGGER.info("npcDemoTest:");
|
||||
npcTest.demoUpdate();
|
||||
assertEquals(2, npcTest.getVelocity());
|
||||
assertEquals(2, npcTest.getCoordinate());
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user