Compare commits
1 Commits
all-contri
...
jacoco-upg
Author | SHA1 | Date | |
---|---|---|---|
d98a3e5b4c |
@ -1404,15 +1404,6 @@
|
|||||||
"contributions": [
|
"contributions": [
|
||||||
"translation"
|
"translation"
|
||||||
]
|
]
|
||||||
},
|
|
||||||
{
|
|
||||||
"login": "richardmr36",
|
|
||||||
"name": "Martel Richard",
|
|
||||||
"avatar_url": "https://avatars.githubusercontent.com/u/19147333?v=4",
|
|
||||||
"profile": "https://github.com/richardmr36",
|
|
||||||
"contributions": [
|
|
||||||
"code"
|
|
||||||
]
|
|
||||||
}
|
}
|
||||||
],
|
],
|
||||||
"contributorsPerLine": 4,
|
"contributorsPerLine": 4,
|
||||||
|
@ -4,18 +4,18 @@
|
|||||||
|
|
||||||
# Design patterns implemented in Java
|
# Design patterns implemented in Java
|
||||||
|
|
||||||

|

|
||||||
[](https://raw.githubusercontent.com/iluwatar/java-design-patterns/master/LICENSE.md)
|
[](https://raw.githubusercontent.com/iluwatar/java-design-patterns/master/LICENSE.md)
|
||||||
[](https://sonarcloud.io/dashboard?id=iluwatar_java-design-patterns)
|
[](https://sonarcloud.io/dashboard?id=iluwatar_java-design-patterns)
|
||||||
[](https://sonarcloud.io/dashboard?id=iluwatar_java-design-patterns)
|
[](https://sonarcloud.io/dashboard?id=iluwatar_java-design-patterns)
|
||||||
[](https://gitter.im/iluwatar/java-design-patterns?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge&utm_content=badge)
|
[](https://gitter.im/iluwatar/java-design-patterns?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge&utm_content=badge)
|
||||||
<!-- ALL-CONTRIBUTORS-BADGE:START - Do not remove or modify this section -->
|
<!-- ALL-CONTRIBUTORS-BADGE:START - Do not remove or modify this section -->
|
||||||
[](#contributors-)
|
[](#contributors-)
|
||||||
<!-- ALL-CONTRIBUTORS-BADGE:END -->
|
<!-- ALL-CONTRIBUTORS-BADGE:END -->
|
||||||
|
|
||||||
<br/>
|
<br/>
|
||||||
|
|
||||||
Read in different language : [**CN**](/zh/README.md), [**KR**](/ko/README.md), [**FR**](/fr/README.md), [**TR**](/tr/README.md), [**AR**](/ar/README.md)
|
Read in different language : [**CN**](/zh/README.md),[**KR**](/ko/README.md),[**FR**](/fr/README.md),[**TR**](/tr/README.md),[**AR**](/ar/README.md),
|
||||||
|
|
||||||
<br/>
|
<br/>
|
||||||
|
|
||||||
@ -306,7 +306,6 @@ This project is licensed under the terms of the MIT license.
|
|||||||
<tr>
|
<tr>
|
||||||
<td align="center"><a href="https://github.com/byoungju94"><img src="https://avatars.githubusercontent.com/u/42516378?v=4?s=100" width="100px;" alt=""/><br /><sub><b>byoungju94</b></sub></a><br /><a href="https://github.com/iluwatar/java-design-patterns/commits?author=byoungju94" title="Code">💻</a></td>
|
<td align="center"><a href="https://github.com/byoungju94"><img src="https://avatars.githubusercontent.com/u/42516378?v=4?s=100" width="100px;" alt=""/><br /><sub><b>byoungju94</b></sub></a><br /><a href="https://github.com/iluwatar/java-design-patterns/commits?author=byoungju94" title="Code">💻</a></td>
|
||||||
<td align="center"><a href="https://github.com/moustafafarhat"><img src="https://avatars.githubusercontent.com/u/38836727?v=4?s=100" width="100px;" alt=""/><br /><sub><b>Moustafa Farhat</b></sub></a><br /><a href="#translation-moustafafarhat" title="Translation">🌍</a></td>
|
<td align="center"><a href="https://github.com/moustafafarhat"><img src="https://avatars.githubusercontent.com/u/38836727?v=4?s=100" width="100px;" alt=""/><br /><sub><b>Moustafa Farhat</b></sub></a><br /><a href="#translation-moustafafarhat" title="Translation">🌍</a></td>
|
||||||
<td align="center"><a href="https://github.com/richardmr36"><img src="https://avatars.githubusercontent.com/u/19147333?v=4?s=100" width="100px;" alt=""/><br /><sub><b>Martel Richard</b></sub></a><br /><a href="https://github.com/iluwatar/java-design-patterns/commits?author=richardmr36" title="Code">💻</a></td>
|
|
||||||
</tr>
|
</tr>
|
||||||
</table>
|
</table>
|
||||||
|
|
||||||
|
@ -1,138 +0,0 @@
|
|||||||
---
|
|
||||||
layout: pattern
|
|
||||||
title: Parameter Object
|
|
||||||
folder: parameter-object
|
|
||||||
permalink: /patterns/parameter-object/
|
|
||||||
categories: Behavioral
|
|
||||||
tags:
|
|
||||||
- Extensibility
|
|
||||||
---
|
|
||||||
|
|
||||||
## Intent
|
|
||||||
|
|
||||||
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.
|
|
||||||
|
|
||||||
## Explanation
|
|
||||||
|
|
||||||
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.
|
|
||||||
|
|
||||||
|
|
||||||
**Programmatic Example**
|
|
||||||
|
|
||||||
Here's the simple `SearchService` class where Method Overloading is used to default values here. To use method overloading, either the number of arguments or argument type has to be different.
|
|
||||||
|
|
||||||
```java
|
|
||||||
public class SearchService {
|
|
||||||
//Method Overloading example. SortOrder is defaulted in this method
|
|
||||||
public String search(String type, String sortBy) {
|
|
||||||
return getQuerySummary(type, sortBy, SortOrder.DESC);
|
|
||||||
}
|
|
||||||
|
|
||||||
/* Method Overloading example. SortBy is defaulted in this method. Note that the type has to be
|
|
||||||
different here to overload the method */
|
|
||||||
public String search(String type, SortOrder sortOrder) {
|
|
||||||
return getQuerySummary(type, "price", sortOrder);
|
|
||||||
}
|
|
||||||
|
|
||||||
private String getQuerySummary(String type, String sortBy, SortOrder sortOrder) {
|
|
||||||
return "Requesting shoes of type \"" + type + "\" sorted by \"" + sortBy + "\" in \""
|
|
||||||
+ sortOrder.getValue() + "ending\" order...";
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
```
|
|
||||||
|
|
||||||
Next we present the `SearchService` with `ParameterObject` created with Builder pattern.
|
|
||||||
|
|
||||||
```java
|
|
||||||
public class SearchService {
|
|
||||||
|
|
||||||
/* Parameter Object example. 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());
|
|
||||||
}
|
|
||||||
|
|
||||||
private String getQuerySummary(String type, String sortBy, SortOrder sortOrder) {
|
|
||||||
return "Requesting shoes of type \"" + type + "\" sorted by \"" + sortBy + "\" in \""
|
|
||||||
+ sortOrder.getValue() + "ending\" order...";
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public class ParameterObject {
|
|
||||||
public static final String DEFAULT_SORT_BY = "price";
|
|
||||||
public static final SortOrder DEFAULT_SORT_ORDER = SortOrder.ASC;
|
|
||||||
|
|
||||||
private String type;
|
|
||||||
private String sortBy = DEFAULT_SORT_BY;
|
|
||||||
private SortOrder sortOrder = DEFAULT_SORT_ORDER;
|
|
||||||
|
|
||||||
private ParameterObject(Builder builder) {
|
|
||||||
type = builder.type;
|
|
||||||
sortBy = builder.sortBy != null && !builder.sortBy.isBlank() ? builder.sortBy : sortBy;
|
|
||||||
sortOrder = builder.sortOrder != null ? builder.sortOrder : sortOrder;
|
|
||||||
}
|
|
||||||
|
|
||||||
public static Builder newBuilder() {
|
|
||||||
return new Builder();
|
|
||||||
}
|
|
||||||
|
|
||||||
//Getters and Setters...
|
|
||||||
|
|
||||||
public static final class Builder {
|
|
||||||
|
|
||||||
private String type;
|
|
||||||
private String sortBy;
|
|
||||||
private SortOrder sortOrder;
|
|
||||||
|
|
||||||
private Builder() {
|
|
||||||
}
|
|
||||||
|
|
||||||
public Builder withType(String type) {
|
|
||||||
this.type = type;
|
|
||||||
return this;
|
|
||||||
}
|
|
||||||
|
|
||||||
public Builder sortBy(String sortBy) {
|
|
||||||
this.sortBy = sortBy;
|
|
||||||
return this;
|
|
||||||
}
|
|
||||||
|
|
||||||
public Builder sortOrder(SortOrder sortOrder) {
|
|
||||||
this.sortOrder = sortOrder;
|
|
||||||
return this;
|
|
||||||
}
|
|
||||||
|
|
||||||
public ParameterObject build() {
|
|
||||||
return new ParameterObject(this);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
```
|
|
||||||
|
|
||||||
## Class diagram
|
|
||||||
|
|
||||||

|
|
||||||
|
|
||||||
## Applicability
|
|
||||||
|
|
||||||
This pattern shows us the way to have default parameters for a method in Java as the language doesn't default parameters feature out of the box.
|
|
||||||
|
|
||||||
## Credits
|
|
||||||
|
|
||||||
- [Does Java have default parameters?](http://dolszewski.com/java/java-default-parameters)
|
|
Binary file not shown.
Before Width: | Height: | Size: 179 KiB |
@ -1,52 +0,0 @@
|
|||||||
@startuml
|
|
||||||
package com.iluwatar.parameter.object {
|
|
||||||
class App {
|
|
||||||
- LOGGER : Logger {static}
|
|
||||||
+ App()
|
|
||||||
+ main(args : String[]) {static}
|
|
||||||
}
|
|
||||||
class ParameterObject {
|
|
||||||
+ DEFAULT_SORT_BY : String {static}
|
|
||||||
+ DEFAULT_SORT_ORDER : SortOrder {static}
|
|
||||||
- sortBy : String
|
|
||||||
- sortOrder : SortOrder
|
|
||||||
- type : String
|
|
||||||
- ParameterObject(builder : Builder)
|
|
||||||
+ getSortBy() : String
|
|
||||||
+ getSortOrder() : SortOrder
|
|
||||||
+ getType() : String
|
|
||||||
+ newBuilder() : Builder {static}
|
|
||||||
+ setSortBy(sortBy : String)
|
|
||||||
+ setSortOrder(sortOrder : SortOrder)
|
|
||||||
+ setType(type : String)
|
|
||||||
}
|
|
||||||
class Builder {
|
|
||||||
- sortBy : String
|
|
||||||
- sortOrder : SortOrder
|
|
||||||
- type : String
|
|
||||||
- Builder()
|
|
||||||
+ build() : ParameterObject
|
|
||||||
+ sortBy(sortBy : String) : Builder
|
|
||||||
+ sortOrder(sortOrder : SortOrder) : Builder
|
|
||||||
+ withType(type : String) : Builder
|
|
||||||
}
|
|
||||||
class SearchService {
|
|
||||||
+ SearchService()
|
|
||||||
- getQuerySummary(type : String, sortBy : String, sortOrder : SortOrder) : String
|
|
||||||
+ search(parameterObject : ParameterObject) : String
|
|
||||||
+ search(type : String, sortBy : String) : String
|
|
||||||
+ search(type : String, sortOrder : SortOrder) : String
|
|
||||||
}
|
|
||||||
enum SortOrder {
|
|
||||||
+ ASC {static}
|
|
||||||
+ DESC {static}
|
|
||||||
- value : String
|
|
||||||
+ getValue() : String
|
|
||||||
+ valueOf(name : String) : SortOrder {static}
|
|
||||||
+ values() : SortOrder[] {static}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
Builder --> "-sortOrder" SortOrder
|
|
||||||
Builder ..+ ParameterObject
|
|
||||||
ParameterObject --> "-DEFAULT_SORT_ORDER" SortOrder
|
|
||||||
@enduml
|
|
@ -1,61 +0,0 @@
|
|||||||
<?xml version="1.0"?>
|
|
||||||
<!--
|
|
||||||
|
|
||||||
The MIT License
|
|
||||||
Copyright © 2014-2019 Ilkka Seppälä
|
|
||||||
|
|
||||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
||||||
of this software and associated documentation files (the "Software"), to deal
|
|
||||||
in the Software without restriction, including without limitation the rights
|
|
||||||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
||||||
copies of the Software, and to permit persons to whom the Software is
|
|
||||||
furnished to do so, subject to the following conditions:
|
|
||||||
|
|
||||||
The above copyright notice and this permission notice shall be included in
|
|
||||||
all copies or substantial portions of the Software.
|
|
||||||
|
|
||||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
||||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
||||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
||||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
||||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
||||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
||||||
THE SOFTWARE.
|
|
||||||
|
|
||||||
-->
|
|
||||||
<project xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" xmlns="http://maven.apache.org/POM/4.0.0"
|
|
||||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
|
|
||||||
<modelVersion>4.0.0</modelVersion>
|
|
||||||
<parent>
|
|
||||||
<groupId>com.iluwatar</groupId>
|
|
||||||
<artifactId>java-design-patterns</artifactId>
|
|
||||||
<version>1.24.0-SNAPSHOT</version>
|
|
||||||
</parent>
|
|
||||||
<artifactId>parameter-object</artifactId>
|
|
||||||
<dependencies>
|
|
||||||
<dependency>
|
|
||||||
<groupId>org.junit.jupiter</groupId>
|
|
||||||
<artifactId>junit-jupiter-engine</artifactId>
|
|
||||||
<scope>test</scope>
|
|
||||||
</dependency>
|
|
||||||
</dependencies>
|
|
||||||
<build>
|
|
||||||
<plugins>
|
|
||||||
<plugin>
|
|
||||||
<groupId>org.apache.maven.plugins</groupId>
|
|
||||||
<artifactId>maven-assembly-plugin</artifactId>
|
|
||||||
<executions>
|
|
||||||
<execution>
|
|
||||||
<configuration>
|
|
||||||
<archive>
|
|
||||||
<manifest>
|
|
||||||
<mainClass>com.iluwatar.parameter.object.App</mainClass>
|
|
||||||
</manifest>
|
|
||||||
</archive>
|
|
||||||
</configuration>
|
|
||||||
</execution>
|
|
||||||
</executions>
|
|
||||||
</plugin>
|
|
||||||
</plugins>
|
|
||||||
</build>
|
|
||||||
</project>
|
|
@ -1,62 +0,0 @@
|
|||||||
/*
|
|
||||||
* The MIT License
|
|
||||||
* Copyright © 2014-2019 Ilkka Seppälä
|
|
||||||
*
|
|
||||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
||||||
* of this software and associated documentation files (the "Software"), to deal
|
|
||||||
* in the Software without restriction, including without limitation the rights
|
|
||||||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
||||||
* copies of the Software, and to permit persons to whom the Software is
|
|
||||||
* furnished to do so, subject to the following conditions:
|
|
||||||
*
|
|
||||||
* The above copyright notice and this permission notice shall be included in
|
|
||||||
* all copies or substantial portions of the Software.
|
|
||||||
*
|
|
||||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
||||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
||||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
||||||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
||||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
||||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
||||||
* THE SOFTWARE.
|
|
||||||
*/
|
|
||||||
|
|
||||||
package com.iluwatar.parameter.object;
|
|
||||||
|
|
||||||
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,
|
|
||||||
* their default values are going to be used.
|
|
||||||
*/
|
|
||||||
public class App {
|
|
||||||
|
|
||||||
private static final Logger LOGGER = LoggerFactory.getLogger(App.class);
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Program entry point.
|
|
||||||
*
|
|
||||||
* @param args command line args
|
|
||||||
*/
|
|
||||||
public static void main(String[] args) {
|
|
||||||
ParameterObject params = ParameterObject.newBuilder()
|
|
||||||
.withType("sneakers")
|
|
||||||
.sortBy("brand")
|
|
||||||
.build();
|
|
||||||
LOGGER.info(params.toString());
|
|
||||||
LOGGER.info(new SearchService().search(params));
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,113 +0,0 @@
|
|||||||
/*
|
|
||||||
* The MIT License
|
|
||||||
* Copyright © 2014-2019 Ilkka Seppälä
|
|
||||||
*
|
|
||||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
||||||
* of this software and associated documentation files (the "Software"), to deal
|
|
||||||
* in the Software without restriction, including without limitation the rights
|
|
||||||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
||||||
* copies of the Software, and to permit persons to whom the Software is
|
|
||||||
* furnished to do so, subject to the following conditions:
|
|
||||||
*
|
|
||||||
* The above copyright notice and this permission notice shall be included in
|
|
||||||
* all copies or substantial portions of the Software.
|
|
||||||
*
|
|
||||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
||||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
||||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
||||||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
||||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
||||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
||||||
* THE SOFTWARE.
|
|
||||||
*/
|
|
||||||
|
|
||||||
package com.iluwatar.parameter.object;
|
|
||||||
|
|
||||||
public class ParameterObject {
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 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.
|
|
||||||
*/
|
|
||||||
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.
|
|
||||||
*/
|
|
||||||
private ParameterObject(Builder builder) {
|
|
||||||
setType(builder.type);
|
|
||||||
setSortBy(builder.sortBy != null && !builder.sortBy.isBlank() ? builder.sortBy : sortBy);
|
|
||||||
setSortOrder(builder.sortOrder != null ? builder.sortOrder : sortOrder);
|
|
||||||
}
|
|
||||||
|
|
||||||
public static Builder newBuilder() {
|
|
||||||
return new Builder();
|
|
||||||
}
|
|
||||||
|
|
||||||
public String getType() {
|
|
||||||
return type;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void setType(String type) {
|
|
||||||
this.type = type;
|
|
||||||
}
|
|
||||||
|
|
||||||
public String getSortBy() {
|
|
||||||
return sortBy;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void setSortBy(String sortBy) {
|
|
||||||
this.sortBy = sortBy;
|
|
||||||
}
|
|
||||||
|
|
||||||
public SortOrder getSortOrder() {
|
|
||||||
return sortOrder;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void setSortOrder(SortOrder sortOrder) {
|
|
||||||
this.sortOrder = sortOrder;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public String toString() {
|
|
||||||
return String.format("ParameterObject[type='%s', sortBy='%s', sortOrder='%s']",
|
|
||||||
type, sortBy, sortOrder);
|
|
||||||
}
|
|
||||||
|
|
||||||
public static final class Builder {
|
|
||||||
|
|
||||||
private String type;
|
|
||||||
private String sortBy;
|
|
||||||
private SortOrder sortOrder;
|
|
||||||
|
|
||||||
private Builder() {
|
|
||||||
}
|
|
||||||
|
|
||||||
public Builder withType(String type) {
|
|
||||||
this.type = type;
|
|
||||||
return this;
|
|
||||||
}
|
|
||||||
|
|
||||||
public Builder sortBy(String sortBy) {
|
|
||||||
this.sortBy = sortBy;
|
|
||||||
return this;
|
|
||||||
}
|
|
||||||
|
|
||||||
public Builder sortOrder(SortOrder sortOrder) {
|
|
||||||
this.sortOrder = sortOrder;
|
|
||||||
return this;
|
|
||||||
}
|
|
||||||
|
|
||||||
public ParameterObject build() {
|
|
||||||
return new ParameterObject(this);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,60 +0,0 @@
|
|||||||
/*
|
|
||||||
* The MIT License
|
|
||||||
* Copyright © 2014-2019 Ilkka Seppälä
|
|
||||||
*
|
|
||||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
||||||
* of this software and associated documentation files (the "Software"), to deal
|
|
||||||
* in the Software without restriction, including without limitation the rights
|
|
||||||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
||||||
* copies of the Software, and to permit persons to whom the Software is
|
|
||||||
* furnished to do so, subject to the following conditions:
|
|
||||||
*
|
|
||||||
* The above copyright notice and this permission notice shall be included in
|
|
||||||
* all copies or substantial portions of the Software.
|
|
||||||
*
|
|
||||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
||||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
||||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
||||||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
||||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
||||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
||||||
* THE SOFTWARE.
|
|
||||||
*/
|
|
||||||
|
|
||||||
package com.iluwatar.parameter.object;
|
|
||||||
|
|
||||||
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.
|
|
||||||
*/
|
|
||||||
public String search(String type, String sortBy) {
|
|
||||||
return getQuerySummary(type, sortBy, SortOrder.ASC);
|
|
||||||
}
|
|
||||||
|
|
||||||
public String search(String type, SortOrder sortOrder) {
|
|
||||||
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.
|
|
||||||
*/
|
|
||||||
public String search(ParameterObject parameterObject) {
|
|
||||||
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());
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,39 +0,0 @@
|
|||||||
/*
|
|
||||||
* The MIT License
|
|
||||||
* Copyright © 2014-2019 Ilkka Seppälä
|
|
||||||
*
|
|
||||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
||||||
* of this software and associated documentation files (the "Software"), to deal
|
|
||||||
* in the Software without restriction, including without limitation the rights
|
|
||||||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
||||||
* copies of the Software, and to permit persons to whom the Software is
|
|
||||||
* furnished to do so, subject to the following conditions:
|
|
||||||
*
|
|
||||||
* The above copyright notice and this permission notice shall be included in
|
|
||||||
* all copies or substantial portions of the Software.
|
|
||||||
*
|
|
||||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
||||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
||||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
||||||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
||||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
||||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
||||||
* THE SOFTWARE.
|
|
||||||
*/
|
|
||||||
|
|
||||||
package com.iluwatar.parameter.object;
|
|
||||||
|
|
||||||
public enum SortOrder {
|
|
||||||
ASC("asc"),
|
|
||||||
DESC("desc");
|
|
||||||
|
|
||||||
private String value;
|
|
||||||
|
|
||||||
SortOrder(String value) {
|
|
||||||
this.value = value;
|
|
||||||
}
|
|
||||||
|
|
||||||
public String getValue() {
|
|
||||||
return value;
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,41 +0,0 @@
|
|||||||
/*
|
|
||||||
* The MIT License
|
|
||||||
* Copyright © 2014-2019 Ilkka Seppälä
|
|
||||||
*
|
|
||||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
||||||
* of this software and associated documentation files (the "Software"), to deal
|
|
||||||
* in the Software without restriction, including without limitation the rights
|
|
||||||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
||||||
* copies of the Software, and to permit persons to whom the Software is
|
|
||||||
* furnished to do so, subject to the following conditions:
|
|
||||||
*
|
|
||||||
* The above copyright notice and this permission notice shall be included in
|
|
||||||
* all copies or substantial portions of the Software.
|
|
||||||
*
|
|
||||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
||||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
||||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
||||||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
||||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
||||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
||||||
* THE SOFTWARE.
|
|
||||||
*/
|
|
||||||
|
|
||||||
package com.iluwatar.parameter.object;
|
|
||||||
|
|
||||||
import org.junit.jupiter.api.Test;
|
|
||||||
import org.slf4j.Logger;
|
|
||||||
import org.slf4j.LoggerFactory;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Application test
|
|
||||||
*/
|
|
||||||
class AppTest {
|
|
||||||
private static final Logger LOGGER = LoggerFactory.getLogger(AppTest.class);
|
|
||||||
|
|
||||||
@Test
|
|
||||||
void shouldExecuteApplicationWithoutException() {
|
|
||||||
App.main(new String[]{});
|
|
||||||
LOGGER.info("Executed successfully without exception.");
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,63 +0,0 @@
|
|||||||
/*
|
|
||||||
* The MIT License
|
|
||||||
* Copyright © 2014-2019 Ilkka Seppälä
|
|
||||||
*
|
|
||||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
||||||
* of this software and associated documentation files (the "Software"), to deal
|
|
||||||
* in the Software without restriction, including without limitation the rights
|
|
||||||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
||||||
* copies of the Software, and to permit persons to whom the Software is
|
|
||||||
* furnished to do so, subject to the following conditions:
|
|
||||||
*
|
|
||||||
* The above copyright notice and this permission notice shall be included in
|
|
||||||
* all copies or substantial portions of the Software.
|
|
||||||
*
|
|
||||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
||||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
||||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
||||||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
||||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
||||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
||||||
* THE SOFTWARE.
|
|
||||||
*/
|
|
||||||
|
|
||||||
package com.iluwatar.parameter.object;
|
|
||||||
|
|
||||||
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
|
|
||||||
public void testForDefaultSortBy() {
|
|
||||||
//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");
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
public void testForDefaultSortOrder() {
|
|
||||||
//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");
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,63 +0,0 @@
|
|||||||
/*
|
|
||||||
* The MIT License
|
|
||||||
* Copyright © 2014-2019 Ilkka Seppälä
|
|
||||||
*
|
|
||||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
||||||
* of this software and associated documentation files (the "Software"), to deal
|
|
||||||
* in the Software without restriction, including without limitation the rights
|
|
||||||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
||||||
* copies of the Software, and to permit persons to whom the Software is
|
|
||||||
* furnished to do so, subject to the following conditions:
|
|
||||||
*
|
|
||||||
* The above copyright notice and this permission notice shall be included in
|
|
||||||
* all copies or substantial portions of the Software.
|
|
||||||
*
|
|
||||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
||||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
||||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
||||||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
||||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
||||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
||||||
* THE SOFTWARE.
|
|
||||||
*/
|
|
||||||
|
|
||||||
package com.iluwatar.parameter.object;
|
|
||||||
|
|
||||||
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;
|
|
||||||
private SearchService searchService;
|
|
||||||
|
|
||||||
@BeforeEach
|
|
||||||
public void setUp() {
|
|
||||||
//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.
|
|
||||||
*/
|
|
||||||
@Test
|
|
||||||
public void testDefaultParametersMatch() {
|
|
||||||
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.");
|
|
||||||
LOGGER.info("SortOrder Default parameter value matches.");
|
|
||||||
|
|
||||||
LOGGER.info("testDefaultParametersMatch executed successfully without errors.");
|
|
||||||
}
|
|
||||||
}
|
|
1
pom.xml
1
pom.xml
@ -208,7 +208,6 @@
|
|||||||
<module>factory</module>
|
<module>factory</module>
|
||||||
<module>separated-interface</module>
|
<module>separated-interface</module>
|
||||||
<module>special-case</module>
|
<module>special-case</module>
|
||||||
<module>parameter-object</module>
|
|
||||||
</modules>
|
</modules>
|
||||||
|
|
||||||
<repositories>
|
<repositories>
|
||||||
|
Reference in New Issue
Block a user