mirror of
https://github.com/tiennm99/java-design-patterns.git
synced 2026-10-14 02:16:06 +00:00
* fix: add UnsupportedOperationException in KingsHand and GameLoopTest * refactor: update DatabaseService to use constant for BINARY_DATA and remove obsolete test files * refactor: update timePasses method in KingsHand * refactor CandyGame.java * refactor: simplify boundary checks in CandyGame * feat: add getters for type and points in Candy class and initialize logger in CandyGame * remove class and logger initialization in CandyGame * Modify CandyGame.java * fix Checkstyle violations * Remove generic wildcard type in ThreatAwareSystem
This commit is contained in:
1 parent
b375919db5
commit
297e429e5a
9 files changed
+29
-21
No files matched your search
@@ -43,5 +43,7 @@ public class KingsHand extends EventEmitter implements EventObserver {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void timePasses(Weekday day) {
|
public void timePasses(Weekday day) {
|
||||||
|
// This method is intentionally left empty because KingsHand does not handle time-based events directly.
|
||||||
|
// It serves as a placeholder to fulfill the EventObserver interface contract.
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -30,7 +30,7 @@ import java.util.List;
|
|||||||
/**
|
/**
|
||||||
* Represents system that is aware of threats that are present in it.
|
* Represents system that is aware of threats that are present in it.
|
||||||
*/
|
*/
|
||||||
public interface ThreatAwareSystem {
|
public interface ThreatAwareSystem<T extends Threat> {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Returns the system id.
|
* Returns the system id.
|
||||||
@@ -43,13 +43,13 @@ public interface ThreatAwareSystem {
|
|||||||
* Returns list of threats for this system.
|
* Returns list of threats for this system.
|
||||||
* @return list of threats for this system.
|
* @return list of threats for this system.
|
||||||
*/
|
*/
|
||||||
List<? extends Threat> threats();
|
List<T> threats();
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Returns the instance of {@link Filterer} helper interface that allows to covariantly
|
* Returns the instance of {@link Filterer} helper interface that allows to covariantly
|
||||||
* specify lower bound for predicate that we want to filter by.
|
* specify lower bound for predicate that we want to filter by.
|
||||||
* @return an instance of {@link Filterer} helper interface.
|
* @return an instance of {@link Filterer} helper interface.
|
||||||
*/
|
*/
|
||||||
Filterer<? extends ThreatAwareSystem, ? extends Threat> filtered();
|
Filterer<ThreatAwareSystem<T>, T> filtered();
|
||||||
|
|
||||||
}
|
}
|
||||||
@@ -24,10 +24,9 @@
|
|||||||
*/
|
*/
|
||||||
package com.iluwatar.gameloop;
|
package com.iluwatar.gameloop;
|
||||||
|
|
||||||
import static org.junit.jupiter.api.Assertions.assertFalse;
|
|
||||||
|
|
||||||
import org.junit.jupiter.api.AfterEach;
|
import org.junit.jupiter.api.AfterEach;
|
||||||
import org.junit.jupiter.api.Assertions;
|
import org.junit.jupiter.api.Assertions;
|
||||||
|
import static org.junit.jupiter.api.Assertions.assertFalse;
|
||||||
import org.junit.jupiter.api.BeforeEach;
|
import org.junit.jupiter.api.BeforeEach;
|
||||||
import org.junit.jupiter.api.Test;
|
import org.junit.jupiter.api.Test;
|
||||||
|
|
||||||
@@ -46,6 +45,7 @@ class GameLoopTest {
|
|||||||
gameLoop = new GameLoop() {
|
gameLoop = new GameLoop() {
|
||||||
@Override
|
@Override
|
||||||
protected void processGameLoop() {
|
protected void processGameLoop() {
|
||||||
|
throw new UnsupportedOperationException("Not supported yet.");
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -92,7 +92,7 @@ public class DatabaseService {
|
|||||||
throws SQLException {
|
throws SQLException {
|
||||||
try (var connection = dataSource.getConnection();
|
try (var connection = dataSource.getConnection();
|
||||||
var statement = connection.createStatement()) {
|
var statement = connection.createStatement()) {
|
||||||
if (dataTypeDb.equals("BINARY")) {
|
if (dataTypeDb.equals(BINARY_DATA)) {
|
||||||
statement.execute(CREATE_BINARY_SCHEMA_DDL);
|
statement.execute(CREATE_BINARY_SCHEMA_DDL);
|
||||||
} else {
|
} else {
|
||||||
statement.execute(CREATE_TEXT_SCHEMA_DDL);
|
statement.execute(CREATE_TEXT_SCHEMA_DDL);
|
||||||
|
|||||||
@@ -37,16 +37,17 @@ import lombok.NoArgsConstructor;
|
|||||||
import org.w3c.dom.Document;
|
import org.w3c.dom.Document;
|
||||||
import org.w3c.dom.Element;
|
import org.w3c.dom.Element;
|
||||||
import org.w3c.dom.NodeList;
|
import org.w3c.dom.NodeList;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Creates an object Forest which contains animals and plants as its constituents. Animals may eat
|
* Creates an object Forest which contains animals and plants as its constituents. Animals may eat
|
||||||
* plants or other animals in the forest.
|
* plants or other animals in the forest.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
|
||||||
@Data
|
@Data
|
||||||
@NoArgsConstructor
|
@NoArgsConstructor
|
||||||
@AllArgsConstructor
|
@AllArgsConstructor
|
||||||
public class Forest implements Serializable {
|
public class Forest implements Serializable {
|
||||||
|
public static final String HORIZONTAL_DIVIDER = "\n--------------------------\n";
|
||||||
private String name;
|
private String name;
|
||||||
private Set<Animal> animals = new HashSet<>();
|
private Set<Animal> animals = new HashSet<>();
|
||||||
private Set<Plant> plants = new HashSet<>();
|
private Set<Plant> plants = new HashSet<>();
|
||||||
@@ -105,16 +106,16 @@ public class Forest implements Serializable {
|
|||||||
sb.append("Forest Name = ").append(name).append("\n");
|
sb.append("Forest Name = ").append(name).append("\n");
|
||||||
sb.append("Animals found in the ").append(name).append(" Forest: \n");
|
sb.append("Animals found in the ").append(name).append(" Forest: \n");
|
||||||
for (Animal animal : animals) {
|
for (Animal animal : animals) {
|
||||||
sb.append("\n--------------------------\n");
|
sb.append(HORIZONTAL_DIVIDER);
|
||||||
sb.append(animal.toString());
|
sb.append(animal.toString());
|
||||||
sb.append("\n--------------------------\n");
|
sb.append(HORIZONTAL_DIVIDER);
|
||||||
}
|
}
|
||||||
sb.append("\n");
|
sb.append("\n");
|
||||||
sb.append("Plants in the ").append(name).append(" Forest: \n");
|
sb.append("Plants in the ").append(name).append(" Forest: \n");
|
||||||
for (Plant plant : plants) {
|
for (Plant plant : plants) {
|
||||||
sb.append("\n--------------------------\n");
|
sb.append(HORIZONTAL_DIVIDER);
|
||||||
sb.append(plant.toString());
|
sb.append(plant.toString());
|
||||||
sb.append("\n--------------------------\n");
|
sb.append(HORIZONTAL_DIVIDER);
|
||||||
}
|
}
|
||||||
return sb.toString();
|
return sb.toString();
|
||||||
}
|
}
|
||||||
|
|||||||
File renamed without changes.
File renamed without changes.
@@ -1,5 +1,6 @@
|
|||||||
/*
|
/*
|
||||||
* This project is licensed under the MIT license. Module model-view-viewmodel is using ZK framework licensed under LGPL (see lgpl-3.0.txt).
|
* This project is licensed under the MIT license. Module model-view-viewmodel
|
||||||
|
* is using ZK framework licensed under LGPL (see lgpl-3.0.txt).
|
||||||
*
|
*
|
||||||
* The MIT License
|
* The MIT License
|
||||||
* Copyright © 2014-2022 Ilkka Seppälä
|
* Copyright © 2014-2022 Ilkka Seppälä
|
||||||
@@ -27,12 +28,12 @@ package com.iluwatar.typeobject;
|
|||||||
import lombok.extern.slf4j.Slf4j;
|
import lombok.extern.slf4j.Slf4j;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* <p>Type object pattern is the pattern we use when the OOP concept of creating a base class and
|
* Type object pattern is the pattern we use when the OOP concept of creating a base class and
|
||||||
* inheriting from it just doesn't work for the case in hand. This happens when we either don't know
|
* 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
|
* 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
|
* 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.</p>
|
* objects by creating one class, which has a field which represents the 'type' of the object.
|
||||||
* <p>In this example, we have a mini candy-crush game in action. There are many different candies
|
* 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
|
* 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
|
* 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
|
* Type. We have a json file {@link candy} which contains the details about the candies, and this is
|
||||||
@@ -41,7 +42,7 @@ import lombok.extern.slf4j.Slf4j;
|
|||||||
* how crushing can be done, how the matrix is to be reconfigured and how points are to be gained.
|
* 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
|
* 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
|
* 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.</p>
|
* the game and the {@link App} class has the game itself.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
@Slf4j
|
@Slf4j
|
||||||
|
|||||||
@@ -28,6 +28,8 @@ import com.iluwatar.typeobject.Candy.Type;
|
|||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import lombok.extern.slf4j.Slf4j;
|
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
|
* The CandyGame class contains the rules for the continuation of the game and has the game matrix
|
||||||
@@ -37,7 +39,6 @@ import lombok.extern.slf4j.Slf4j;
|
|||||||
@Slf4j
|
@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 {
|
public class CandyGame {
|
||||||
|
|
||||||
Cell[][] cells;
|
Cell[][] cells;
|
||||||
CellPool pool;
|
CellPool pool;
|
||||||
int totalPoints;
|
int totalPoints;
|
||||||
@@ -85,17 +86,20 @@ public class CandyGame {
|
|||||||
if (x == 0) {
|
if (x == 0) {
|
||||||
adjacent.add(this.cells[y][1]);
|
adjacent.add(this.cells[y][1]);
|
||||||
}
|
}
|
||||||
if (y == cells.length - 1) {
|
if (y == cells.length - 1 && cells.length > 1) {
|
||||||
adjacent.add(this.cells[cells.length - 2][x]);
|
adjacent.add(this.cells[cells.length - 2][x]);
|
||||||
}
|
}
|
||||||
if (x == cells.length - 1) {
|
|
||||||
|
if (x == cells.length - 1 && cells.length > 1) {
|
||||||
adjacent.add(this.cells[y][cells.length - 2]);
|
adjacent.add(this.cells[y][cells.length - 2]);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
if (y > 0 && y < cells.length - 1) {
|
if (y > 0 && y < cells.length - 1) {
|
||||||
adjacent.add(this.cells[y - 1][x]);
|
adjacent.add(this.cells[y - 1][x]);
|
||||||
adjacent.add(this.cells[y + 1][x]);
|
adjacent.add(this.cells[y + 1][x]);
|
||||||
}
|
}
|
||||||
if (x > 0 && x < cells.length - 1) {
|
if (y >= 0 && y < cells.length && x > 0 && x < cells[y].length - 1) {
|
||||||
adjacent.add(this.cells[y][x - 1]);
|
adjacent.add(this.cells[y][x - 1]);
|
||||||
adjacent.add(this.cells[y][x + 1]);
|
adjacent.add(this.cells[y][x + 1]);
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in new issue
Block a user