mirror of
https://github.com/tiennm99/java-design-patterns.git
synced 2026-05-29 20:23:07 +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:
@@ -30,39 +30,36 @@ import java.util.logging.Logger;
|
||||
* The main entry point of the application demonstrating the use of vehicles.
|
||||
*
|
||||
* <p>The Table Inheritance pattern models a class hierarchy in a relational database by creating
|
||||
* separate tables for each class in the hierarchy. These tables share a common primary key, which in
|
||||
* subclass tables also serves as a foreign key referencing the primary key of the base class table.
|
||||
* This linkage maintains relationships and effectively represents the inheritance structure. This
|
||||
* pattern enables the organization of complex data models, particularly when subclasses have unique
|
||||
* properties that must be stored in distinct tables.
|
||||
* separate tables for each class in the hierarchy. These tables share a common primary key, which
|
||||
* in subclass tables also serves as a foreign key referencing the primary key of the base class
|
||||
* table. This linkage maintains relationships and effectively represents the inheritance structure.
|
||||
* This pattern enables the organization of complex data models, particularly when subclasses have
|
||||
* unique properties that must be stored in distinct tables.
|
||||
*/
|
||||
|
||||
public class App {
|
||||
/**
|
||||
* Manages the storage and retrieval of Vehicle objects, including Cars and Trucks.
|
||||
*
|
||||
* <p>This example demonstrates the **Table Inheritance** pattern, where each vehicle type
|
||||
* (Car and Truck) is stored in its own separate table. The `VehicleDatabase` simulates
|
||||
* a simple database that manages these entities, with each subclass (Car and Truck)
|
||||
* being stored in its respective table.
|
||||
* <p>This example demonstrates the **Table Inheritance** pattern, where each vehicle type (Car
|
||||
* and Truck) is stored in its own separate table. The `VehicleDatabase` simulates a simple
|
||||
* database that manages these entities, with each subclass (Car and Truck) being stored in its
|
||||
* respective table.
|
||||
*
|
||||
* <p>The `VehicleDatabase` contains the following tables:
|
||||
* - `vehicleTable`: Stores all vehicle objects, including both `Car` and `Truck` objects.
|
||||
* - `carTable`: Stores only `Car` objects, with fields specific to cars.
|
||||
* - `truckTable`: Stores only `Truck` objects, with fields specific to trucks.
|
||||
* <p>The `VehicleDatabase` contains the following tables: - `vehicleTable`: Stores all vehicle
|
||||
* objects, including both `Car` and `Truck` objects. - `carTable`: Stores only `Car` objects,
|
||||
* with fields specific to cars. - `truckTable`: Stores only `Truck` objects, with fields specific
|
||||
* to trucks.
|
||||
*
|
||||
* <p>The example demonstrates:
|
||||
* 1. Saving instances of `Car` and `Truck` to their respective tables in the database.
|
||||
* 2. Retrieving vehicles (both cars and trucks) from the appropriate table based on their ID.
|
||||
* 3. Printing all vehicles stored in the database.
|
||||
* 4. Showing how to retrieve specific types of vehicles (`Car` or `Truck`) by their IDs.
|
||||
* <p>The example demonstrates: 1. Saving instances of `Car` and `Truck` to their respective
|
||||
* tables in the database. 2. Retrieving vehicles (both cars and trucks) from the appropriate
|
||||
* table based on their ID. 3. Printing all vehicles stored in the database. 4. Showing how to
|
||||
* retrieve specific types of vehicles (`Car` or `Truck`) by their IDs.
|
||||
*
|
||||
* <p>In the **Table Inheritance** pattern, each subclass has its own table, making it easier
|
||||
* to manage specific attributes of each subclass.
|
||||
* <p>In the **Table Inheritance** pattern, each subclass has its own table, making it easier to
|
||||
* manage specific attributes of each subclass.
|
||||
*
|
||||
* @param args command-line arguments
|
||||
*/
|
||||
|
||||
public static void main(String[] args) {
|
||||
|
||||
final Logger logger = Logger.getLogger(App.class.getName());
|
||||
@@ -84,6 +81,5 @@ public class App {
|
||||
logger.info(String.format("Retrieved Vehicle: %s", vehicle));
|
||||
logger.info(String.format("Retrieved Car: %s", retrievedCar));
|
||||
logger.info(String.format("Retrieved Truck: %s", retrievedTruck));
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
@@ -23,11 +23,10 @@
|
||||
* THE SOFTWARE.
|
||||
*/
|
||||
package com.iluwatar.table.inheritance;
|
||||
import lombok.Getter;
|
||||
/**
|
||||
* Represents a car with a specific number of doors.
|
||||
*/
|
||||
|
||||
import lombok.Getter;
|
||||
|
||||
/** Represents a car with a specific number of doors. */
|
||||
@Getter
|
||||
public class Car extends Vehicle {
|
||||
private int numDoors;
|
||||
@@ -35,11 +34,11 @@ public class Car extends Vehicle {
|
||||
/**
|
||||
* Constructs a Car object.
|
||||
*
|
||||
* @param year the manufacturing year
|
||||
* @param make the make of the car
|
||||
* @param model the model of the car
|
||||
* @param year the manufacturing year
|
||||
* @param make the make of the car
|
||||
* @param model the model of the car
|
||||
* @param numDoors the number of doors
|
||||
* @param id the unique identifier for the car
|
||||
* @param id the unique identifier for the car
|
||||
*/
|
||||
public Car(int year, String make, String model, int numDoors, int id) {
|
||||
super(year, make, model, id);
|
||||
@@ -64,11 +63,18 @@ public class Car extends Vehicle {
|
||||
@Override
|
||||
public String toString() {
|
||||
return "Car{"
|
||||
+ "id=" + getId()
|
||||
+ ", make='" + getMake() + '\''
|
||||
+ ", model='" + getModel() + '\''
|
||||
+ ", year=" + getYear()
|
||||
+ ", numberOfDoors=" + getNumDoors()
|
||||
+ "id="
|
||||
+ getId()
|
||||
+ ", make='"
|
||||
+ getMake()
|
||||
+ '\''
|
||||
+ ", model='"
|
||||
+ getModel()
|
||||
+ '\''
|
||||
+ ", year="
|
||||
+ getYear()
|
||||
+ ", numberOfDoors="
|
||||
+ getNumDoors()
|
||||
+ '}';
|
||||
}
|
||||
}
|
||||
|
||||
@@ -26,9 +26,7 @@ package com.iluwatar.table.inheritance;
|
||||
|
||||
import lombok.Getter;
|
||||
|
||||
/**
|
||||
* Represents a truck, a type of vehicle with a specific load capacity.
|
||||
*/
|
||||
/** Represents a truck, a type of vehicle with a specific load capacity. */
|
||||
@Getter
|
||||
public class Truck extends Vehicle {
|
||||
private double loadCapacity;
|
||||
@@ -36,11 +34,11 @@ public class Truck extends Vehicle {
|
||||
/**
|
||||
* Constructs a Truck object with the given parameters.
|
||||
*
|
||||
* @param year the year of manufacture
|
||||
* @param make the make of the truck
|
||||
* @param model the model of the truck
|
||||
* @param year the year of manufacture
|
||||
* @param make the make of the truck
|
||||
* @param model the model of the truck
|
||||
* @param loadCapacity the load capacity of the truck
|
||||
* @param id the unique ID of the truck
|
||||
* @param id the unique ID of the truck
|
||||
*/
|
||||
public Truck(int year, String make, String model, double loadCapacity, int id) {
|
||||
super(year, make, model, id);
|
||||
@@ -70,12 +68,18 @@ public class Truck extends Vehicle {
|
||||
@Override
|
||||
public String toString() {
|
||||
return "Truck{"
|
||||
+ "id=" + getId()
|
||||
+ ", make='" + getMake() + '\''
|
||||
+ ", model='" + getModel() + '\''
|
||||
+ ", year=" + getYear()
|
||||
+ ", payloadCapacity=" + getLoadCapacity()
|
||||
+ "id="
|
||||
+ getId()
|
||||
+ ", make='"
|
||||
+ getMake()
|
||||
+ '\''
|
||||
+ ", model='"
|
||||
+ getModel()
|
||||
+ '\''
|
||||
+ ", year="
|
||||
+ getYear()
|
||||
+ ", payloadCapacity="
|
||||
+ getLoadCapacity()
|
||||
+ '}';
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -27,10 +27,7 @@ package com.iluwatar.table.inheritance;
|
||||
import lombok.Getter;
|
||||
import lombok.Setter;
|
||||
|
||||
/**
|
||||
* Represents a generic vehicle with basic attributes like make, model, year, and ID.
|
||||
*/
|
||||
|
||||
/** Represents a generic vehicle with basic attributes like make, model, year, and ID. */
|
||||
@Setter
|
||||
@Getter
|
||||
public class Vehicle {
|
||||
@@ -43,10 +40,10 @@ public class Vehicle {
|
||||
/**
|
||||
* Constructs a Vehicle object with the given parameters.
|
||||
*
|
||||
* @param year the year of manufacture
|
||||
* @param make the make of the vehicle
|
||||
* @param year the year of manufacture
|
||||
* @param make the make of the vehicle
|
||||
* @param model the model of the vehicle
|
||||
* @param id the unique ID of the vehicle
|
||||
* @param id the unique ID of the vehicle
|
||||
*/
|
||||
public Vehicle(int year, String make, String model, int id) {
|
||||
this.make = make;
|
||||
@@ -63,10 +60,16 @@ public class Vehicle {
|
||||
@Override
|
||||
public String toString() {
|
||||
return "Vehicle{"
|
||||
+ "id=" + id
|
||||
+ ", make='" + make + '\''
|
||||
+ ", model='" + model + '\''
|
||||
+ ", year=" + year
|
||||
+ "id="
|
||||
+ id
|
||||
+ ", make='"
|
||||
+ make
|
||||
+ '\''
|
||||
+ ", model='"
|
||||
+ model
|
||||
+ '\''
|
||||
+ ", year="
|
||||
+ year
|
||||
+ '}';
|
||||
}
|
||||
}
|
||||
|
||||
+4
-10
@@ -24,15 +24,11 @@
|
||||
*/
|
||||
package com.iluwatar.table.inheritance;
|
||||
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
import java.util.logging.Logger;
|
||||
|
||||
|
||||
/**
|
||||
* Manages the storage and retrieval of Vehicle objects, including Cars and Trucks.
|
||||
*/
|
||||
/** Manages the storage and retrieval of Vehicle objects, including Cars and Trucks. */
|
||||
public class VehicleDatabase {
|
||||
|
||||
final Logger logger = Logger.getLogger(VehicleDatabase.class.getName());
|
||||
@@ -42,7 +38,8 @@ public class VehicleDatabase {
|
||||
private Map<Integer, Truck> truckTable = new HashMap<>();
|
||||
|
||||
/**
|
||||
* Saves a vehicle to the database. If the vehicle is a Car or Truck, it is added to the respective table.
|
||||
* Saves a vehicle to the database. If the vehicle is a Car or Truck, it is added to the
|
||||
* respective table.
|
||||
*
|
||||
* @param vehicle the vehicle to save
|
||||
*/
|
||||
@@ -85,13 +82,10 @@ public class VehicleDatabase {
|
||||
return truckTable.get(id);
|
||||
}
|
||||
|
||||
/**
|
||||
* Prints all vehicles in the database.
|
||||
*/
|
||||
/** Prints all vehicles in the database. */
|
||||
public void printAllVehicles() {
|
||||
for (Vehicle vehicle : vehicleTable.values()) {
|
||||
logger.info(vehicle.toString());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -32,10 +32,7 @@ import java.util.logging.Handler;
|
||||
import java.util.logging.Logger;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
/**
|
||||
* Tests if the main method runs without throwing exceptions and prints expected output.
|
||||
*/
|
||||
|
||||
/** Tests if the main method runs without throwing exceptions and prints expected output. */
|
||||
class AppTest {
|
||||
|
||||
@Test
|
||||
@@ -48,27 +45,24 @@ class AppTest {
|
||||
|
||||
Logger logger = Logger.getLogger(App.class.getName());
|
||||
|
||||
Handler handler = new ConsoleHandler() {
|
||||
@Override
|
||||
public void publish(java.util.logging.LogRecord recordObj) {
|
||||
printStream.println(getFormatter().format(recordObj));
|
||||
}
|
||||
};
|
||||
Handler handler =
|
||||
new ConsoleHandler() {
|
||||
@Override
|
||||
public void publish(java.util.logging.LogRecord recordObj) {
|
||||
printStream.println(getFormatter().format(recordObj));
|
||||
}
|
||||
};
|
||||
handler.setLevel(java.util.logging.Level.ALL);
|
||||
logger.addHandler(handler);
|
||||
|
||||
App.main(new String[]{});
|
||||
App.main(new String[] {});
|
||||
|
||||
String output = outContent.toString();
|
||||
|
||||
assertTrue(output.contains("Retrieved Vehicle:"));
|
||||
assertTrue(output.contains("Toyota")); // Car make
|
||||
assertTrue(output.contains("Ford")); // Truck make
|
||||
assertTrue(output.contains("Toyota")); // Car make
|
||||
assertTrue(output.contains("Ford")); // Truck make
|
||||
assertTrue(output.contains("Retrieved Car:"));
|
||||
assertTrue(output.contains("Retrieved Truck:"));
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
@@ -34,24 +34,20 @@ import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
/**
|
||||
* Unit tests for the {@link VehicleDatabase} class.
|
||||
* Tests saving, retrieving, and printing vehicles of different types.
|
||||
* Unit tests for the {@link VehicleDatabase} class. Tests saving, retrieving, and printing vehicles
|
||||
* of different types.
|
||||
*/
|
||||
class VehicleDatabaseTest {
|
||||
|
||||
private VehicleDatabase vehicleDatabase;
|
||||
|
||||
/**
|
||||
* Sets up a new instance of {@link VehicleDatabase} before each test.
|
||||
*/
|
||||
/** Sets up a new instance of {@link VehicleDatabase} before each test. */
|
||||
@BeforeEach
|
||||
public void setUp() {
|
||||
vehicleDatabase = new VehicleDatabase();
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests saving a {@link Car} to the database and retrieving it.
|
||||
*/
|
||||
/** Tests saving a {@link Car} to the database and retrieving it. */
|
||||
@Test
|
||||
void testSaveAndRetrieveCar() {
|
||||
Car car = new Car(2020, "Toyota", "Corolla", 4, 1);
|
||||
@@ -69,9 +65,7 @@ class VehicleDatabaseTest {
|
||||
assertEquals(car.getNumDoors(), retrievedCar.getNumDoors());
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests saving a {@link Truck} to the database and retrieving it.
|
||||
*/
|
||||
/** Tests saving a {@link Truck} to the database and retrieving it. */
|
||||
@Test
|
||||
void testSaveAndRetrieveTruck() {
|
||||
Truck truck = new Truck(2018, "Ford", "F-150", 60, 2);
|
||||
@@ -89,9 +83,7 @@ class VehicleDatabaseTest {
|
||||
assertEquals(truck.getLoadCapacity(), retrievedTruck.getLoadCapacity());
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests saving multiple vehicles to the database and printing them.
|
||||
*/
|
||||
/** Tests saving multiple vehicles to the database and printing them. */
|
||||
@Test
|
||||
void testPrintAllVehicles() {
|
||||
Car car = new Car(2020, "Toyota", "Corolla", 4, 1);
|
||||
@@ -108,9 +100,7 @@ class VehicleDatabaseTest {
|
||||
assertNotNull(retrievedTruck);
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests the constructor of {@link Car} with valid values.
|
||||
*/
|
||||
/** Tests the constructor of {@link Car} with valid values. */
|
||||
@Test
|
||||
void testCarConstructor() {
|
||||
Car car = new Car(2020, "Toyota", "Corolla", 4, 1);
|
||||
@@ -121,73 +111,77 @@ class VehicleDatabaseTest {
|
||||
assertEquals(1, car.getId()); // Assuming the ID is auto-generated in the constructor
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests the constructor of {@link Car} with invalid number of doors (negative value).
|
||||
*/
|
||||
/** Tests the constructor of {@link Car} with invalid number of doors (negative value). */
|
||||
@Test
|
||||
void testCarConstructorWithInvalidNumDoors() {
|
||||
IllegalArgumentException exception = assertThrows(IllegalArgumentException.class, () -> {
|
||||
new Car(2020, "Toyota", "Corolla", -4, 1);
|
||||
});
|
||||
IllegalArgumentException exception =
|
||||
assertThrows(
|
||||
IllegalArgumentException.class,
|
||||
() -> {
|
||||
new Car(2020, "Toyota", "Corolla", -4, 1);
|
||||
});
|
||||
assertEquals("Number of doors must be positive.", exception.getMessage());
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests the constructor of {@link Car} with zero doors.
|
||||
*/
|
||||
/** Tests the constructor of {@link Car} with zero doors. */
|
||||
@Test
|
||||
void testCarConstructorWithZeroDoors() {
|
||||
IllegalArgumentException exception = assertThrows(IllegalArgumentException.class, () -> {
|
||||
new Car(2020, "Toyota", "Corolla", 0, 1);
|
||||
});
|
||||
IllegalArgumentException exception =
|
||||
assertThrows(
|
||||
IllegalArgumentException.class,
|
||||
() -> {
|
||||
new Car(2020, "Toyota", "Corolla", 0, 1);
|
||||
});
|
||||
assertEquals("Number of doors must be positive.", exception.getMessage());
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests the constructor of {@link Truck} with invalid load capacity (negative value).
|
||||
*/
|
||||
/** Tests the constructor of {@link Truck} with invalid load capacity (negative value). */
|
||||
@Test
|
||||
void testTruckConstructorWithInvalidLoadCapacity() {
|
||||
IllegalArgumentException exception = assertThrows(IllegalArgumentException.class, () -> {
|
||||
new Truck(2018, "Ford", "F-150", -60, 2);
|
||||
});
|
||||
IllegalArgumentException exception =
|
||||
assertThrows(
|
||||
IllegalArgumentException.class,
|
||||
() -> {
|
||||
new Truck(2018, "Ford", "F-150", -60, 2);
|
||||
});
|
||||
assertEquals("Load capacity must be positive.", exception.getMessage());
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests the constructor of {@link Truck} with zero load capacity.
|
||||
*/
|
||||
/** Tests the constructor of {@link Truck} with zero load capacity. */
|
||||
@Test
|
||||
void testTruckConstructorWithZeroLoadCapacity() {
|
||||
IllegalArgumentException exception = assertThrows(IllegalArgumentException.class, () -> {
|
||||
new Truck(2018, "Ford", "F-150", 0, 2);
|
||||
});
|
||||
IllegalArgumentException exception =
|
||||
assertThrows(
|
||||
IllegalArgumentException.class,
|
||||
() -> {
|
||||
new Truck(2018, "Ford", "F-150", 0, 2);
|
||||
});
|
||||
assertEquals("Load capacity must be positive.", exception.getMessage());
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests setting invalid number of doors in {@link Car} using setter (negative value).
|
||||
*/
|
||||
/** Tests setting invalid number of doors in {@link Car} using setter (negative value). */
|
||||
@Test
|
||||
void testSetInvalidNumDoors() {
|
||||
Car car = new Car(2020, "Toyota", "Corolla", 4, 1);
|
||||
IllegalArgumentException exception = assertThrows(IllegalArgumentException.class, () -> {
|
||||
car.setNumDoors(-2);
|
||||
});
|
||||
IllegalArgumentException exception =
|
||||
assertThrows(
|
||||
IllegalArgumentException.class,
|
||||
() -> {
|
||||
car.setNumDoors(-2);
|
||||
});
|
||||
assertEquals("Number of doors must be positive.", exception.getMessage());
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests setting invalid load capacity in {@link Truck} using setter (negative value).
|
||||
*/
|
||||
/** Tests setting invalid load capacity in {@link Truck} using setter (negative value). */
|
||||
@Test
|
||||
void testSetInvalidLoadCapacity() {
|
||||
Truck truck = new Truck(2018, "Ford", "F-150", 60, 2);
|
||||
IllegalArgumentException exception = assertThrows(IllegalArgumentException.class, () -> {
|
||||
truck.setLoadCapacity(-10);
|
||||
});
|
||||
IllegalArgumentException exception =
|
||||
assertThrows(
|
||||
IllegalArgumentException.class,
|
||||
() -> {
|
||||
truck.setLoadCapacity(-10);
|
||||
});
|
||||
assertEquals("Load capacity must be positive.", exception.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user