mirror of
https://github.com/tiennm99/java-design-patterns.git
synced 2026-05-14 08:58:26 +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:
@@ -34,6 +34,14 @@
|
||||
</parent>
|
||||
<artifactId>parameter-object</artifactId>
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>org.slf4j</groupId>
|
||||
<artifactId>slf4j-api</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>ch.qos.logback</groupId>
|
||||
<artifactId>logback-classic</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.junit.jupiter</groupId>
|
||||
<artifactId>junit-jupiter-engine</artifactId>
|
||||
|
||||
@@ -28,19 +28,18 @@ import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
/**
|
||||
* The syntax of Java language doesn’t allow you to declare a method with a predefined value
|
||||
* for a parameter. Probably the best option to achieve default method parameters in Java is
|
||||
* by using the method overloading. Method overloading allows you to declare several methods
|
||||
* with the same name but with a different number of parameters. But the main problem with
|
||||
* method overloading as a solution for default parameter values reveals itself when a method
|
||||
* accepts multiple parameters. Creating an overloaded method for each possible combination of
|
||||
* parameters might be cumbersome. To deal with this issue, the Parameter Object pattern is used.
|
||||
* The Parameter Object is simply a wrapper object for all parameters of a method.
|
||||
* It is nothing more than just a regular POJO. The advantage of the Parameter Object over a
|
||||
* regular method parameter list is the fact that class fields can have default values.
|
||||
* Once the wrapper class is created for the method parameter list, a corresponding builder class
|
||||
* is also created. Usually it's an inner static class. The final step is to use the builder
|
||||
* to construct a new parameter object. For those parameters that are skipped,
|
||||
* The syntax of Java language doesn’t allow you to declare a method with a predefined value for a
|
||||
* parameter. Probably the best option to achieve default method parameters in Java is by using the
|
||||
* method overloading. Method overloading allows you to declare several methods with the same name
|
||||
* but with a different number of parameters. But the main problem with method overloading as a
|
||||
* solution for default parameter values reveals itself when a method accepts multiple parameters.
|
||||
* Creating an overloaded method for each possible combination of parameters might be cumbersome. To
|
||||
* deal with this issue, the Parameter Object pattern is used. The Parameter Object is simply a
|
||||
* wrapper object for all parameters of a method. It is nothing more than just a regular POJO. The
|
||||
* advantage of the Parameter Object over a regular method parameter list is the fact that class
|
||||
* fields can have default values. Once the wrapper class is created for the method parameter list,
|
||||
* a corresponding builder class is also created. Usually it's an inner static class. The final step
|
||||
* is to use the builder to construct a new parameter object. For those parameters that are skipped,
|
||||
* their default values are going to be used.
|
||||
*/
|
||||
public class App {
|
||||
@@ -53,10 +52,8 @@ public class App {
|
||||
* @param args command line args
|
||||
*/
|
||||
public static void main(String[] args) {
|
||||
ParameterObject params = ParameterObject.newBuilder()
|
||||
.withType("sneakers")
|
||||
.sortBy("brand")
|
||||
.build();
|
||||
ParameterObject params =
|
||||
ParameterObject.newBuilder().withType("sneakers").sortBy("brand").build();
|
||||
LOGGER.info(params.toString());
|
||||
LOGGER.info(new SearchService().search(params));
|
||||
}
|
||||
|
||||
@@ -27,30 +27,24 @@ package com.iluwatar.parameter.object;
|
||||
import lombok.Getter;
|
||||
import lombok.Setter;
|
||||
|
||||
/**
|
||||
* ParameterObject.
|
||||
*/
|
||||
/** ParameterObject. */
|
||||
@Getter
|
||||
@Setter
|
||||
public class ParameterObject {
|
||||
|
||||
/**
|
||||
* Default values are defined here.
|
||||
*/
|
||||
/** Default values are defined here. */
|
||||
public static final String DEFAULT_SORT_BY = "price";
|
||||
|
||||
public static final SortOrder DEFAULT_SORT_ORDER = SortOrder.ASC;
|
||||
|
||||
private String type;
|
||||
|
||||
/**
|
||||
* Default values are assigned here.
|
||||
*/
|
||||
/** Default values are assigned here. */
|
||||
private String sortBy = DEFAULT_SORT_BY;
|
||||
|
||||
private SortOrder sortOrder = DEFAULT_SORT_ORDER;
|
||||
|
||||
/**
|
||||
* Overriding default values on object creation only when builder object has a valid value.
|
||||
*/
|
||||
/** Overriding default values on object creation only when builder object has a valid value. */
|
||||
private ParameterObject(Builder builder) {
|
||||
setType(builder.type);
|
||||
setSortBy(builder.sortBy != null && !builder.sortBy.isBlank() ? builder.sortBy : sortBy);
|
||||
@@ -63,21 +57,18 @@ public class ParameterObject {
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return String.format("ParameterObject[type='%s', sortBy='%s', sortOrder='%s']",
|
||||
type, sortBy, sortOrder);
|
||||
return String.format(
|
||||
"ParameterObject[type='%s', sortBy='%s', sortOrder='%s']", type, sortBy, sortOrder);
|
||||
}
|
||||
|
||||
/**
|
||||
* Builder for ParameterObject.
|
||||
*/
|
||||
/** Builder for ParameterObject. */
|
||||
public static final class Builder {
|
||||
|
||||
private String type;
|
||||
private String sortBy;
|
||||
private SortOrder sortOrder;
|
||||
|
||||
private Builder() {
|
||||
}
|
||||
private Builder() {}
|
||||
|
||||
public Builder withType(String type) {
|
||||
this.type = type;
|
||||
|
||||
@@ -24,17 +24,15 @@
|
||||
*/
|
||||
package com.iluwatar.parameter.object;
|
||||
|
||||
/**
|
||||
* SearchService to demonstrate parameter object pattern.
|
||||
*/
|
||||
/** SearchService to demonstrate parameter object pattern. */
|
||||
public class SearchService {
|
||||
|
||||
/**
|
||||
* Below two methods of name `search` is overloaded so that we can send a default value for
|
||||
* one of the criteria and call the final api. A default SortOrder is sent in the first method
|
||||
* and a default SortBy is sent in the second method. So two separate method definitions are
|
||||
* needed for having default values for one argument in each case. Hence, multiple overloaded
|
||||
* methods are needed as the number of argument increases.
|
||||
* Below two methods of name `search` is overloaded so that we can send a default value for one of
|
||||
* the criteria and call the final api. A default SortOrder is sent in the first method and a
|
||||
* default SortBy is sent in the second method. So two separate method definitions are needed for
|
||||
* having default values for one argument in each case. Hence, multiple overloaded methods are
|
||||
* needed as the number of argument increases.
|
||||
*/
|
||||
public String search(String type, String sortBy) {
|
||||
return getQuerySummary(type, sortBy, SortOrder.ASC);
|
||||
@@ -44,21 +42,19 @@ public class SearchService {
|
||||
return getQuerySummary(type, "price", sortOrder);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* The need for multiple method definitions can be avoided by the Parameter Object pattern.
|
||||
* Below is the example where only one method is required and all the logic for having default
|
||||
* values are abstracted into the Parameter Object at the time of object creation.
|
||||
* The need for multiple method definitions can be avoided by the Parameter Object pattern. Below
|
||||
* is the example where only one method is required and all the logic for having default values
|
||||
* are abstracted into the Parameter Object at the time of object creation.
|
||||
*/
|
||||
public String search(ParameterObject parameterObject) {
|
||||
return getQuerySummary(parameterObject.getType(), parameterObject.getSortBy(),
|
||||
parameterObject.getSortOrder());
|
||||
return getQuerySummary(
|
||||
parameterObject.getType(), parameterObject.getSortBy(), parameterObject.getSortOrder());
|
||||
}
|
||||
|
||||
private String getQuerySummary(String type, String sortBy, SortOrder sortOrder) {
|
||||
return String.format("Requesting shoes of type \"%s\" sorted by \"%s\" in \"%sending\" order..",
|
||||
type,
|
||||
sortBy,
|
||||
sortOrder.getValue());
|
||||
return String.format(
|
||||
"Requesting shoes of type \"%s\" sorted by \"%s\" in \"%sending\" order..",
|
||||
type, sortBy, sortOrder.getValue());
|
||||
}
|
||||
}
|
||||
|
||||
@@ -26,15 +26,12 @@ package com.iluwatar.parameter.object;
|
||||
|
||||
import lombok.Getter;
|
||||
|
||||
/**
|
||||
* enum for sort order types.
|
||||
*/
|
||||
/** enum for sort order types. */
|
||||
public enum SortOrder {
|
||||
ASC("asc"),
|
||||
DESC("desc");
|
||||
|
||||
@Getter
|
||||
private String value;
|
||||
@Getter private String value;
|
||||
|
||||
SortOrder(String value) {
|
||||
this.value = value;
|
||||
|
||||
@@ -28,12 +28,10 @@ import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
/**
|
||||
* Application test
|
||||
*/
|
||||
/** Application test */
|
||||
class AppTest {
|
||||
@Test
|
||||
void shouldExecuteApplicationWithoutException() {
|
||||
assertDoesNotThrow(() -> App.main(new String[]{}));
|
||||
assertDoesNotThrow(() -> App.main(new String[] {}));
|
||||
}
|
||||
}
|
||||
|
||||
+17
-20
@@ -24,41 +24,38 @@
|
||||
*/
|
||||
package com.iluwatar.parameter.object;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
|
||||
class ParameterObjectTest {
|
||||
|
||||
private static final Logger LOGGER = LoggerFactory.getLogger(ParameterObjectTest.class);
|
||||
|
||||
@Test
|
||||
void testForDefaultSortBy() {
|
||||
//Creating parameter object with default value for SortBy set
|
||||
ParameterObject params = ParameterObject.newBuilder()
|
||||
.withType("sneakers")
|
||||
.sortOrder(SortOrder.DESC)
|
||||
.build();
|
||||
// Creating parameter object with default value for SortBy set
|
||||
ParameterObject params =
|
||||
ParameterObject.newBuilder().withType("sneakers").sortOrder(SortOrder.DESC).build();
|
||||
|
||||
assertEquals(ParameterObject.DEFAULT_SORT_BY, params.getSortBy(),
|
||||
"Default SortBy is not set.");
|
||||
LOGGER.info("{} Default parameter value is set during object creation as no value is passed."
|
||||
, "SortBy");
|
||||
assertEquals(ParameterObject.DEFAULT_SORT_BY, params.getSortBy(), "Default SortBy is not set.");
|
||||
LOGGER.info(
|
||||
"{} Default parameter value is set during object creation as no value is passed.",
|
||||
"SortBy");
|
||||
}
|
||||
|
||||
@Test
|
||||
void testForDefaultSortOrder() {
|
||||
//Creating parameter object with default value for SortOrder set
|
||||
ParameterObject params = ParameterObject.newBuilder()
|
||||
.withType("sneakers")
|
||||
.sortBy("brand")
|
||||
.build();
|
||||
// Creating parameter object with default value for SortOrder set
|
||||
ParameterObject params =
|
||||
ParameterObject.newBuilder().withType("sneakers").sortBy("brand").build();
|
||||
|
||||
assertEquals(ParameterObject.DEFAULT_SORT_ORDER, params.getSortOrder(),
|
||||
"Default SortOrder is not set.");
|
||||
LOGGER.info("{} Default parameter value is set during object creation as no value is passed."
|
||||
, "SortOrder");
|
||||
assertEquals(
|
||||
ParameterObject.DEFAULT_SORT_ORDER, params.getSortOrder(), "Default SortOrder is not set.");
|
||||
LOGGER.info(
|
||||
"{} Default parameter value is set during object creation as no value is passed.",
|
||||
"SortOrder");
|
||||
}
|
||||
}
|
||||
|
||||
+13
-13
@@ -24,13 +24,13 @@
|
||||
*/
|
||||
package com.iluwatar.parameter.object;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
|
||||
class SearchServiceTest {
|
||||
private static final Logger LOGGER = LoggerFactory.getLogger(SearchServiceTest.class);
|
||||
private ParameterObject parameterObject;
|
||||
@@ -38,25 +38,25 @@ class SearchServiceTest {
|
||||
|
||||
@BeforeEach
|
||||
void setUp() {
|
||||
//Creating parameter object with default values set
|
||||
parameterObject = ParameterObject.newBuilder()
|
||||
.withType("sneakers")
|
||||
.build();
|
||||
// Creating parameter object with default values set
|
||||
parameterObject = ParameterObject.newBuilder().withType("sneakers").build();
|
||||
|
||||
searchService = new SearchService();
|
||||
}
|
||||
|
||||
/**
|
||||
* Testing parameter object against the overloaded method to verify if the behaviour is same.
|
||||
*/
|
||||
/** Testing parameter object against the overloaded method to verify if the behaviour is same. */
|
||||
@Test
|
||||
void testDefaultParametersMatch() {
|
||||
assertEquals(searchService.search(parameterObject), searchService.search("sneakers",
|
||||
SortOrder.ASC), "Default Parameter values do not not match.");
|
||||
assertEquals(
|
||||
searchService.search(parameterObject),
|
||||
searchService.search("sneakers", SortOrder.ASC),
|
||||
"Default Parameter values do not not match.");
|
||||
LOGGER.info("SortBy Default parameter value matches.");
|
||||
|
||||
assertEquals(searchService.search(parameterObject), searchService.search("sneakers",
|
||||
"price"), "Default Parameter values do not not match.");
|
||||
assertEquals(
|
||||
searchService.search(parameterObject),
|
||||
searchService.search("sneakers", "price"),
|
||||
"Default Parameter values do not not match.");
|
||||
LOGGER.info("SortOrder Default parameter value matches.");
|
||||
|
||||
LOGGER.info("testDefaultParametersMatch executed successfully without errors.");
|
||||
|
||||
Reference in New Issue
Block a user