mirror of
https://github.com/tiennm99/java-design-patterns.git
synced 2026-05-15 06:58: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:
@@ -37,9 +37,9 @@ import lombok.extern.slf4j.Slf4j;
|
||||
* to change the method's implementation and add a new instanceof-check. This violates the single
|
||||
* responsibility principle - a class should have only one reason to change.
|
||||
*
|
||||
* <p>Instead of the instanceof-checks a better way is to make another virtual call on the
|
||||
* parameter object. This way new functionality can be easily added without the need to modify
|
||||
* existing implementation (open-closed principle).
|
||||
* <p>Instead of the instanceof-checks a better way is to make another virtual call on the parameter
|
||||
* object. This way new functionality can be easily added without the need to modify existing
|
||||
* implementation (open-closed principle).
|
||||
*
|
||||
* <p>In this example we have hierarchy of objects ({@link GameObject}) that can collide to each
|
||||
* other. Each object has its own coordinates which are checked against the other objects'
|
||||
@@ -57,21 +57,24 @@ public class App {
|
||||
public static void main(String[] args) {
|
||||
// initialize game objects and print their status
|
||||
LOGGER.info("Init objects and print their status");
|
||||
var objects = List.of(
|
||||
new FlamingAsteroid(0, 0, 5, 5),
|
||||
new SpaceStationMir(1, 1, 2, 2),
|
||||
new Meteoroid(10, 10, 15, 15),
|
||||
new SpaceStationIss(12, 12, 14, 14)
|
||||
);
|
||||
var objects =
|
||||
List.of(
|
||||
new FlamingAsteroid(0, 0, 5, 5),
|
||||
new SpaceStationMir(1, 1, 2, 2),
|
||||
new Meteoroid(10, 10, 15, 15),
|
||||
new SpaceStationIss(12, 12, 14, 14));
|
||||
objects.forEach(o -> LOGGER.info(o.toString()));
|
||||
|
||||
// collision check
|
||||
LOGGER.info("Collision check");
|
||||
objects.forEach(o1 -> objects.forEach(o2 -> {
|
||||
if (o1 != o2 && o1.intersectsWith(o2)) {
|
||||
o1.collision(o2);
|
||||
}
|
||||
}));
|
||||
objects.forEach(
|
||||
o1 ->
|
||||
objects.forEach(
|
||||
o2 -> {
|
||||
if (o1 != o2 && o1.intersectsWith(o2)) {
|
||||
o1.collision(o2);
|
||||
}
|
||||
}));
|
||||
|
||||
// output eventual object statuses
|
||||
LOGGER.info("Print object status after collision checks");
|
||||
|
||||
@@ -24,9 +24,7 @@
|
||||
*/
|
||||
package com.iluwatar.doubledispatch;
|
||||
|
||||
/**
|
||||
* Flaming asteroid game object.
|
||||
*/
|
||||
/** Flaming asteroid game object. */
|
||||
public class FlamingAsteroid extends Meteoroid {
|
||||
|
||||
public FlamingAsteroid(int left, int top, int right, int bottom) {
|
||||
|
||||
@@ -27,9 +27,7 @@ package com.iluwatar.doubledispatch;
|
||||
import lombok.Getter;
|
||||
import lombok.Setter;
|
||||
|
||||
/**
|
||||
* Game objects have coordinates and some other status information.
|
||||
*/
|
||||
/** Game objects have coordinates and some other status information. */
|
||||
@Getter
|
||||
@Setter
|
||||
public abstract class GameObject extends Rectangle {
|
||||
@@ -43,8 +41,9 @@ public abstract class GameObject extends Rectangle {
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return String.format("%s at %s damaged=%b onFire=%b", this.getClass().getSimpleName(),
|
||||
super.toString(), isDamaged(), isOnFire());
|
||||
return String.format(
|
||||
"%s at %s damaged=%b onFire=%b",
|
||||
this.getClass().getSimpleName(), super.toString(), isDamaged(), isOnFire());
|
||||
}
|
||||
|
||||
public abstract void collision(GameObject gameObject);
|
||||
|
||||
@@ -27,9 +27,7 @@ package com.iluwatar.doubledispatch;
|
||||
import com.iluwatar.doubledispatch.constants.AppConstants;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
|
||||
/**
|
||||
* Meteoroid game object.
|
||||
*/
|
||||
/** Meteoroid game object. */
|
||||
@Slf4j
|
||||
public class Meteoroid extends GameObject {
|
||||
|
||||
@@ -44,14 +42,14 @@ public class Meteoroid extends GameObject {
|
||||
|
||||
@Override
|
||||
public void collisionResolve(FlamingAsteroid asteroid) {
|
||||
LOGGER.info(AppConstants.HITS, asteroid.getClass().getSimpleName(), this.getClass()
|
||||
.getSimpleName());
|
||||
LOGGER.info(
|
||||
AppConstants.HITS, asteroid.getClass().getSimpleName(), this.getClass().getSimpleName());
|
||||
}
|
||||
|
||||
@Override
|
||||
public void collisionResolve(Meteoroid meteoroid) {
|
||||
LOGGER.info(AppConstants.HITS, meteoroid.getClass().getSimpleName(), this.getClass()
|
||||
.getSimpleName());
|
||||
LOGGER.info(
|
||||
AppConstants.HITS, meteoroid.getClass().getSimpleName(), this.getClass().getSimpleName());
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
@@ -27,9 +27,7 @@ package com.iluwatar.doubledispatch;
|
||||
import lombok.Getter;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
|
||||
/**
|
||||
* Rectangle has coordinates and can be checked for overlap against other Rectangles.
|
||||
*/
|
||||
/** Rectangle has coordinates and can be checked for overlap against other Rectangles. */
|
||||
@Getter
|
||||
@RequiredArgsConstructor
|
||||
public class Rectangle {
|
||||
@@ -40,8 +38,10 @@ public class Rectangle {
|
||||
private final int bottom;
|
||||
|
||||
boolean intersectsWith(Rectangle r) {
|
||||
return !(r.getLeft() > getRight() || r.getRight() < getLeft() || r.getTop() > getBottom() || r
|
||||
.getBottom() < getTop());
|
||||
return !(r.getLeft() > getRight()
|
||||
|| r.getRight() < getLeft()
|
||||
|| r.getTop() > getBottom()
|
||||
|| r.getBottom() < getTop());
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
@@ -24,9 +24,7 @@
|
||||
*/
|
||||
package com.iluwatar.doubledispatch;
|
||||
|
||||
/**
|
||||
* Space station ISS game object.
|
||||
*/
|
||||
/** Space station ISS game object. */
|
||||
public class SpaceStationIss extends SpaceStationMir {
|
||||
|
||||
public SpaceStationIss(int left, int top, int right, int bottom) {
|
||||
|
||||
@@ -27,9 +27,7 @@ package com.iluwatar.doubledispatch;
|
||||
import com.iluwatar.doubledispatch.constants.AppConstants;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
|
||||
/**
|
||||
* Space station Mir game object.
|
||||
*/
|
||||
/** Space station Mir game object. */
|
||||
@Slf4j
|
||||
public class SpaceStationMir extends GameObject {
|
||||
|
||||
@@ -44,10 +42,12 @@ public class SpaceStationMir extends GameObject {
|
||||
|
||||
@Override
|
||||
public void collisionResolve(FlamingAsteroid asteroid) {
|
||||
LOGGER.info(AppConstants.HITS + " {} is damaged! {} is set on fire!", asteroid.getClass()
|
||||
.getSimpleName(),
|
||||
this.getClass().getSimpleName(), this.getClass().getSimpleName(), this.getClass()
|
||||
.getSimpleName());
|
||||
LOGGER.info(
|
||||
AppConstants.HITS + " {} is damaged! {} is set on fire!",
|
||||
asteroid.getClass().getSimpleName(),
|
||||
this.getClass().getSimpleName(),
|
||||
this.getClass().getSimpleName(),
|
||||
this.getClass().getSimpleName());
|
||||
setDamaged(true);
|
||||
setOnFire(true);
|
||||
}
|
||||
@@ -71,7 +71,11 @@ public class SpaceStationMir extends GameObject {
|
||||
}
|
||||
|
||||
private void logHits(GameObject gameObject) {
|
||||
LOGGER.info(AppConstants.HITS, " {} is damaged!", gameObject.getClass().getSimpleName(),
|
||||
this.getClass().getSimpleName(), this.getClass().getSimpleName());
|
||||
LOGGER.info(
|
||||
AppConstants.HITS,
|
||||
" {} is damaged!",
|
||||
gameObject.getClass().getSimpleName(),
|
||||
this.getClass().getSimpleName(),
|
||||
this.getClass().getSimpleName());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
+1
-3
@@ -24,9 +24,7 @@
|
||||
*/
|
||||
package com.iluwatar.doubledispatch.constants;
|
||||
|
||||
/**
|
||||
* Constants class to define all constants.
|
||||
*/
|
||||
/** Constants class to define all constants. */
|
||||
public class AppConstants {
|
||||
|
||||
public static final String HITS = "{} hits {}.";
|
||||
|
||||
Reference in New Issue
Block a user