mirror of
https://github.com/tiennm99/java-design-patterns.git
synced 2026-05-19 17:28:48 +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,19 +31,18 @@ import lombok.extern.slf4j.Slf4j;
|
||||
* inheriting from it just doesn't work for the case in hand. This happens when we either don't know
|
||||
* what types we will need upfront, or want to be able to modify or add new types conveniently w/o
|
||||
* recompiling repeatedly. The pattern provides a solution by allowing flexible creation of required
|
||||
* objects by creating one class, which has a field which represents the 'type' of the object.
|
||||
* In this example, we have a mini candy-crush game in action. There are many different candies
|
||||
* in the game, which may change over time, as we may want to upgrade the game. To make the object
|
||||
* creation convenient, we have a class {@link Candy} which has a field name, parent, points and
|
||||
* Type. We have a json file {@link candy} which contains the details about the candies, and this is
|
||||
* parsed to get all the different candies in {@link JsonParser}. The {@link Cell} class is what the
|
||||
* game matrix is made of, which has the candies that are to be crushed, and contains information on
|
||||
* how crushing can be done, how the matrix is to be reconfigured and how points are to be gained.
|
||||
* The {@link CellPool} class is a pool which reuses the candy cells that have been crushed instead
|
||||
* of making new ones repeatedly. The {@link CandyGame} class has the rules for the continuation of
|
||||
* the game and the {@link App} class has the game itself.
|
||||
* objects by creating one class, which has a field which represents the 'type' of the object. In
|
||||
* this example, we have a mini candy-crush game in action. There are many different candies in the
|
||||
* game, which may change over time, as we may want to upgrade the game. To make the object creation
|
||||
* convenient, we have a class {@link Candy} which has a field name, parent, points and Type. We
|
||||
* have a json file {@link candy} which contains the details about the candies, and this is parsed
|
||||
* to get all the different candies in {@link JsonParser}. The {@link Cell} class is what the game
|
||||
* matrix is made of, which has the candies that are to be crushed, and contains information on how
|
||||
* crushing can be done, how the matrix is to be reconfigured and how points are to be gained. The
|
||||
* {@link CellPool} class is a pool which reuses the candy cells that have been crushed instead of
|
||||
* making new ones repeatedly. The {@link CandyGame} class has the rules for the continuation of the
|
||||
* game and the {@link App} class has the game itself.
|
||||
*/
|
||||
|
||||
@Slf4j
|
||||
public class App {
|
||||
|
||||
@@ -53,8 +52,8 @@ public class App {
|
||||
* @param args command line args
|
||||
*/
|
||||
public static void main(String[] args) {
|
||||
var givenTime = 50; //50ms
|
||||
var toWin = 500; //points
|
||||
var givenTime = 50; // 50ms
|
||||
var toWin = 500; // points
|
||||
var pointsWon = 0;
|
||||
var numOfRows = 3;
|
||||
var start = System.currentTimeMillis();
|
||||
|
||||
@@ -44,8 +44,7 @@ public class Candy {
|
||||
Candy parent;
|
||||
String parentName;
|
||||
|
||||
@Setter
|
||||
private int points;
|
||||
@Setter private int points;
|
||||
private final Type type;
|
||||
|
||||
Candy(String name, String parentName, Type type, int points) {
|
||||
@@ -55,5 +54,4 @@ public class Candy {
|
||||
this.points = points;
|
||||
this.parentName = parentName;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -28,16 +28,13 @@ import com.iluwatar.typeobject.Candy.Type;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
/**
|
||||
* The CandyGame class contains the rules for the continuation of the game and has the game matrix
|
||||
* (field 'cells') and totalPoints gained during the game.
|
||||
*/
|
||||
|
||||
@Slf4j
|
||||
@SuppressWarnings("java:S3776") //"Cognitive Complexity of methods should not be too high"
|
||||
@SuppressWarnings("java:S3776") // "Cognitive Complexity of methods should not be too high"
|
||||
public class CandyGame {
|
||||
Cell[][] cells;
|
||||
CellPool pool;
|
||||
@@ -67,8 +64,11 @@ public class CandyGame {
|
||||
var candyName = cell[j].candy.name;
|
||||
if (candyName.length() < 20) {
|
||||
var totalSpaces = 20 - candyName.length();
|
||||
LOGGER.info(numOfSpaces(totalSpaces / 2) + cell[j].candy.name
|
||||
+ numOfSpaces(totalSpaces - totalSpaces / 2) + "|");
|
||||
LOGGER.info(
|
||||
numOfSpaces(totalSpaces / 2)
|
||||
+ cell[j].candy.name
|
||||
+ numOfSpaces(totalSpaces - totalSpaces / 2)
|
||||
+ "|");
|
||||
} else {
|
||||
LOGGER.info(candyName + "|");
|
||||
}
|
||||
@@ -89,12 +89,11 @@ public class CandyGame {
|
||||
if (y == cells.length - 1 && cells.length > 1) {
|
||||
adjacent.add(this.cells[cells.length - 2][x]);
|
||||
}
|
||||
|
||||
|
||||
if (x == cells.length - 1 && cells.length > 1) {
|
||||
adjacent.add(this.cells[y][cells.length - 2]);
|
||||
}
|
||||
|
||||
|
||||
|
||||
if (y > 0 && y < cells.length - 1) {
|
||||
adjacent.add(this.cells[y - 1][x]);
|
||||
adjacent.add(this.cells[y + 1][x]);
|
||||
@@ -173,5 +172,4 @@ public class CandyGame {
|
||||
end = System.currentTimeMillis();
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
@@ -40,7 +40,7 @@ public class Cell {
|
||||
int positionY;
|
||||
|
||||
void crush(CellPool pool, Cell[][] cellMatrix) {
|
||||
//take out from this position and put back in pool
|
||||
// take out from this position and put back in pool
|
||||
pool.addNewCell(this);
|
||||
this.fillThisSpace(pool, cellMatrix);
|
||||
}
|
||||
@@ -67,8 +67,8 @@ public class Cell {
|
||||
}
|
||||
|
||||
int interact(Cell c, CellPool pool, Cell[][] cellMatrix) {
|
||||
if (this.candy.getType().equals(Type.REWARD_FRUIT) || c.candy.getType()
|
||||
.equals(Type.REWARD_FRUIT)) {
|
||||
if (this.candy.getType().equals(Type.REWARD_FRUIT)
|
||||
|| c.candy.getType().equals(Type.REWARD_FRUIT)) {
|
||||
return 0;
|
||||
} else {
|
||||
if (this.candy.name.equals(c.candy.name)) {
|
||||
|
||||
@@ -51,7 +51,7 @@ public class CellPool {
|
||||
this.randomCode = assignRandomCandytypes();
|
||||
} catch (Exception e) {
|
||||
LOGGER.error("Error occurred: ", e);
|
||||
//manually initialising this.randomCode
|
||||
// manually initialising this.randomCode
|
||||
this.randomCode = new Candy[5];
|
||||
randomCode[0] = new Candy("cherry", FRUIT, Type.REWARD_FRUIT, 20);
|
||||
randomCode[1] = new Candy("mango", FRUIT, Type.REWARD_FRUIT, 20);
|
||||
@@ -74,7 +74,7 @@ public class CellPool {
|
||||
}
|
||||
|
||||
void addNewCell(Cell c) {
|
||||
c.candy = randomCode[RANDOM.nextInt(randomCode.length)]; //changing candytype to new
|
||||
c.candy = randomCode[RANDOM.nextInt(randomCode.length)]; // changing candytype to new
|
||||
this.pool.add(c);
|
||||
pointer++;
|
||||
}
|
||||
@@ -82,12 +82,12 @@ public class CellPool {
|
||||
Candy[] assignRandomCandytypes() throws JsonParseException {
|
||||
var jp = new JsonParser();
|
||||
jp.parse();
|
||||
var randomCode = new Candy[jp.candies.size() - 2]; //exclude generic types 'fruit' and 'candy'
|
||||
var randomCode = new Candy[jp.candies.size() - 2]; // exclude generic types 'fruit' and 'candy'
|
||||
var i = 0;
|
||||
for (var e = jp.candies.keys(); e.hasMoreElements(); ) {
|
||||
var s = e.nextElement();
|
||||
if (!s.equals(FRUIT) && !s.equals(CANDY)) {
|
||||
//not generic
|
||||
// not generic
|
||||
randomCode[i] = jp.candies.get(s);
|
||||
i++;
|
||||
}
|
||||
|
||||
@@ -31,10 +31,7 @@ import com.iluwatar.typeobject.Candy.Type;
|
||||
import java.io.InputStreamReader;
|
||||
import java.util.Hashtable;
|
||||
|
||||
/**
|
||||
* The JsonParser class helps parse the json file candy.json to get all the different candies.
|
||||
*/
|
||||
|
||||
/** The JsonParser class helps parse the json file candy.json to get all the different candies. */
|
||||
public class JsonParser {
|
||||
Hashtable<String, Candy> candies;
|
||||
|
||||
@@ -76,5 +73,4 @@ public class JsonParser {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -29,10 +29,7 @@ import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
import com.iluwatar.typeobject.Candy.Type;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
/**
|
||||
* The CandyGameTest class tests the methods in the {@link CandyGame} class.
|
||||
*/
|
||||
|
||||
/** The CandyGameTest class tests the methods in the {@link CandyGame} class. */
|
||||
class CandyGameTest {
|
||||
|
||||
@Test
|
||||
@@ -66,5 +63,4 @@ class CandyGameTest {
|
||||
var noneLeft = cg.continueRound();
|
||||
assertTrue(fruitInLastRow && matchingCandy && !noneLeft);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -29,10 +29,7 @@ import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
import java.util.Hashtable;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
/**
|
||||
* The CellPoolTest class tests the methods in the {@link CellPool} class.
|
||||
*/
|
||||
|
||||
/** The CellPoolTest class tests the methods in the {@link CellPool} class. */
|
||||
class CellPoolTest {
|
||||
|
||||
@Test
|
||||
@@ -48,5 +45,4 @@ class CellPoolTest {
|
||||
}
|
||||
assertTrue(ht.size() == 5 && parentTypes == 0);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -30,9 +30,7 @@ import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
import com.iluwatar.typeobject.Candy.Type;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
/**
|
||||
* The CellTest class tests the methods in the {@link Cell} class.
|
||||
*/
|
||||
/** The CellTest class tests the methods in the {@link Cell} class. */
|
||||
class CellTest {
|
||||
|
||||
@Test
|
||||
|
||||
Reference in New Issue
Block a user