Compare commits
155 Commits
Author | SHA1 | Date | |
---|---|---|---|
73f9b8bef1 | |||
19b129c28e | |||
63c707fcb1 | |||
cc4b990ae8 | |||
8747f1fd7a | |||
9e58edf05e | |||
271d7ae9bd | |||
26f4aa8001 | |||
1fdc650545 | |||
3ccc9baa1a | |||
df8a4e3b47 | |||
cc571f4149 | |||
51b444dccf | |||
50467c9e76 | |||
87af122509 | |||
160b737dcc | |||
f04fc3c0dc | |||
3c57bf7078 | |||
33ea7335b1 | |||
6ef840f3cf | |||
1e76d91929 | |||
390795154f | |||
37599eb48f | |||
c954a436ad | |||
2628cc0dfc | |||
0272d71851 | |||
329479dda0 | |||
c4418311c6 | |||
f0f0143d48 | |||
b92eb5229d | |||
9c8ad4485b | |||
4dae1fae57 | |||
5ae2ce6e2e | |||
7c888e8886 | |||
f2c91eb836 | |||
01e489c77b | |||
eae09fc07e | |||
7f06f3b78c | |||
dda09535e6 | |||
4f9ee0189c | |||
de56cbb971 | |||
2f49648047 | |||
31f27a720b | |||
efc17fcc70 | |||
6d1c0b1563 | |||
74c7273381 | |||
8f85353baf | |||
09b4663b9a | |||
1fa8a604eb | |||
50986fa15b | |||
8ecdee44c8 | |||
47ae477a56 | |||
7dc47da131 | |||
fbcfeb072a | |||
ffeee2f1a4 | |||
1b32e23493 | |||
3a4677c56d | |||
c4c37d77e9 | |||
0b17abdf11 | |||
7ac47b291d | |||
564cf1239f | |||
768e647108 | |||
a8c7771784 | |||
1d4a7681e2 | |||
55b0341c8d | |||
3b1a28149b | |||
1eb1961f1b | |||
cc85c73fbc | |||
cdb80b8ddd | |||
a9dfd7e809 | |||
fca7e9c8c7 | |||
91a085d3d1 | |||
ff52848481 | |||
ccc1a6340b | |||
a65a601835 | |||
c8a481bb77 | |||
dd971d8c19 | |||
6bb3438965 | |||
63fb8dc318 | |||
b50189e283 | |||
9f7e3fe552 | |||
d1767bbb51 | |||
5d47488fe9 | |||
d01a3a66b4 | |||
e3827945c8 | |||
fadad43f8b | |||
c7fee7bb07 | |||
954e7300e9 | |||
03e1e92b52 | |||
af8c5ed0dd | |||
349d8edebf | |||
12f13a631e | |||
0b8d78e4bf | |||
58d8fa1758 | |||
f7a53f2d17 | |||
70ddeaa194 | |||
6aeafcf2aa | |||
1cb1bdcb57 | |||
1b6cecda19 | |||
9403d53096 | |||
630532cdda | |||
acaa6cdc62 | |||
4904d7eea0 | |||
c81c3ff1c7 | |||
5fc03ee9f8 | |||
20b4195fb2 | |||
f00ebe1a8d | |||
2217fbc5ff | |||
82f9a6c232 | |||
f671f03d13 | |||
f09a7eb468 | |||
7c5d5f6b0d | |||
8a4844792f | |||
0c60a95d8f | |||
c3656109a7 | |||
a204383f45 | |||
2982db456d | |||
c308ca09e5 | |||
880b234d8f | |||
a5646b63c1 | |||
27c131c2cb | |||
c438ec0557 | |||
dd5997b495 | |||
e6c71b63fc | |||
7e698a90dd | |||
088bb764f3 | |||
47d92bbffb | |||
cfdfedbd2e | |||
72b174619f | |||
9ebbc421dd | |||
fcc1c0a27c | |||
6faec9901a | |||
e36bbbb22b | |||
71f26c3e59 | |||
795b6be17d | |||
90ea4506ca | |||
41b8d80479 | |||
f903d7e9a9 | |||
84c4b034a9 | |||
f5455f9887 | |||
2b1c09aa72 | |||
94ca254626 | |||
60171e3c87 | |||
933de30d42 | |||
364c43a73a | |||
218ba44dbf | |||
05d0f0babf | |||
823c58bef2 | |||
b03e0b8c35 | |||
3d62e02891 | |||
06fa92af2c | |||
6b297b701f | |||
5f39f7fbf7 | |||
019abc9980 | |||
b874adc296 |
1
.github/FUNDING.yml
vendored
Normal file
1
.github/FUNDING.yml
vendored
Normal file
@ -0,0 +1 @@
|
|||||||
|
github: [iluwatar]
|
1
.gitignore
vendored
1
.gitignore
vendored
@ -16,5 +16,4 @@ datanucleus.log
|
|||||||
/bin/
|
/bin/
|
||||||
/bin/
|
/bin/
|
||||||
*.log
|
*.log
|
||||||
data-mapper/src/main/resources/log4j.xml
|
|
||||||
event-sourcing/Journal.json
|
event-sourcing/Journal.json
|
||||||
|
@ -1,15 +0,0 @@
|
|||||||
# Path to sources
|
|
||||||
#sonar.sources=.
|
|
||||||
#sonar.exclusions=
|
|
||||||
#sonar.inclusions=
|
|
||||||
|
|
||||||
# Path to tests
|
|
||||||
#sonar.tests=
|
|
||||||
#sonar.test.exclusions=
|
|
||||||
#sonar.test.inclusions=
|
|
||||||
|
|
||||||
# Source encoding
|
|
||||||
#sonar.sourceEncoding=UTF-8
|
|
||||||
|
|
||||||
# Exclusions for copy-paste detection
|
|
||||||
#sonar.cpd.exclusions=
|
|
40
.travis.yml
40
.travis.yml
@ -1,31 +1,29 @@
|
|||||||
language: java
|
language: java
|
||||||
dist: trusty # Xenial build environment won't allow installation of Java 8
|
dist: bionic
|
||||||
jdk:
|
jdk:
|
||||||
- oraclejdk8
|
- openjdk11
|
||||||
|
sudo: required
|
||||||
|
|
||||||
env:
|
env:
|
||||||
global:
|
global:
|
||||||
- GH_REF: github.com/iluwatar/java-design-patterns.git
|
- secure: "DCpazS3nkLnter3sguXEAS2fC/1ZWNfM+XLyif9MfNFxlZdpni2vCD/jA0Rdpga8puQWHNVLyAec+RPFH/2qSmJ1c1UTV5MaLv8tPqwUX0VFA+1I6XoSv6oX4ldHTBWHEWqQHkRFOLoil0h0edc0tTOWQwXF8U+DLAB+HkRb4gw="
|
||||||
- secure: LxTDuNS/rBWIvKkaEqr79ImZAe48mCdoYCF41coxNXgNoippo4GIBArknqtv+XvdkiuRZ1yGyj6pn8GU33c/yn+krddTUkVCwTbVatbalW5jhQjDbHYym/JcxaK9ZS/3JTeGcWrBgiPqHEEDhCf26vPZsXoMSeVCEORVKTp1BSg=
|
|
||||||
- secure: "eoWlW9GyTJY04P8K3pxayXwU9/hmptQg/LfirispQkV9YvmziCfSzXnatnBhNfud98sCzY8BScXnb+OWLTnjLKpId4rtEqb0aJ40Jc32cUKzgzFAUn7cNcDAbUIfyPAGVqyQqfj/11wYSADwWMMOPlW97ExUtoyiH2WenXuRHso="
|
|
||||||
|
|
||||||
before_install:
|
|
||||||
- export DISPLAY=:99.0
|
|
||||||
- sh -e /etc/init.d/xvfb start
|
|
||||||
|
|
||||||
# default install command is just "mvn install -DskipTests=true -Dmaven.javadoc.skip=true -B -V"
|
services:
|
||||||
install:
|
- xvfb
|
||||||
- mvn install -DskipTests=true -Dmaven.javadoc.skip=true -B -V -e
|
|
||||||
|
addons:
|
||||||
|
sonarcloud:
|
||||||
|
organization: "iluwatar"
|
||||||
|
token:
|
||||||
|
secure: "FpHwMYPMkdWU6CeIB7+O3qIeIM4vJMp47UjkKK53f0w0s6tPZofZZkab+gcL2TqKSil7sFVB/AQXU1cUubflRszwcLbNsc8H2yFehD79o0o0Mqd1Dd5ip/q0KQbHkkln+InFlVLfvrLB4Xd4mlQVxbGhqpULBhXjKzFzQlRFcuU="
|
||||||
|
script:
|
||||||
|
# Because of Travis security restrictions, SonarCloud analysis cannot be run on pull requests originated from forks
|
||||||
|
# See https://docs.travis-ci.com/user/pull-requests/#pull-requests-and-security-restrictions
|
||||||
|
- 'if [ "$TRAVIS_PULL_REQUEST" != "false" ]; then mvn clean verify; fi'
|
||||||
|
- 'if [ "$TRAVIS_PULL_REQUEST" = "false" ]; then mvn clean verify sonar:sonar -Dsonar.projectKey=iluwatar_java-design-patterns -Dsonar.host.url=https://sonarcloud.io; fi'
|
||||||
|
|
||||||
after_success:
|
after_success:
|
||||||
- mvn clean org.jacoco:jacoco-maven-plugin:prepare-agent package sonar:sonar -Dsonar.host.url=https://sonarcloud.io -Dsonar.login=$SONAR_TOKEN
|
- bash update-website.sh
|
||||||
- bash update-ghpages.sh
|
|
||||||
|
|
||||||
# use latest java version available instead of travis default
|
|
||||||
addons:
|
|
||||||
apt:
|
|
||||||
packages:
|
|
||||||
- oracle-java8-installer
|
|
||||||
|
|
||||||
notifications:
|
notifications:
|
||||||
email:
|
email:
|
||||||
@ -36,5 +34,3 @@ notifications:
|
|||||||
on_success: change # options: [always|never|change] default: always
|
on_success: change # options: [always|never|change] default: always
|
||||||
on_failure: always # options: [always|never|change] default: always
|
on_failure: always # options: [always|never|change] default: always
|
||||||
on_start: never # options: [always|never|change] default: always
|
on_start: never # options: [always|never|change] default: always
|
||||||
|
|
||||||
sudo: required
|
|
||||||
|
@ -1,13 +0,0 @@
|
|||||||
# Code Coverage Report generation
|
|
||||||
|
|
||||||
To generate the code coverage report, execute the following command:
|
|
||||||
> mvn clean verify jacoco:report
|
|
||||||
|
|
||||||
This will generate code coverage report in each of the modules. In order to view the same, open the following file in your browser.
|
|
||||||
> target/site/jacoco/index.html
|
|
||||||
|
|
||||||
Please note that the above folder is created under each of the modules. For example:
|
|
||||||
* adapter/target/site/jacoco/index.html
|
|
||||||
* business-delegate/target/site/jacoco/index.html
|
|
||||||
|
|
||||||
|
|
20
README.md
20
README.md
@ -17,14 +17,21 @@ solve common problems when designing an application or system.
|
|||||||
Design patterns can speed up the development process by providing tested, proven
|
Design patterns can speed up the development process by providing tested, proven
|
||||||
development paradigms.
|
development paradigms.
|
||||||
|
|
||||||
Reusing design patterns help prevent subtle issues which cause major
|
Reusing design patterns help prevent subtle issues that cause major
|
||||||
problems, and it also improves code readability for coders and architects who
|
problems, and it also improves code readability for coders and architects who
|
||||||
are familiar with the patterns.
|
are familiar with the patterns.
|
||||||
|
|
||||||
# Getting started
|
# Getting started
|
||||||
|
|
||||||
|
This site showcases Java Design Patterns. The solutions have been developed by
|
||||||
|
experienced programmers and architects from the open source community. The
|
||||||
|
patterns can be browsed by their high level descriptions or by looking at their
|
||||||
|
source code. The source code examples are well commented and can be thought as
|
||||||
|
programming tutorials how to implement a specific pattern. We use the most
|
||||||
|
popular battle-proven open source Java technologies.
|
||||||
|
|
||||||
Before you dive into the material, you should be familiar with various
|
Before you dive into the material, you should be familiar with various
|
||||||
Programming/Software Design Principles.
|
software design principles.
|
||||||
|
|
||||||
All designs should be as simple as possible. You should start with KISS, YAGNI,
|
All designs should be as simple as possible. You should start with KISS, YAGNI,
|
||||||
and Do The Simplest Thing That Could Possibly Work principles. Complexity and
|
and Do The Simplest Thing That Could Possibly Work principles. Complexity and
|
||||||
@ -35,12 +42,17 @@ Once you are familiar with these concepts you can start drilling down into
|
|||||||
patterns by any of the following approaches
|
patterns by any of the following approaches
|
||||||
|
|
||||||
- Using difficulty tags, `Difficulty-Beginner`, `Difficulty-Intermediate` & `Difficulty-Expert`.
|
- Using difficulty tags, `Difficulty-Beginner`, `Difficulty-Intermediate` & `Difficulty-Expert`.
|
||||||
- Using pattern categories, `Creational`, `Behavioral` and others.
|
- Using pattern categories, `Creational`, `Behavioral`, and others.
|
||||||
- Search for a specific pattern. Can't find one? Please report a new pattern [here](https://github.com/iluwatar/java-design-patterns/issues).
|
- Search for a specific pattern. Can't find one? Please report a new pattern [here](https://github.com/iluwatar/java-design-patterns/issues).
|
||||||
|
|
||||||
|
Hopefully you find the object oriented solutions presented on this site useful
|
||||||
|
in your architectures and have as much fun learning them as we had developing them.
|
||||||
|
|
||||||
# How to contribute
|
# How to contribute
|
||||||
|
|
||||||
If you are willing to contribute to the project you will find the relevant information in our [developer wiki](https://github.com/iluwatar/java-design-patterns/wiki). We will help you and answer your questions in the [Gitter chatroom](https://gitter.im/iluwatar/java-design-patterns).
|
If you are willing to contribute to the project you will find the relevant information in
|
||||||
|
our [developer wiki](https://github.com/iluwatar/java-design-patterns/wiki). We will help
|
||||||
|
you and answer your questions in the [Gitter chatroom](https://gitter.im/iluwatar/java-design-patterns).
|
||||||
|
|
||||||
# License
|
# License
|
||||||
|
|
||||||
|
@ -2,7 +2,7 @@
|
|||||||
<!--
|
<!--
|
||||||
|
|
||||||
The MIT License
|
The MIT License
|
||||||
Copyright (c) 2014-2016 Ilkka Seppälä
|
Copyright © 2014-2019 Ilkka Seppälä
|
||||||
|
|
||||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||||
of this software and associated documentation files (the "Software"), to deal
|
of this software and associated documentation files (the "Software"), to deal
|
||||||
@ -29,7 +29,7 @@
|
|||||||
<parent>
|
<parent>
|
||||||
<artifactId>java-design-patterns</artifactId>
|
<artifactId>java-design-patterns</artifactId>
|
||||||
<groupId>com.iluwatar</groupId>
|
<groupId>com.iluwatar</groupId>
|
||||||
<version>1.21.0</version>
|
<version>1.22.0-SNAPSHOT</version>
|
||||||
</parent>
|
</parent>
|
||||||
<artifactId>abstract-document</artifactId>
|
<artifactId>abstract-document</artifactId>
|
||||||
<dependencies>
|
<dependencies>
|
||||||
|
@ -1,17 +1,17 @@
|
|||||||
/**
|
/*
|
||||||
* The MIT License
|
* The MIT License
|
||||||
* Copyright (c) 2014-2016 Ilkka Seppälä
|
* Copyright © 2014-2019 Ilkka Seppälä
|
||||||
* <p>
|
*
|
||||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||||
* of this software and associated documentation files (the "Software"), to deal
|
* of this software and associated documentation files (the "Software"), to deal
|
||||||
* in the Software without restriction, including without limitation the rights
|
* in the Software without restriction, including without limitation the rights
|
||||||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||||
* copies of the Software, and to permit persons to whom the Software is
|
* copies of the Software, and to permit persons to whom the Software is
|
||||||
* furnished to do so, subject to the following conditions:
|
* furnished to do so, subject to the following conditions:
|
||||||
* <p>
|
*
|
||||||
* The above copyright notice and this permission notice shall be included in
|
* The above copyright notice and this permission notice shall be included in
|
||||||
* all copies or substantial portions of the Software.
|
* all copies or substantial portions of the Software.
|
||||||
* <p>
|
*
|
||||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||||
@ -20,17 +20,18 @@
|
|||||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||||
* THE SOFTWARE.
|
* THE SOFTWARE.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
package com.iluwatar.abstractdocument;
|
package com.iluwatar.abstractdocument;
|
||||||
|
|
||||||
|
import java.util.Collection;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
import java.util.Objects;
|
import java.util.Objects;
|
||||||
import java.util.Optional;
|
|
||||||
import java.util.function.Function;
|
import java.util.function.Function;
|
||||||
import java.util.stream.Stream;
|
import java.util.stream.Stream;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Abstract implementation of Document interface
|
* Abstract implementation of Document interface.
|
||||||
*/
|
*/
|
||||||
public abstract class AbstractDocument implements Document {
|
public abstract class AbstractDocument implements Document {
|
||||||
|
|
||||||
@ -54,16 +55,21 @@ public abstract class AbstractDocument implements Document {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public <T> Stream<T> children(String key, Function<Map<String, Object>, T> constructor) {
|
public <T> Stream<T> children(String key, Function<Map<String, Object>, T> constructor) {
|
||||||
Optional<List<Map<String, Object>>> any = Stream.of(get(key)).filter(Objects::nonNull)
|
return Stream.ofNullable(get(key))
|
||||||
.map(el -> (List<Map<String, Object>>) el).findAny();
|
.filter(Objects::nonNull)
|
||||||
return any.map(maps -> maps.stream().map(constructor)).orElseGet(Stream::empty);
|
.map(el -> (List<Map<String, Object>>) el)
|
||||||
|
.findAny()
|
||||||
|
.stream()
|
||||||
|
.flatMap(Collection::stream)
|
||||||
|
.map(constructor);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public String toString() {
|
public String toString() {
|
||||||
StringBuilder builder = new StringBuilder();
|
var builder = new StringBuilder();
|
||||||
builder.append(getClass().getName()).append("[");
|
builder.append(getClass().getName()).append("[");
|
||||||
properties.forEach((key, value) -> builder.append("[").append(key).append(" : ").append(value).append("]"));
|
properties.forEach((key, value) -> builder.append("[").append(key).append(" : ").append(value)
|
||||||
|
.append("]"));
|
||||||
builder.append("]");
|
builder.append("]");
|
||||||
return builder.toString();
|
return builder.toString();
|
||||||
}
|
}
|
||||||
|
@ -1,17 +1,17 @@
|
|||||||
/**
|
/*
|
||||||
* The MIT License
|
* The MIT License
|
||||||
* Copyright (c) 2014-2016 Ilkka Seppälä
|
* Copyright © 2014-2019 Ilkka Seppälä
|
||||||
* <p>
|
*
|
||||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||||
* of this software and associated documentation files (the "Software"), to deal
|
* of this software and associated documentation files (the "Software"), to deal
|
||||||
* in the Software without restriction, including without limitation the rights
|
* in the Software without restriction, including without limitation the rights
|
||||||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||||
* copies of the Software, and to permit persons to whom the Software is
|
* copies of the Software, and to permit persons to whom the Software is
|
||||||
* furnished to do so, subject to the following conditions:
|
* furnished to do so, subject to the following conditions:
|
||||||
* <p>
|
*
|
||||||
* The above copyright notice and this permission notice shall be included in
|
* The above copyright notice and this permission notice shall be included in
|
||||||
* all copies or substantial portions of the Software.
|
* all copies or substantial portions of the Software.
|
||||||
* <p>
|
*
|
||||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||||
@ -20,65 +20,64 @@
|
|||||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||||
* THE SOFTWARE.
|
* THE SOFTWARE.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
package com.iluwatar.abstractdocument;
|
package com.iluwatar.abstractdocument;
|
||||||
|
|
||||||
import java.util.Arrays;
|
|
||||||
import java.util.HashMap;
|
|
||||||
import java.util.Map;
|
|
||||||
|
|
||||||
import org.slf4j.Logger;
|
|
||||||
import org.slf4j.LoggerFactory;
|
|
||||||
|
|
||||||
import com.iluwatar.abstractdocument.domain.Car;
|
import com.iluwatar.abstractdocument.domain.Car;
|
||||||
import com.iluwatar.abstractdocument.domain.enums.Property;
|
import com.iluwatar.abstractdocument.domain.enums.Property;
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.Map;
|
||||||
|
import org.slf4j.Logger;
|
||||||
|
import org.slf4j.LoggerFactory;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* The Abstract Document pattern enables handling additional, non-static
|
* The Abstract Document pattern enables handling additional, non-static properties. This pattern
|
||||||
* properties. This pattern uses concept of traits to enable type safety and
|
* uses concept of traits to enable type safety and separate properties of different classes into
|
||||||
* separate properties of different classes into set of interfaces.
|
* set of interfaces.
|
||||||
* <p>
|
*
|
||||||
* <p>
|
* <p>In Abstract Document pattern,({@link AbstractDocument}) fully implements {@link Document})
|
||||||
* In Abstract Document pattern,({@link AbstractDocument}) fully implements
|
* interface. Traits are then defined to enable access to properties in usual, static way.
|
||||||
* {@link Document}) interface. Traits are then defined to enable access to
|
|
||||||
* properties in usual, static way.
|
|
||||||
*/
|
*/
|
||||||
public class App {
|
public class App {
|
||||||
|
|
||||||
private static final Logger LOGGER = LoggerFactory.getLogger(App.class);
|
private static final Logger LOGGER = LoggerFactory.getLogger(App.class);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Executes the App
|
* Executes the App.
|
||||||
*/
|
*/
|
||||||
public App() {
|
public App() {
|
||||||
LOGGER.info("Constructing parts and car");
|
LOGGER.info("Constructing parts and car");
|
||||||
|
|
||||||
Map<String, Object> carProperties = new HashMap<>();
|
var wheelProperties = Map.of(
|
||||||
carProperties.put(Property.MODEL.toString(), "300SL");
|
Property.TYPE.toString(), "wheel",
|
||||||
carProperties.put(Property.PRICE.toString(), 10000L);
|
Property.MODEL.toString(), "15C",
|
||||||
|
Property.PRICE.toString(), 100L);
|
||||||
|
|
||||||
Map<String, Object> wheelProperties = new HashMap<>();
|
var doorProperties = Map.of(
|
||||||
wheelProperties.put(Property.TYPE.toString(), "wheel");
|
Property.TYPE.toString(), "door",
|
||||||
wheelProperties.put(Property.MODEL.toString(), "15C");
|
Property.MODEL.toString(), "Lambo",
|
||||||
wheelProperties.put(Property.PRICE.toString(), 100L);
|
Property.PRICE.toString(), 300L);
|
||||||
|
|
||||||
Map<String, Object> doorProperties = new HashMap<>();
|
var carProperties = Map.of(
|
||||||
doorProperties.put(Property.TYPE.toString(), "door");
|
Property.MODEL.toString(), "300SL",
|
||||||
doorProperties.put(Property.MODEL.toString(), "Lambo");
|
Property.PRICE.toString(), 10000L,
|
||||||
doorProperties.put(Property.PRICE.toString(), 300L);
|
Property.PARTS.toString(), List.of(wheelProperties, doorProperties));
|
||||||
|
|
||||||
carProperties.put(Property.PARTS.toString(), Arrays.asList(wheelProperties, doorProperties));
|
var car = new Car(carProperties);
|
||||||
|
|
||||||
Car car = new Car(carProperties);
|
|
||||||
|
|
||||||
LOGGER.info("Here is our car:");
|
LOGGER.info("Here is our car:");
|
||||||
LOGGER.info("-> model: {}", car.getModel().get());
|
LOGGER.info("-> model: {}", car.getModel().orElseThrow());
|
||||||
LOGGER.info("-> price: {}", car.getPrice().get());
|
LOGGER.info("-> price: {}", car.getPrice().orElseThrow());
|
||||||
LOGGER.info("-> parts: ");
|
LOGGER.info("-> parts: ");
|
||||||
car.getParts().forEach(p -> LOGGER.info("\t{}/{}/{}", p.getType().get(), p.getModel().get(), p.getPrice().get()));
|
car.getParts().forEach(p -> LOGGER.info("\t{}/{}/{}",
|
||||||
|
p.getType().orElse(null),
|
||||||
|
p.getModel().orElse(null),
|
||||||
|
p.getPrice().orElse(null))
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Program entry point
|
* Program entry point.
|
||||||
*
|
*
|
||||||
* @param args command line args
|
* @param args command line args
|
||||||
*/
|
*/
|
||||||
|
@ -1,17 +1,17 @@
|
|||||||
/**
|
/*
|
||||||
* The MIT License
|
* The MIT License
|
||||||
* Copyright (c) 2014-2016 Ilkka Seppälä
|
* Copyright © 2014-2019 Ilkka Seppälä
|
||||||
* <p>
|
*
|
||||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||||
* of this software and associated documentation files (the "Software"), to deal
|
* of this software and associated documentation files (the "Software"), to deal
|
||||||
* in the Software without restriction, including without limitation the rights
|
* in the Software without restriction, including without limitation the rights
|
||||||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||||
* copies of the Software, and to permit persons to whom the Software is
|
* copies of the Software, and to permit persons to whom the Software is
|
||||||
* furnished to do so, subject to the following conditions:
|
* furnished to do so, subject to the following conditions:
|
||||||
* <p>
|
*
|
||||||
* The above copyright notice and this permission notice shall be included in
|
* The above copyright notice and this permission notice shall be included in
|
||||||
* all copies or substantial portions of the Software.
|
* all copies or substantial portions of the Software.
|
||||||
* <p>
|
*
|
||||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||||
@ -20,6 +20,7 @@
|
|||||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||||
* THE SOFTWARE.
|
* THE SOFTWARE.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
package com.iluwatar.abstractdocument;
|
package com.iluwatar.abstractdocument;
|
||||||
|
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
@ -27,12 +28,12 @@ import java.util.function.Function;
|
|||||||
import java.util.stream.Stream;
|
import java.util.stream.Stream;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Document interface
|
* Document interface.
|
||||||
*/
|
*/
|
||||||
public interface Document {
|
public interface Document {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Puts the value related to the key
|
* Puts the value related to the key.
|
||||||
*
|
*
|
||||||
* @param key element key
|
* @param key element key
|
||||||
* @param value element value
|
* @param value element value
|
||||||
@ -41,7 +42,7 @@ public interface Document {
|
|||||||
Void put(String key, Object value);
|
Void put(String key, Object value);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Gets the value for the key
|
* Gets the value for the key.
|
||||||
*
|
*
|
||||||
* @param key element key
|
* @param key element key
|
||||||
* @return value or null
|
* @return value or null
|
||||||
@ -49,7 +50,7 @@ public interface Document {
|
|||||||
Object get(String key);
|
Object get(String key);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Gets the stream of child documents
|
* Gets the stream of child documents.
|
||||||
*
|
*
|
||||||
* @param key element key
|
* @param key element key
|
||||||
* @param constructor constructor of child class
|
* @param constructor constructor of child class
|
||||||
|
@ -1,17 +1,17 @@
|
|||||||
/**
|
/*
|
||||||
* The MIT License
|
* The MIT License
|
||||||
* Copyright (c) 2014-2016 Ilkka Seppälä
|
* Copyright © 2014-2019 Ilkka Seppälä
|
||||||
* <p>
|
*
|
||||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||||
* of this software and associated documentation files (the "Software"), to deal
|
* of this software and associated documentation files (the "Software"), to deal
|
||||||
* in the Software without restriction, including without limitation the rights
|
* in the Software without restriction, including without limitation the rights
|
||||||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||||
* copies of the Software, and to permit persons to whom the Software is
|
* copies of the Software, and to permit persons to whom the Software is
|
||||||
* furnished to do so, subject to the following conditions:
|
* furnished to do so, subject to the following conditions:
|
||||||
* <p>
|
*
|
||||||
* The above copyright notice and this permission notice shall be included in
|
* The above copyright notice and this permission notice shall be included in
|
||||||
* all copies or substantial portions of the Software.
|
* all copies or substantial portions of the Software.
|
||||||
* <p>
|
*
|
||||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||||
@ -20,14 +20,14 @@
|
|||||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||||
* THE SOFTWARE.
|
* THE SOFTWARE.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
package com.iluwatar.abstractdocument.domain;
|
package com.iluwatar.abstractdocument.domain;
|
||||||
|
|
||||||
|
import com.iluwatar.abstractdocument.AbstractDocument;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
|
|
||||||
import com.iluwatar.abstractdocument.AbstractDocument;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Car entity
|
* Car entity.
|
||||||
*/
|
*/
|
||||||
public class Car extends AbstractDocument implements HasModel, HasPrice, HasParts {
|
public class Car extends AbstractDocument implements HasModel, HasPrice, HasParts {
|
||||||
|
|
||||||
|
@ -1,17 +1,17 @@
|
|||||||
/**
|
/*
|
||||||
* The MIT License
|
* The MIT License
|
||||||
* Copyright (c) 2014-2016 Ilkka Seppälä
|
* Copyright © 2014-2019 Ilkka Seppälä
|
||||||
* <p>
|
*
|
||||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||||
* of this software and associated documentation files (the "Software"), to deal
|
* of this software and associated documentation files (the "Software"), to deal
|
||||||
* in the Software without restriction, including without limitation the rights
|
* in the Software without restriction, including without limitation the rights
|
||||||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||||
* copies of the Software, and to permit persons to whom the Software is
|
* copies of the Software, and to permit persons to whom the Software is
|
||||||
* furnished to do so, subject to the following conditions:
|
* furnished to do so, subject to the following conditions:
|
||||||
* <p>
|
*
|
||||||
* The above copyright notice and this permission notice shall be included in
|
* The above copyright notice and this permission notice shall be included in
|
||||||
* all copies or substantial portions of the Software.
|
* all copies or substantial portions of the Software.
|
||||||
* <p>
|
*
|
||||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||||
@ -20,15 +20,15 @@
|
|||||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||||
* THE SOFTWARE.
|
* THE SOFTWARE.
|
||||||
*/
|
*/
|
||||||
package com.iluwatar.abstractdocument.domain;
|
|
||||||
|
|
||||||
import java.util.Optional;
|
package com.iluwatar.abstractdocument.domain;
|
||||||
|
|
||||||
import com.iluwatar.abstractdocument.Document;
|
import com.iluwatar.abstractdocument.Document;
|
||||||
import com.iluwatar.abstractdocument.domain.enums.Property;
|
import com.iluwatar.abstractdocument.domain.enums.Property;
|
||||||
|
import java.util.Optional;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* HasModel trait for static access to 'model' property
|
* HasModel trait for static access to 'model' property.
|
||||||
*/
|
*/
|
||||||
public interface HasModel extends Document {
|
public interface HasModel extends Document {
|
||||||
|
|
||||||
|
@ -1,17 +1,17 @@
|
|||||||
/**
|
/*
|
||||||
* The MIT License
|
* The MIT License
|
||||||
* Copyright (c) 2014-2016 Ilkka Seppälä
|
* Copyright © 2014-2019 Ilkka Seppälä
|
||||||
* <p>
|
*
|
||||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||||
* of this software and associated documentation files (the "Software"), to deal
|
* of this software and associated documentation files (the "Software"), to deal
|
||||||
* in the Software without restriction, including without limitation the rights
|
* in the Software without restriction, including without limitation the rights
|
||||||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||||
* copies of the Software, and to permit persons to whom the Software is
|
* copies of the Software, and to permit persons to whom the Software is
|
||||||
* furnished to do so, subject to the following conditions:
|
* furnished to do so, subject to the following conditions:
|
||||||
* <p>
|
*
|
||||||
* The above copyright notice and this permission notice shall be included in
|
* The above copyright notice and this permission notice shall be included in
|
||||||
* all copies or substantial portions of the Software.
|
* all copies or substantial portions of the Software.
|
||||||
* <p>
|
*
|
||||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||||
@ -20,15 +20,15 @@
|
|||||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||||
* THE SOFTWARE.
|
* THE SOFTWARE.
|
||||||
*/
|
*/
|
||||||
package com.iluwatar.abstractdocument.domain;
|
|
||||||
|
|
||||||
import java.util.stream.Stream;
|
package com.iluwatar.abstractdocument.domain;
|
||||||
|
|
||||||
import com.iluwatar.abstractdocument.Document;
|
import com.iluwatar.abstractdocument.Document;
|
||||||
import com.iluwatar.abstractdocument.domain.enums.Property;
|
import com.iluwatar.abstractdocument.domain.enums.Property;
|
||||||
|
import java.util.stream.Stream;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* HasParts trait for static access to 'parts' property
|
* HasParts trait for static access to 'parts' property.
|
||||||
*/
|
*/
|
||||||
public interface HasParts extends Document {
|
public interface HasParts extends Document {
|
||||||
|
|
||||||
|
@ -1,17 +1,17 @@
|
|||||||
/**
|
/*
|
||||||
* The MIT License
|
* The MIT License
|
||||||
* Copyright (c) 2014-2016 Ilkka Seppälä
|
* Copyright © 2014-2019 Ilkka Seppälä
|
||||||
* <p>
|
*
|
||||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||||
* of this software and associated documentation files (the "Software"), to deal
|
* of this software and associated documentation files (the "Software"), to deal
|
||||||
* in the Software without restriction, including without limitation the rights
|
* in the Software without restriction, including without limitation the rights
|
||||||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||||
* copies of the Software, and to permit persons to whom the Software is
|
* copies of the Software, and to permit persons to whom the Software is
|
||||||
* furnished to do so, subject to the following conditions:
|
* furnished to do so, subject to the following conditions:
|
||||||
* <p>
|
*
|
||||||
* The above copyright notice and this permission notice shall be included in
|
* The above copyright notice and this permission notice shall be included in
|
||||||
* all copies or substantial portions of the Software.
|
* all copies or substantial portions of the Software.
|
||||||
* <p>
|
*
|
||||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||||
@ -20,15 +20,15 @@
|
|||||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||||
* THE SOFTWARE.
|
* THE SOFTWARE.
|
||||||
*/
|
*/
|
||||||
package com.iluwatar.abstractdocument.domain;
|
|
||||||
|
|
||||||
import java.util.Optional;
|
package com.iluwatar.abstractdocument.domain;
|
||||||
|
|
||||||
import com.iluwatar.abstractdocument.Document;
|
import com.iluwatar.abstractdocument.Document;
|
||||||
import com.iluwatar.abstractdocument.domain.enums.Property;
|
import com.iluwatar.abstractdocument.domain.enums.Property;
|
||||||
|
import java.util.Optional;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* HasPrice trait for static access to 'price' property
|
* HasPrice trait for static access to 'price' property.
|
||||||
*/
|
*/
|
||||||
public interface HasPrice extends Document {
|
public interface HasPrice extends Document {
|
||||||
|
|
||||||
|
@ -1,17 +1,17 @@
|
|||||||
/**
|
/*
|
||||||
* The MIT License
|
* The MIT License
|
||||||
* Copyright (c) 2014-2016 Ilkka Seppälä
|
* Copyright © 2014-2019 Ilkka Seppälä
|
||||||
* <p>
|
*
|
||||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||||
* of this software and associated documentation files (the "Software"), to deal
|
* of this software and associated documentation files (the "Software"), to deal
|
||||||
* in the Software without restriction, including without limitation the rights
|
* in the Software without restriction, including without limitation the rights
|
||||||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||||
* copies of the Software, and to permit persons to whom the Software is
|
* copies of the Software, and to permit persons to whom the Software is
|
||||||
* furnished to do so, subject to the following conditions:
|
* furnished to do so, subject to the following conditions:
|
||||||
* <p>
|
*
|
||||||
* The above copyright notice and this permission notice shall be included in
|
* The above copyright notice and this permission notice shall be included in
|
||||||
* all copies or substantial portions of the Software.
|
* all copies or substantial portions of the Software.
|
||||||
* <p>
|
*
|
||||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||||
@ -20,15 +20,15 @@
|
|||||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||||
* THE SOFTWARE.
|
* THE SOFTWARE.
|
||||||
*/
|
*/
|
||||||
package com.iluwatar.abstractdocument.domain;
|
|
||||||
|
|
||||||
import java.util.Optional;
|
package com.iluwatar.abstractdocument.domain;
|
||||||
|
|
||||||
import com.iluwatar.abstractdocument.Document;
|
import com.iluwatar.abstractdocument.Document;
|
||||||
import com.iluwatar.abstractdocument.domain.enums.Property;
|
import com.iluwatar.abstractdocument.domain.enums.Property;
|
||||||
|
import java.util.Optional;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* HasType trait for static access to 'type' property
|
* HasType trait for static access to 'type' property.
|
||||||
*/
|
*/
|
||||||
public interface HasType extends Document {
|
public interface HasType extends Document {
|
||||||
|
|
||||||
|
@ -1,17 +1,17 @@
|
|||||||
/**
|
/*
|
||||||
* The MIT License
|
* The MIT License
|
||||||
* Copyright (c) 2014-2016 Ilkka Seppälä
|
* Copyright © 2014-2019 Ilkka Seppälä
|
||||||
* <p>
|
*
|
||||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||||
* of this software and associated documentation files (the "Software"), to deal
|
* of this software and associated documentation files (the "Software"), to deal
|
||||||
* in the Software without restriction, including without limitation the rights
|
* in the Software without restriction, including without limitation the rights
|
||||||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||||
* copies of the Software, and to permit persons to whom the Software is
|
* copies of the Software, and to permit persons to whom the Software is
|
||||||
* furnished to do so, subject to the following conditions:
|
* furnished to do so, subject to the following conditions:
|
||||||
* <p>
|
*
|
||||||
* The above copyright notice and this permission notice shall be included in
|
* The above copyright notice and this permission notice shall be included in
|
||||||
* all copies or substantial portions of the Software.
|
* all copies or substantial portions of the Software.
|
||||||
* <p>
|
*
|
||||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||||
@ -20,14 +20,14 @@
|
|||||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||||
* THE SOFTWARE.
|
* THE SOFTWARE.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
package com.iluwatar.abstractdocument.domain;
|
package com.iluwatar.abstractdocument.domain;
|
||||||
|
|
||||||
|
import com.iluwatar.abstractdocument.AbstractDocument;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
|
|
||||||
import com.iluwatar.abstractdocument.AbstractDocument;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Part entity
|
* Part entity.
|
||||||
*/
|
*/
|
||||||
public class Part extends AbstractDocument implements HasType, HasModel, HasPrice {
|
public class Part extends AbstractDocument implements HasType, HasModel, HasPrice {
|
||||||
|
|
||||||
|
@ -1,6 +1,6 @@
|
|||||||
/**
|
/*
|
||||||
* The MIT License
|
* The MIT License
|
||||||
* Copyright (c) 2014 Ilkka Seppälä
|
* Copyright © 2014-2019 Ilkka Seppälä
|
||||||
*
|
*
|
||||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||||
* of this software and associated documentation files (the "Software"), to deal
|
* of this software and associated documentation files (the "Software"), to deal
|
||||||
@ -20,12 +20,11 @@
|
|||||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||||
* THE SOFTWARE.
|
* THE SOFTWARE.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
package com.iluwatar.abstractdocument.domain.enums;
|
package com.iluwatar.abstractdocument.domain.enums;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
*
|
* Enum To Describe Property type.
|
||||||
* Enum To Describe Property type
|
|
||||||
*
|
|
||||||
*/
|
*/
|
||||||
public enum Property {
|
public enum Property {
|
||||||
|
|
||||||
|
@ -1,17 +1,17 @@
|
|||||||
/**
|
/*
|
||||||
* The MIT License
|
* The MIT License
|
||||||
* Copyright (c) 2014-2016 Ilkka Seppälä
|
* Copyright © 2014-2019 Ilkka Seppälä
|
||||||
* <p>
|
*
|
||||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||||
* of this software and associated documentation files (the "Software"), to deal
|
* of this software and associated documentation files (the "Software"), to deal
|
||||||
* in the Software without restriction, including without limitation the rights
|
* in the Software without restriction, including without limitation the rights
|
||||||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||||
* copies of the Software, and to permit persons to whom the Software is
|
* copies of the Software, and to permit persons to whom the Software is
|
||||||
* furnished to do so, subject to the following conditions:
|
* furnished to do so, subject to the following conditions:
|
||||||
* <p>
|
*
|
||||||
* The above copyright notice and this permission notice shall be included in
|
* The above copyright notice and this permission notice shall be included in
|
||||||
* all copies or substantial portions of the Software.
|
* all copies or substantial portions of the Software.
|
||||||
* <p>
|
*
|
||||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||||
@ -20,20 +20,18 @@
|
|||||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||||
* THE SOFTWARE.
|
* THE SOFTWARE.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
package com.iluwatar.abstractdocument;
|
package com.iluwatar.abstractdocument;
|
||||||
|
|
||||||
import org.junit.jupiter.api.Test;
|
|
||||||
|
|
||||||
import java.util.Arrays;
|
|
||||||
import java.util.HashMap;
|
|
||||||
import java.util.List;
|
|
||||||
import java.util.Map;
|
|
||||||
import java.util.stream.Stream;
|
|
||||||
|
|
||||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||||
import static org.junit.jupiter.api.Assertions.assertNotNull;
|
import static org.junit.jupiter.api.Assertions.assertNotNull;
|
||||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||||
|
|
||||||
|
import java.util.HashMap;
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.Map;
|
||||||
|
import org.junit.jupiter.api.Test;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* AbstractDocument test class
|
* AbstractDocument test class
|
||||||
*/
|
*/
|
||||||
@ -59,29 +57,26 @@ public class AbstractDocumentTest {
|
|||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void shouldRetrieveChildren() {
|
public void shouldRetrieveChildren() {
|
||||||
Map<String, Object> child1 = new HashMap<>();
|
var children = List.of(Map.of(), Map.of());
|
||||||
Map<String, Object> child2 = new HashMap<>();
|
|
||||||
List<Map<String, Object>> children = Arrays.asList(child1, child2);
|
|
||||||
|
|
||||||
document.put(KEY, children);
|
document.put(KEY, children);
|
||||||
|
|
||||||
Stream<DocumentImplementation> childrenStream = document.children(KEY, DocumentImplementation::new);
|
var childrenStream = document.children(KEY, DocumentImplementation::new);
|
||||||
assertNotNull(children);
|
assertNotNull(children);
|
||||||
assertEquals(2, childrenStream.count());
|
assertEquals(2, childrenStream.count());
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void shouldRetrieveEmptyStreamForNonExistingChildren() {
|
public void shouldRetrieveEmptyStreamForNonExistingChildren() {
|
||||||
Stream<DocumentImplementation> children = document.children(KEY, DocumentImplementation::new);
|
var children = document.children(KEY, DocumentImplementation::new);
|
||||||
assertNotNull(children);
|
assertNotNull(children);
|
||||||
assertEquals(0, children.count());
|
assertEquals(0, children.count());
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void shouldIncludePropsInToString() {
|
public void shouldIncludePropsInToString() {
|
||||||
Map<String, Object> props = new HashMap<>();
|
var props = Map.of(KEY, (Object) VALUE);
|
||||||
props.put(KEY, VALUE);
|
var document = new DocumentImplementation(props);
|
||||||
DocumentImplementation document = new DocumentImplementation(props);
|
|
||||||
assertTrue(document.toString().contains(KEY));
|
assertTrue(document.toString().contains(KEY));
|
||||||
assertTrue(document.toString().contains(VALUE));
|
assertTrue(document.toString().contains(VALUE));
|
||||||
}
|
}
|
||||||
|
@ -1,17 +1,17 @@
|
|||||||
/**
|
/*
|
||||||
* The MIT License
|
* The MIT License
|
||||||
* Copyright (c) 2014-2016 Ilkka Seppälä
|
* Copyright © 2014-2019 Ilkka Seppälä
|
||||||
* <p>
|
*
|
||||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||||
* of this software and associated documentation files (the "Software"), to deal
|
* of this software and associated documentation files (the "Software"), to deal
|
||||||
* in the Software without restriction, including without limitation the rights
|
* in the Software without restriction, including without limitation the rights
|
||||||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||||
* copies of the Software, and to permit persons to whom the Software is
|
* copies of the Software, and to permit persons to whom the Software is
|
||||||
* furnished to do so, subject to the following conditions:
|
* furnished to do so, subject to the following conditions:
|
||||||
* <p>
|
*
|
||||||
* The above copyright notice and this permission notice shall be included in
|
* The above copyright notice and this permission notice shall be included in
|
||||||
* all copies or substantial portions of the Software.
|
* all copies or substantial portions of the Software.
|
||||||
* <p>
|
*
|
||||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||||
@ -20,6 +20,7 @@
|
|||||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||||
* THE SOFTWARE.
|
* THE SOFTWARE.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
package com.iluwatar.abstractdocument;
|
package com.iluwatar.abstractdocument;
|
||||||
|
|
||||||
import org.junit.jupiter.api.Test;
|
import org.junit.jupiter.api.Test;
|
||||||
|
@ -1,17 +1,17 @@
|
|||||||
/**
|
/*
|
||||||
* The MIT License
|
* The MIT License
|
||||||
* Copyright (c) 2014-2016 Ilkka Seppälä
|
* Copyright © 2014-2019 Ilkka Seppälä
|
||||||
* <p>
|
*
|
||||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||||
* of this software and associated documentation files (the "Software"), to deal
|
* of this software and associated documentation files (the "Software"), to deal
|
||||||
* in the Software without restriction, including without limitation the rights
|
* in the Software without restriction, including without limitation the rights
|
||||||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||||
* copies of the Software, and to permit persons to whom the Software is
|
* copies of the Software, and to permit persons to whom the Software is
|
||||||
* furnished to do so, subject to the following conditions:
|
* furnished to do so, subject to the following conditions:
|
||||||
* <p>
|
*
|
||||||
* The above copyright notice and this permission notice shall be included in
|
* The above copyright notice and this permission notice shall be included in
|
||||||
* all copies or substantial portions of the Software.
|
* all copies or substantial portions of the Software.
|
||||||
* <p>
|
*
|
||||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||||
@ -20,19 +20,17 @@
|
|||||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||||
* THE SOFTWARE.
|
* THE SOFTWARE.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
package com.iluwatar.abstractdocument;
|
package com.iluwatar.abstractdocument;
|
||||||
|
|
||||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||||
|
|
||||||
import java.util.Arrays;
|
|
||||||
import java.util.HashMap;
|
|
||||||
import java.util.Map;
|
|
||||||
|
|
||||||
import org.junit.jupiter.api.Test;
|
|
||||||
|
|
||||||
import com.iluwatar.abstractdocument.domain.Car;
|
import com.iluwatar.abstractdocument.domain.Car;
|
||||||
import com.iluwatar.abstractdocument.domain.Part;
|
import com.iluwatar.abstractdocument.domain.Part;
|
||||||
import com.iluwatar.abstractdocument.domain.enums.Property;
|
import com.iluwatar.abstractdocument.domain.enums.Property;
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.Map;
|
||||||
|
import org.junit.jupiter.api.Test;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Test for Part and Car
|
* Test for Part and Car
|
||||||
@ -48,27 +46,27 @@ public class DomainTest {
|
|||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void shouldConstructPart() {
|
public void shouldConstructPart() {
|
||||||
Map<String, Object> partProperties = new HashMap<>();
|
var partProperties = Map.of(
|
||||||
partProperties.put(Property.TYPE.toString(), TEST_PART_TYPE);
|
Property.TYPE.toString(), TEST_PART_TYPE,
|
||||||
partProperties.put(Property.MODEL.toString(), TEST_PART_MODEL);
|
Property.MODEL.toString(), TEST_PART_MODEL,
|
||||||
partProperties.put(Property.PRICE.toString(), TEST_PART_PRICE);
|
Property.PRICE.toString(), (Object) TEST_PART_PRICE
|
||||||
Part part = new Part(partProperties);
|
);
|
||||||
|
var part = new Part(partProperties);
|
||||||
assertEquals(TEST_PART_TYPE, part.getType().get());
|
assertEquals(TEST_PART_TYPE, part.getType().orElseThrow());
|
||||||
assertEquals(TEST_PART_MODEL, part.getModel().get());
|
assertEquals(TEST_PART_MODEL, part.getModel().orElseThrow());
|
||||||
assertEquals(TEST_PART_PRICE, part.getPrice().get());
|
assertEquals(TEST_PART_PRICE, part.getPrice().orElseThrow());
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void shouldConstructCar() {
|
public void shouldConstructCar() {
|
||||||
Map<String, Object> carProperties = new HashMap<>();
|
var carProperties = Map.of(
|
||||||
carProperties.put(Property.MODEL.toString(), TEST_CAR_MODEL);
|
Property.MODEL.toString(), TEST_CAR_MODEL,
|
||||||
carProperties.put(Property.PRICE.toString(), TEST_CAR_PRICE);
|
Property.PRICE.toString(), TEST_CAR_PRICE,
|
||||||
carProperties.put(Property.PARTS.toString(), Arrays.asList(new HashMap<>(), new HashMap<>()));
|
Property.PARTS.toString(), List.of(Map.of(), Map.of())
|
||||||
Car car = new Car(carProperties);
|
);
|
||||||
|
var car = new Car(carProperties);
|
||||||
assertEquals(TEST_CAR_MODEL, car.getModel().get());
|
assertEquals(TEST_CAR_MODEL, car.getModel().orElseThrow());
|
||||||
assertEquals(TEST_CAR_PRICE, car.getPrice().get());
|
assertEquals(TEST_CAR_PRICE, car.getPrice().orElseThrow());
|
||||||
assertEquals(2, car.getParts().count());
|
assertEquals(2, car.getParts().count());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -109,10 +109,10 @@ public class OrcKingdomFactory implements KingdomFactory {
|
|||||||
Now we have our abstract factory that lets us make family of related objects i.e. Elven kingdom factory creates Elven castle, king and army etc.
|
Now we have our abstract factory that lets us make family of related objects i.e. Elven kingdom factory creates Elven castle, king and army etc.
|
||||||
|
|
||||||
```java
|
```java
|
||||||
KingdomFactory factory = new ElfKingdomFactory();
|
var factory = new ElfKingdomFactory();
|
||||||
Castle castle = factory.createCastle();
|
var castle = factory.createCastle();
|
||||||
King king = factory.createKing();
|
var king = factory.createKing();
|
||||||
Army army = factory.createArmy();
|
var army = factory.createArmy();
|
||||||
|
|
||||||
castle.getDescription(); // Output: This is the Elven castle!
|
castle.getDescription(); // Output: This is the Elven castle!
|
||||||
king.getDescription(); // Output: This is the Elven king!
|
king.getDescription(); // Output: This is the Elven king!
|
||||||
@ -143,7 +143,7 @@ public static class FactoryMaker {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public static void main(String[] args) {
|
public static void main(String[] args) {
|
||||||
App app = new App();
|
var app = new App();
|
||||||
|
|
||||||
LOGGER.info("Elf Kingdom");
|
LOGGER.info("Elf Kingdom");
|
||||||
app.createKingdom(FactoryMaker.makeFactory(KingdomType.ELF));
|
app.createKingdom(FactoryMaker.makeFactory(KingdomType.ELF));
|
||||||
@ -169,24 +169,25 @@ Use the Abstract Factory pattern when
|
|||||||
* you need a run-time value to construct a particular dependency
|
* you need a run-time value to construct a particular dependency
|
||||||
* you want to decide which product to call from a family at runtime.
|
* you want to decide which product to call from a family at runtime.
|
||||||
* you need to supply one or more parameters only known at run-time before you can resolve a dependency.
|
* you need to supply one or more parameters only known at run-time before you can resolve a dependency.
|
||||||
|
* when you need consistency among products
|
||||||
|
* you don’t want to change existing code when adding new products or families of products to the program.
|
||||||
|
|
||||||
## Use Cases:
|
## Use Cases:
|
||||||
|
|
||||||
* Selecting to call the appropriate implementation of FileSystemAcmeService or DatabaseAcmeService or NetworkAcmeService at runtime.
|
* Selecting to call the appropriate implementation of FileSystemAcmeService or DatabaseAcmeService or NetworkAcmeService at runtime.
|
||||||
* Unit test case writing becomes much easier
|
* Unit test case writing becomes much easier
|
||||||
|
* UI tools for different OS
|
||||||
|
|
||||||
## Consequences:
|
## Consequences:
|
||||||
|
|
||||||
* Dependency injection in java hides the service class dependencies that can lead to runtime errors that would have been caught at compile time.
|
* Dependency injection in java hides the service class dependencies that can lead to runtime errors that would have been caught at compile time.
|
||||||
|
* While the pattern is great when creating predefined objects, adding the new ones might be challenging.
|
||||||
|
* The code may become more complicated than it should be, since a lot of new interfaces and classes are introduced along with the pattern.
|
||||||
|
|
||||||
|
|
||||||
## Tutorial
|
## Tutorial
|
||||||
* [Abstract Factory Pattern Tutorial](https://www.journaldev.com/1418/abstract-factory-design-pattern-in-java)
|
* [Abstract Factory Pattern Tutorial](https://www.journaldev.com/1418/abstract-factory-design-pattern-in-java)
|
||||||
|
|
||||||
## Presentations
|
|
||||||
|
|
||||||
* [Abstract Factory Pattern](etc/presentation.html)
|
|
||||||
|
|
||||||
|
|
||||||
## Real world examples
|
## Real world examples
|
||||||
|
|
||||||
|
Binary file not shown.
Before Width: | Height: | Size: 57 KiB |
Binary file not shown.
Before Width: | Height: | Size: 26 KiB |
@ -1,190 +0,0 @@
|
|||||||
<!--
|
|
||||||
|
|
||||||
The MIT License
|
|
||||||
Copyright (c) 2017 Rodolfo Forte
|
|
||||||
|
|
||||||
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.
|
|
||||||
|
|
||||||
-->
|
|
||||||
<!DOCTYPE html>
|
|
||||||
<html>
|
|
||||||
<head>
|
|
||||||
<title>Design Patterns - Abstract Factory Presentation</title>
|
|
||||||
<meta charset="utf-8">
|
|
||||||
<style>
|
|
||||||
@import url(https://fonts.googleapis.com/css?family=Yanone+Kaffeesatz);
|
|
||||||
@import url(https://fonts.googleapis.com/css?family=Droid+Serif:400,700,400italic);
|
|
||||||
@import url(https://fonts.googleapis.com/css?family=Ubuntu+Mono:400,700,400italic);
|
|
||||||
|
|
||||||
body { font-family: 'Droid Serif'; }
|
|
||||||
h1, h2, h3 {
|
|
||||||
font-family: 'Yanone Kaffeesatz';
|
|
||||||
font-weight: normal;
|
|
||||||
}
|
|
||||||
.remark-code, .remark-inline-code { font-family: 'Ubuntu Mono'; }
|
|
||||||
|
|
||||||
blockquote {
|
|
||||||
border-left: 0.3em solid rgba(0,0,0,0.5);
|
|
||||||
padding: 0 15px;
|
|
||||||
font-style: italic;
|
|
||||||
}
|
|
||||||
|
|
||||||
img {
|
|
||||||
max-width:100%;
|
|
||||||
}
|
|
||||||
</style>
|
|
||||||
</head>
|
|
||||||
<body>
|
|
||||||
<textarea id="source">
|
|
||||||
|
|
||||||
class: center, middle
|
|
||||||
|
|
||||||
# Abstract Factory
|
|
||||||
|
|
||||||
---
|
|
||||||
|
|
||||||
# Also known as
|
|
||||||
|
|
||||||
* Kit
|
|
||||||
|
|
||||||
---
|
|
||||||
|
|
||||||
# Intent
|
|
||||||
|
|
||||||
* Provide an interface for creating families of related or dependent objects without specifying their concrete classes
|
|
||||||
|
|
||||||
---
|
|
||||||
|
|
||||||
# Explanation
|
|
||||||
|
|
||||||
* [Wikipedia](https://en.wikipedia.org/wiki/Abstract_factory_pattern) says:
|
|
||||||
> "The abstract factory pattern provides a way to encapsulate a group of individual factories that have a common theme without specifying their concrete classes"
|
|
||||||
|
|
||||||
<br />
|
|
||||||
|
|
||||||
* In plain words:
|
|
||||||
* A factory that groups individual but related/dependent factories together without specifying their concrete classes;
|
|
||||||
* A factory of factories;
|
|
||||||
|
|
||||||
---
|
|
||||||
|
|
||||||
# Example
|
|
||||||
|
|
||||||
* In a factory that creates kingdoms, we need objects with common theme:
|
|
||||||
|
|
||||||
* Elven kingdom needs an Elven king, Elven castle and Elven army;
|
|
||||||
|
|
||||||
* Orcish kingdom needs an Orcish king, Orcish castle and Orcish army;
|
|
||||||
|
|
||||||
<br />
|
|
||||||
|
|
||||||
* There is a dependency between the objects in the kingdom;
|
|
||||||
|
|
||||||
---
|
|
||||||
|
|
||||||
# Diagram
|
|
||||||
|
|
||||||
* Based on the kingdom example, the diagram below showcases the different concrete factories and their concrete products:
|
|
||||||
|
|
||||||
.center[]
|
|
||||||
|
|
||||||
|
|
||||||
---
|
|
||||||
|
|
||||||
# Diagram
|
|
||||||
|
|
||||||
* The class diagram below showcases the factory of factories;
|
|
||||||
|
|
||||||
* At runtime, we can define which Kingdom type is needed and pass it as a parameter to define which concrete KingdomFactory to instantiate;
|
|
||||||
|
|
||||||
* The concrete factory returned will then be able to produce the related objects of the specified type;
|
|
||||||
|
|
||||||
.center[]
|
|
||||||
|
|
||||||
---
|
|
||||||
|
|
||||||
# Applicability
|
|
||||||
|
|
||||||
Use the Abstract Factory pattern when:
|
|
||||||
|
|
||||||
* A system should be independent of how its products are created, composed and represented;
|
|
||||||
|
|
||||||
* A system should be configured with one of multiple families of products;
|
|
||||||
|
|
||||||
* A family of related product objects is designed to be used together, and you need to enforce this constraint;
|
|
||||||
|
|
||||||
* You want to provide a class library of products, and you want to reveal just their interfaces, not their implementations;
|
|
||||||
|
|
||||||
---
|
|
||||||
|
|
||||||
# Applicability
|
|
||||||
|
|
||||||
Use the Abstract Factory pattern when:
|
|
||||||
|
|
||||||
* The lifetime of the dependency is conceptually shorter than the lifetime of the consumer;
|
|
||||||
|
|
||||||
* You need a run-time value to construct a particular dependency;
|
|
||||||
|
|
||||||
* You want to decide which product to call from a family at runtime;
|
|
||||||
|
|
||||||
* You need to supply one or more parameters only known at run-time before you can resolve a dependency;
|
|
||||||
|
|
||||||
---
|
|
||||||
|
|
||||||
#Use Cases
|
|
||||||
|
|
||||||
* Selecting to call the appropriate implementation of FileSystemAcmeService or DatabaseAcmeService or NetworkAcmeService at runtime;
|
|
||||||
* Unit test case writing becomes much easier;
|
|
||||||
|
|
||||||
---
|
|
||||||
|
|
||||||
# Consequences
|
|
||||||
|
|
||||||
* Dependency injection in java hides the service class dependencies that can lead to runtime errors that would have been caught at compile time
|
|
||||||
|
|
||||||
---
|
|
||||||
|
|
||||||
# Real world examples
|
|
||||||
|
|
||||||
[javax.xml.parsers.DocumentBuilderFactory](http://docs.oracle.com/javase/8/docs/api/javax/xml/parsers/DocumentBuilderFactory.html)
|
|
||||||
|
|
||||||
[javax.xml.transform.TransformerFactory](http://docs.oracle.com/javase/8/docs/api/javax/xml/transform/TransformerFactory.html#newInstance--)
|
|
||||||
|
|
||||||
[javax.xml.xpath.XPathFactory](http://docs.oracle.com/javase/8/docs/api/javax/xml/xpath/XPathFactory.html#newInstance--)
|
|
||||||
|
|
||||||
---
|
|
||||||
|
|
||||||
# Credits
|
|
||||||
|
|
||||||
* [Design Patterns: Elements of Reusable Object-Oriented Software](http://www.amazon.com/Design-Patterns-Elements-Reusable-Object-Oriented/dp/0201633612)
|
|
||||||
|
|
||||||
---
|
|
||||||
|
|
||||||
# Tutorials
|
|
||||||
|
|
||||||
* Source code http://java-design-patterns.com/patterns/abstract-factory/
|
|
||||||
|
|
||||||
</textarea>
|
|
||||||
<script src="https://remarkjs.com/downloads/remark-latest.min.js">
|
|
||||||
</script>
|
|
||||||
<script>
|
|
||||||
var slideshow = remark.create();
|
|
||||||
</script>
|
|
||||||
</body>
|
|
||||||
</html>
|
|
@ -2,7 +2,7 @@
|
|||||||
<!--
|
<!--
|
||||||
|
|
||||||
The MIT License
|
The MIT License
|
||||||
Copyright (c) 2014-2016 Ilkka Seppälä
|
Copyright © 2014-2019 Ilkka Seppälä
|
||||||
|
|
||||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||||
of this software and associated documentation files (the "Software"), to deal
|
of this software and associated documentation files (the "Software"), to deal
|
||||||
@ -29,7 +29,7 @@
|
|||||||
<parent>
|
<parent>
|
||||||
<groupId>com.iluwatar</groupId>
|
<groupId>com.iluwatar</groupId>
|
||||||
<artifactId>java-design-patterns</artifactId>
|
<artifactId>java-design-patterns</artifactId>
|
||||||
<version>1.21.0</version>
|
<version>1.22.0-SNAPSHOT</version>
|
||||||
</parent>
|
</parent>
|
||||||
<artifactId>abstract-factory</artifactId>
|
<artifactId>abstract-factory</artifactId>
|
||||||
<dependencies>
|
<dependencies>
|
||||||
|
@ -1,6 +1,6 @@
|
|||||||
/**
|
/*
|
||||||
* The MIT License
|
* The MIT License
|
||||||
* Copyright (c) 2014-2016 Ilkka Seppälä
|
* Copyright © 2014-2019 Ilkka Seppälä
|
||||||
*
|
*
|
||||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||||
* of this software and associated documentation files (the "Software"), to deal
|
* of this software and associated documentation files (the "Software"), to deal
|
||||||
@ -20,27 +20,26 @@
|
|||||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||||
* THE SOFTWARE.
|
* THE SOFTWARE.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
package com.iluwatar.abstractfactory;
|
package com.iluwatar.abstractfactory;
|
||||||
|
|
||||||
|
import com.iluwatar.abstractfactory.App.FactoryMaker.KingdomType;
|
||||||
import org.slf4j.Logger;
|
import org.slf4j.Logger;
|
||||||
import org.slf4j.LoggerFactory;
|
import org.slf4j.LoggerFactory;
|
||||||
|
|
||||||
import com.iluwatar.abstractfactory.App.FactoryMaker.KingdomType;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
*
|
* The Abstract Factory pattern provides a way to encapsulate a group of individual factories that
|
||||||
* The Abstract Factory pattern provides a way to encapsulate a group of individual factories that have a common theme
|
* have a common theme without specifying their concrete classes. In normal usage, the client
|
||||||
* without specifying their concrete classes. In normal usage, the client software creates a concrete implementation of
|
* software creates a concrete implementation of the abstract factory and then uses the generic
|
||||||
* the abstract factory and then uses the generic interface of the factory to create the concrete objects that are part
|
* interface of the factory to create the concrete objects that are part of the theme. The client
|
||||||
* of the theme. The client does not know (or care) which concrete objects it gets from each of these internal
|
* does not know (or care) which concrete objects it gets from each of these internal factories,
|
||||||
* factories, since it uses only the generic interfaces of their products. This pattern separates the details of
|
* since it uses only the generic interfaces of their products. This pattern separates the details
|
||||||
* implementation of a set of objects from their general usage and relies on object composition, as object creation is
|
* of implementation of a set of objects from their general usage and relies on object composition,
|
||||||
* implemented in methods exposed in the factory interface.
|
* as object creation is implemented in methods exposed in the factory interface.
|
||||||
* <p>
|
*
|
||||||
* The essence of the Abstract Factory pattern is a factory interface ({@link KingdomFactory}) and its implementations (
|
* <p>The essence of the Abstract Factory pattern is a factory interface ({@link KingdomFactory})
|
||||||
* {@link ElfKingdomFactory}, {@link OrcKingdomFactory}). The example uses both concrete implementations to create a
|
* and its implementations ( {@link ElfKingdomFactory}, {@link OrcKingdomFactory}). The example uses
|
||||||
* king, a castle and an army.
|
* both concrete implementations to create a king, a castle and an army.
|
||||||
*
|
|
||||||
*/
|
*/
|
||||||
public class App {
|
public class App {
|
||||||
|
|
||||||
@ -51,14 +50,14 @@ public class App {
|
|||||||
private Army army;
|
private Army army;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Creates kingdom
|
* Creates kingdom.
|
||||||
*/
|
*/
|
||||||
public void createKingdom(final KingdomFactory factory) {
|
public void createKingdom(final KingdomFactory factory) {
|
||||||
setKing(factory.createKing());
|
setKing(factory.createKing());
|
||||||
setCastle(factory.createCastle());
|
setCastle(factory.createCastle());
|
||||||
setArmy(factory.createArmy());
|
setArmy(factory.createArmy());
|
||||||
}
|
}
|
||||||
|
|
||||||
King getKing(final KingdomFactory factory) {
|
King getKing(final KingdomFactory factory) {
|
||||||
return factory.createKing();
|
return factory.createKing();
|
||||||
}
|
}
|
||||||
@ -70,7 +69,7 @@ public class App {
|
|||||||
private void setKing(final King king) {
|
private void setKing(final King king) {
|
||||||
this.king = king;
|
this.king = king;
|
||||||
}
|
}
|
||||||
|
|
||||||
Castle getCastle(final KingdomFactory factory) {
|
Castle getCastle(final KingdomFactory factory) {
|
||||||
return factory.createCastle();
|
return factory.createCastle();
|
||||||
}
|
}
|
||||||
@ -82,7 +81,7 @@ public class App {
|
|||||||
private void setCastle(final Castle castle) {
|
private void setCastle(final Castle castle) {
|
||||||
this.castle = castle;
|
this.castle = castle;
|
||||||
}
|
}
|
||||||
|
|
||||||
Army getArmy(final KingdomFactory factory) {
|
Army getArmy(final KingdomFactory factory) {
|
||||||
return factory.createArmy();
|
return factory.createArmy();
|
||||||
}
|
}
|
||||||
@ -124,13 +123,12 @@ public class App {
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* Program entry point.
|
* Program entry point.
|
||||||
*
|
*
|
||||||
* @param args
|
* @param args command line args
|
||||||
* command line args
|
|
||||||
*/
|
*/
|
||||||
public static void main(String[] args) {
|
public static void main(String[] args) {
|
||||||
|
|
||||||
App app = new App();
|
var app = new App();
|
||||||
|
|
||||||
LOGGER.info("Elf Kingdom");
|
LOGGER.info("Elf Kingdom");
|
||||||
app.createKingdom(FactoryMaker.makeFactory(KingdomType.ELF));
|
app.createKingdom(FactoryMaker.makeFactory(KingdomType.ELF));
|
||||||
|
@ -1,6 +1,6 @@
|
|||||||
/**
|
/*
|
||||||
* The MIT License
|
* The MIT License
|
||||||
* Copyright (c) 2014-2016 Ilkka Seppälä
|
* Copyright © 2014-2019 Ilkka Seppälä
|
||||||
*
|
*
|
||||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||||
* of this software and associated documentation files (the "Software"), to deal
|
* of this software and associated documentation files (the "Software"), to deal
|
||||||
@ -20,12 +20,11 @@
|
|||||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||||
* THE SOFTWARE.
|
* THE SOFTWARE.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
package com.iluwatar.abstractfactory;
|
package com.iluwatar.abstractfactory;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
*
|
* Army interface.
|
||||||
* Army interface
|
|
||||||
*
|
|
||||||
*/
|
*/
|
||||||
public interface Army {
|
public interface Army {
|
||||||
|
|
||||||
|
@ -1,6 +1,6 @@
|
|||||||
/**
|
/*
|
||||||
* The MIT License
|
* The MIT License
|
||||||
* Copyright (c) 2014-2016 Ilkka Seppälä
|
* Copyright © 2014-2019 Ilkka Seppälä
|
||||||
*
|
*
|
||||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||||
* of this software and associated documentation files (the "Software"), to deal
|
* of this software and associated documentation files (the "Software"), to deal
|
||||||
@ -20,12 +20,11 @@
|
|||||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||||
* THE SOFTWARE.
|
* THE SOFTWARE.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
package com.iluwatar.abstractfactory;
|
package com.iluwatar.abstractfactory;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
*
|
* Castle interface.
|
||||||
* Castle interface
|
|
||||||
*
|
|
||||||
*/
|
*/
|
||||||
public interface Castle {
|
public interface Castle {
|
||||||
|
|
||||||
|
@ -1,6 +1,6 @@
|
|||||||
/**
|
/*
|
||||||
* The MIT License
|
* The MIT License
|
||||||
* Copyright (c) 2014-2016 Ilkka Seppälä
|
* Copyright © 2014-2019 Ilkka Seppälä
|
||||||
*
|
*
|
||||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||||
* of this software and associated documentation files (the "Software"), to deal
|
* of this software and associated documentation files (the "Software"), to deal
|
||||||
@ -20,12 +20,11 @@
|
|||||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||||
* THE SOFTWARE.
|
* THE SOFTWARE.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
package com.iluwatar.abstractfactory;
|
package com.iluwatar.abstractfactory;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
*
|
* ElfArmy.
|
||||||
* ElfArmy
|
|
||||||
*
|
|
||||||
*/
|
*/
|
||||||
public class ElfArmy implements Army {
|
public class ElfArmy implements Army {
|
||||||
|
|
||||||
|
@ -1,6 +1,6 @@
|
|||||||
/**
|
/*
|
||||||
* The MIT License
|
* The MIT License
|
||||||
* Copyright (c) 2014-2016 Ilkka Seppälä
|
* Copyright © 2014-2019 Ilkka Seppälä
|
||||||
*
|
*
|
||||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||||
* of this software and associated documentation files (the "Software"), to deal
|
* of this software and associated documentation files (the "Software"), to deal
|
||||||
@ -20,12 +20,11 @@
|
|||||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||||
* THE SOFTWARE.
|
* THE SOFTWARE.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
package com.iluwatar.abstractfactory;
|
package com.iluwatar.abstractfactory;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
*
|
* ElfCastle.
|
||||||
* ElfCastle
|
|
||||||
*
|
|
||||||
*/
|
*/
|
||||||
public class ElfCastle implements Castle {
|
public class ElfCastle implements Castle {
|
||||||
|
|
||||||
|
@ -1,6 +1,6 @@
|
|||||||
/**
|
/*
|
||||||
* The MIT License
|
* The MIT License
|
||||||
* Copyright (c) 2014-2016 Ilkka Seppälä
|
* Copyright © 2014-2019 Ilkka Seppälä
|
||||||
*
|
*
|
||||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||||
* of this software and associated documentation files (the "Software"), to deal
|
* of this software and associated documentation files (the "Software"), to deal
|
||||||
@ -20,12 +20,11 @@
|
|||||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||||
* THE SOFTWARE.
|
* THE SOFTWARE.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
package com.iluwatar.abstractfactory;
|
package com.iluwatar.abstractfactory;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
*
|
* ElfKing.
|
||||||
* ElfKing
|
|
||||||
*
|
|
||||||
*/
|
*/
|
||||||
public class ElfKing implements King {
|
public class ElfKing implements King {
|
||||||
|
|
||||||
|
@ -1,6 +1,6 @@
|
|||||||
/**
|
/*
|
||||||
* The MIT License
|
* The MIT License
|
||||||
* Copyright (c) 2014-2016 Ilkka Seppälä
|
* Copyright © 2014-2019 Ilkka Seppälä
|
||||||
*
|
*
|
||||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||||
* of this software and associated documentation files (the "Software"), to deal
|
* of this software and associated documentation files (the "Software"), to deal
|
||||||
@ -20,12 +20,11 @@
|
|||||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||||
* THE SOFTWARE.
|
* THE SOFTWARE.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
package com.iluwatar.abstractfactory;
|
package com.iluwatar.abstractfactory;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
*
|
|
||||||
* ElfKingdomFactory concrete factory.
|
* ElfKingdomFactory concrete factory.
|
||||||
*
|
|
||||||
*/
|
*/
|
||||||
public class ElfKingdomFactory implements KingdomFactory {
|
public class ElfKingdomFactory implements KingdomFactory {
|
||||||
|
|
||||||
|
@ -1,6 +1,6 @@
|
|||||||
/**
|
/*
|
||||||
* The MIT License
|
* The MIT License
|
||||||
* Copyright (c) 2014-2016 Ilkka Seppälä
|
* Copyright © 2014-2019 Ilkka Seppälä
|
||||||
*
|
*
|
||||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||||
* of this software and associated documentation files (the "Software"), to deal
|
* of this software and associated documentation files (the "Software"), to deal
|
||||||
@ -20,12 +20,11 @@
|
|||||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||||
* THE SOFTWARE.
|
* THE SOFTWARE.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
package com.iluwatar.abstractfactory;
|
package com.iluwatar.abstractfactory;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
*
|
* King interface.
|
||||||
* King interface
|
|
||||||
*
|
|
||||||
*/
|
*/
|
||||||
public interface King {
|
public interface King {
|
||||||
|
|
||||||
|
@ -1,6 +1,6 @@
|
|||||||
/**
|
/*
|
||||||
* The MIT License
|
* The MIT License
|
||||||
* Copyright (c) 2014-2016 Ilkka Seppälä
|
* Copyright © 2014-2019 Ilkka Seppälä
|
||||||
*
|
*
|
||||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||||
* of this software and associated documentation files (the "Software"), to deal
|
* of this software and associated documentation files (the "Software"), to deal
|
||||||
@ -20,12 +20,11 @@
|
|||||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||||
* THE SOFTWARE.
|
* THE SOFTWARE.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
package com.iluwatar.abstractfactory;
|
package com.iluwatar.abstractfactory;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
*
|
|
||||||
* KingdomFactory factory interface.
|
* KingdomFactory factory interface.
|
||||||
*
|
|
||||||
*/
|
*/
|
||||||
public interface KingdomFactory {
|
public interface KingdomFactory {
|
||||||
|
|
||||||
|
@ -1,6 +1,6 @@
|
|||||||
/**
|
/*
|
||||||
* The MIT License
|
* The MIT License
|
||||||
* Copyright (c) 2014-2016 Ilkka Seppälä
|
* Copyright © 2014-2019 Ilkka Seppälä
|
||||||
*
|
*
|
||||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||||
* of this software and associated documentation files (the "Software"), to deal
|
* of this software and associated documentation files (the "Software"), to deal
|
||||||
@ -20,12 +20,11 @@
|
|||||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||||
* THE SOFTWARE.
|
* THE SOFTWARE.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
package com.iluwatar.abstractfactory;
|
package com.iluwatar.abstractfactory;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
*
|
* OrcArmy.
|
||||||
* OrcArmy
|
|
||||||
*
|
|
||||||
*/
|
*/
|
||||||
public class OrcArmy implements Army {
|
public class OrcArmy implements Army {
|
||||||
|
|
||||||
|
@ -1,6 +1,6 @@
|
|||||||
/**
|
/*
|
||||||
* The MIT License
|
* The MIT License
|
||||||
* Copyright (c) 2014-2016 Ilkka Seppälä
|
* Copyright © 2014-2019 Ilkka Seppälä
|
||||||
*
|
*
|
||||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||||
* of this software and associated documentation files (the "Software"), to deal
|
* of this software and associated documentation files (the "Software"), to deal
|
||||||
@ -20,12 +20,11 @@
|
|||||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||||
* THE SOFTWARE.
|
* THE SOFTWARE.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
package com.iluwatar.abstractfactory;
|
package com.iluwatar.abstractfactory;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
*
|
* OrcCastle.
|
||||||
* OrcCastle
|
|
||||||
*
|
|
||||||
*/
|
*/
|
||||||
public class OrcCastle implements Castle {
|
public class OrcCastle implements Castle {
|
||||||
|
|
||||||
|
@ -1,6 +1,6 @@
|
|||||||
/**
|
/*
|
||||||
* The MIT License
|
* The MIT License
|
||||||
* Copyright (c) 2014-2016 Ilkka Seppälä
|
* Copyright © 2014-2019 Ilkka Seppälä
|
||||||
*
|
*
|
||||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||||
* of this software and associated documentation files (the "Software"), to deal
|
* of this software and associated documentation files (the "Software"), to deal
|
||||||
@ -20,12 +20,11 @@
|
|||||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||||
* THE SOFTWARE.
|
* THE SOFTWARE.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
package com.iluwatar.abstractfactory;
|
package com.iluwatar.abstractfactory;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
*
|
* OrcKing.
|
||||||
* OrcKing
|
|
||||||
*
|
|
||||||
*/
|
*/
|
||||||
public class OrcKing implements King {
|
public class OrcKing implements King {
|
||||||
|
|
||||||
|
@ -1,6 +1,6 @@
|
|||||||
/**
|
/*
|
||||||
* The MIT License
|
* The MIT License
|
||||||
* Copyright (c) 2014-2016 Ilkka Seppälä
|
* Copyright © 2014-2019 Ilkka Seppälä
|
||||||
*
|
*
|
||||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||||
* of this software and associated documentation files (the "Software"), to deal
|
* of this software and associated documentation files (the "Software"), to deal
|
||||||
@ -20,12 +20,11 @@
|
|||||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||||
* THE SOFTWARE.
|
* THE SOFTWARE.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
package com.iluwatar.abstractfactory;
|
package com.iluwatar.abstractfactory;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
*
|
|
||||||
* OrcKingdomFactory concrete factory.
|
* OrcKingdomFactory concrete factory.
|
||||||
*
|
|
||||||
*/
|
*/
|
||||||
public class OrcKingdomFactory implements KingdomFactory {
|
public class OrcKingdomFactory implements KingdomFactory {
|
||||||
|
|
||||||
|
@ -1,6 +1,6 @@
|
|||||||
/**
|
/*
|
||||||
* The MIT License
|
* The MIT License
|
||||||
* Copyright (c) 2014-2016 Ilkka Seppälä
|
* Copyright © 2014-2019 Ilkka Seppälä
|
||||||
*
|
*
|
||||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||||
* of this software and associated documentation files (the "Software"), to deal
|
* of this software and associated documentation files (the "Software"), to deal
|
||||||
@ -20,6 +20,7 @@
|
|||||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||||
* THE SOFTWARE.
|
* THE SOFTWARE.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
package com.iluwatar.abstractfactory;
|
package com.iluwatar.abstractfactory;
|
||||||
|
|
||||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||||
@ -27,12 +28,11 @@ import static org.junit.jupiter.api.Assertions.assertTrue;
|
|||||||
|
|
||||||
import com.iluwatar.abstractfactory.App.FactoryMaker;
|
import com.iluwatar.abstractfactory.App.FactoryMaker;
|
||||||
import com.iluwatar.abstractfactory.App.FactoryMaker.KingdomType;
|
import com.iluwatar.abstractfactory.App.FactoryMaker.KingdomType;
|
||||||
|
|
||||||
import org.junit.jupiter.api.BeforeEach;
|
import org.junit.jupiter.api.BeforeEach;
|
||||||
import org.junit.jupiter.api.Test;
|
import org.junit.jupiter.api.Test;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Test for abstract factory
|
* Test for abstract factory.
|
||||||
*/
|
*/
|
||||||
public class AbstractFactoryTest {
|
public class AbstractFactoryTest {
|
||||||
|
|
||||||
@ -48,30 +48,30 @@ public class AbstractFactoryTest {
|
|||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void king() {
|
public void king() {
|
||||||
final King elfKing = app.getKing(elfFactory);
|
final var elfKing = app.getKing(elfFactory);
|
||||||
assertTrue(elfKing instanceof ElfKing);
|
assertTrue(elfKing instanceof ElfKing);
|
||||||
assertEquals(ElfKing.DESCRIPTION, elfKing.getDescription());
|
assertEquals(ElfKing.DESCRIPTION, elfKing.getDescription());
|
||||||
final King orcKing = app.getKing(orcFactory);
|
final var orcKing = app.getKing(orcFactory);
|
||||||
assertTrue(orcKing instanceof OrcKing);
|
assertTrue(orcKing instanceof OrcKing);
|
||||||
assertEquals(OrcKing.DESCRIPTION, orcKing.getDescription());
|
assertEquals(OrcKing.DESCRIPTION, orcKing.getDescription());
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void castle() {
|
public void castle() {
|
||||||
final Castle elfCastle = app.getCastle(elfFactory);
|
final var elfCastle = app.getCastle(elfFactory);
|
||||||
assertTrue(elfCastle instanceof ElfCastle);
|
assertTrue(elfCastle instanceof ElfCastle);
|
||||||
assertEquals(ElfCastle.DESCRIPTION, elfCastle.getDescription());
|
assertEquals(ElfCastle.DESCRIPTION, elfCastle.getDescription());
|
||||||
final Castle orcCastle = app.getCastle(orcFactory);
|
final var orcCastle = app.getCastle(orcFactory);
|
||||||
assertTrue(orcCastle instanceof OrcCastle);
|
assertTrue(orcCastle instanceof OrcCastle);
|
||||||
assertEquals(OrcCastle.DESCRIPTION, orcCastle.getDescription());
|
assertEquals(OrcCastle.DESCRIPTION, orcCastle.getDescription());
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void army() {
|
public void army() {
|
||||||
final Army elfArmy = app.getArmy(elfFactory);
|
final var elfArmy = app.getArmy(elfFactory);
|
||||||
assertTrue(elfArmy instanceof ElfArmy);
|
assertTrue(elfArmy instanceof ElfArmy);
|
||||||
assertEquals(ElfArmy.DESCRIPTION, elfArmy.getDescription());
|
assertEquals(ElfArmy.DESCRIPTION, elfArmy.getDescription());
|
||||||
final Army orcArmy = app.getArmy(orcFactory);
|
final var orcArmy = app.getArmy(orcFactory);
|
||||||
assertTrue(orcArmy instanceof OrcArmy);
|
assertTrue(orcArmy instanceof OrcArmy);
|
||||||
assertEquals(OrcArmy.DESCRIPTION, orcArmy.getDescription());
|
assertEquals(OrcArmy.DESCRIPTION, orcArmy.getDescription());
|
||||||
}
|
}
|
||||||
@ -79,9 +79,9 @@ public class AbstractFactoryTest {
|
|||||||
@Test
|
@Test
|
||||||
public void createElfKingdom() {
|
public void createElfKingdom() {
|
||||||
app.createKingdom(elfFactory);
|
app.createKingdom(elfFactory);
|
||||||
final King king = app.getKing();
|
final var king = app.getKing();
|
||||||
final Castle castle = app.getCastle();
|
final var castle = app.getCastle();
|
||||||
final Army army = app.getArmy();
|
final var army = app.getArmy();
|
||||||
assertTrue(king instanceof ElfKing);
|
assertTrue(king instanceof ElfKing);
|
||||||
assertEquals(ElfKing.DESCRIPTION, king.getDescription());
|
assertEquals(ElfKing.DESCRIPTION, king.getDescription());
|
||||||
assertTrue(castle instanceof ElfCastle);
|
assertTrue(castle instanceof ElfCastle);
|
||||||
@ -93,9 +93,9 @@ public class AbstractFactoryTest {
|
|||||||
@Test
|
@Test
|
||||||
public void createOrcKingdom() {
|
public void createOrcKingdom() {
|
||||||
app.createKingdom(orcFactory);
|
app.createKingdom(orcFactory);
|
||||||
final King king = app.getKing();
|
final var king = app.getKing();
|
||||||
final Castle castle = app.getCastle();
|
final var castle = app.getCastle();
|
||||||
final Army army = app.getArmy();
|
final var army = app.getArmy();
|
||||||
assertTrue(king instanceof OrcKing);
|
assertTrue(king instanceof OrcKing);
|
||||||
assertEquals(OrcKing.DESCRIPTION, king.getDescription());
|
assertEquals(OrcKing.DESCRIPTION, king.getDescription());
|
||||||
assertTrue(castle instanceof OrcCastle);
|
assertTrue(castle instanceof OrcCastle);
|
||||||
|
@ -1,6 +1,6 @@
|
|||||||
/**
|
/*
|
||||||
* The MIT License
|
* The MIT License
|
||||||
* Copyright (c) 2014-2016 Ilkka Seppälä
|
* Copyright © 2014-2019 Ilkka Seppälä
|
||||||
*
|
*
|
||||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||||
* of this software and associated documentation files (the "Software"), to deal
|
* of this software and associated documentation files (the "Software"), to deal
|
||||||
@ -20,19 +20,17 @@
|
|||||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||||
* THE SOFTWARE.
|
* THE SOFTWARE.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
package com.iluwatar.abstractfactory;
|
package com.iluwatar.abstractfactory;
|
||||||
|
|
||||||
import org.junit.jupiter.api.Test;
|
import org.junit.jupiter.api.Test;
|
||||||
|
|
||||||
import java.io.IOException;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Tests that Abstract Factory example runs without errors.
|
* Tests that Abstract Factory example runs without errors.
|
||||||
*/
|
*/
|
||||||
public class AppTest {
|
public class AppTest {
|
||||||
@Test
|
@Test
|
||||||
public void test() throws IOException {
|
public void test() {
|
||||||
String[] args = {};
|
App.main(new String[]{});
|
||||||
App.main(args);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -33,7 +33,7 @@ The bad:
|
|||||||
* Parallel hierarchy of visitors has to be created for all members in visitable class hierarchy.
|
* Parallel hierarchy of visitors has to be created for all members in visitable class hierarchy.
|
||||||
|
|
||||||
## Related patterns
|
## Related patterns
|
||||||
* [Visitor Pattern](../visitor/README.md)
|
* [Visitor Pattern](../visitor/)
|
||||||
|
|
||||||
## Credits
|
## Credits
|
||||||
* [Acyclic Visitor](http://condor.depaul.edu/dmumaugh/OOT/Design-Principles/acv.pdf)
|
* [Acyclic Visitor](http://condor.depaul.edu/dmumaugh/OOT/Design-Principles/acv.pdf)
|
||||||
|
@ -1,7 +1,7 @@
|
|||||||
<!--
|
<!--
|
||||||
|
|
||||||
The MIT License
|
The MIT License
|
||||||
Copyright (c) 2014 Ilkka Seppälä
|
Copyright © 2014-2019 Ilkka Seppälä
|
||||||
|
|
||||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||||
of this software and associated documentation files (the "Software"), to deal
|
of this software and associated documentation files (the "Software"), to deal
|
||||||
@ -30,7 +30,7 @@
|
|||||||
<parent>
|
<parent>
|
||||||
<groupId>com.iluwatar</groupId>
|
<groupId>com.iluwatar</groupId>
|
||||||
<artifactId>java-design-patterns</artifactId>
|
<artifactId>java-design-patterns</artifactId>
|
||||||
<version>1.21.0</version>
|
<version>1.22.0-SNAPSHOT</version>
|
||||||
</parent>
|
</parent>
|
||||||
|
|
||||||
<artifactId>acyclic-visitor</artifactId>
|
<artifactId>acyclic-visitor</artifactId>
|
||||||
|
@ -1,6 +1,6 @@
|
|||||||
/**
|
/*
|
||||||
* The MIT License
|
* The MIT License
|
||||||
* Copyright (c) 2014-2016 Ilkka Seppälä
|
* Copyright © 2014-2019 Ilkka Seppälä
|
||||||
*
|
*
|
||||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||||
* of this software and associated documentation files (the "Software"), to deal
|
* of this software and associated documentation files (the "Software"), to deal
|
||||||
@ -20,12 +20,13 @@
|
|||||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||||
* THE SOFTWARE.
|
* THE SOFTWARE.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
package com.iluwatar.acyclicvisitor;
|
package com.iluwatar.acyclicvisitor;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* All ModemVisitor interface extends all visitor interfaces. This interface
|
* All ModemVisitor interface extends all visitor interfaces. This interface provides ease of use
|
||||||
* provides ease of use when a visitor needs to visit all modem types.
|
* when a visitor needs to visit all modem types.
|
||||||
*/
|
*/
|
||||||
public interface AllModemVisitor extends ZoomVisitor, HayesVisitor{
|
public interface AllModemVisitor extends ZoomVisitor, HayesVisitor {
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -1,6 +1,6 @@
|
|||||||
/**
|
/*
|
||||||
* The MIT License
|
* The MIT License
|
||||||
* Copyright (c) 2014-2016 Ilkka Seppälä
|
* Copyright © 2014-2019 Ilkka Seppälä
|
||||||
*
|
*
|
||||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||||
* of this software and associated documentation files (the "Software"), to deal
|
* of this software and associated documentation files (the "Software"), to deal
|
||||||
@ -20,33 +20,32 @@
|
|||||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||||
* THE SOFTWARE.
|
* THE SOFTWARE.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
package com.iluwatar.acyclicvisitor;
|
package com.iluwatar.acyclicvisitor;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* The Acyclic Visitor pattern allows new functions to be added to existing class
|
* The Acyclic Visitor pattern allows new functions to be added to existing class hierarchies
|
||||||
* hierarchies without affecting those hierarchies, and without creating the dependency
|
* without affecting those hierarchies, and without creating the dependency cycles that are inherent
|
||||||
* cycles that are inherent to the GoF Visitor pattern, by making the Visitor base class
|
* to the GoF Visitor pattern, by making the Visitor base class degenerate
|
||||||
* degenerate
|
*
|
||||||
* <p>
|
* <p>In this example the visitor base class is {@link ModemVisitor}. The base class of the visited
|
||||||
* In this example the visitor base class is {@link ModemVisitor}. The base class of the
|
* hierarchy is {@link Modem} and has two children {@link Hayes} and {@link Zoom} each one having
|
||||||
* visited hierarchy is {@link Modem} and has two children {@link Hayes} and {@link Zoom}
|
* its own visitor interface {@link HayesVisitor} and {@link ZoomVisitor} respectively. {@link
|
||||||
* each one having its own visitor interface {@link HayesVisitor} and {@link ZoomVisitor}
|
* ConfigureForUnixVisitor} and {@link ConfigureForDosVisitor} implement each derivative's visit
|
||||||
* respectively. {@link ConfigureForUnixVisitor} and {@link ConfigureForDosVisitor}
|
* method only if it is required
|
||||||
* implement each derivative's visit method only if it is required
|
|
||||||
*/
|
*/
|
||||||
public class App {
|
public class App {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Program's entry point
|
* Program's entry point.
|
||||||
*/
|
*/
|
||||||
|
public static void main(String[] args) {
|
||||||
public static void main(String[] args) {
|
var conUnix = new ConfigureForUnixVisitor();
|
||||||
ConfigureForUnixVisitor conUnix = new ConfigureForUnixVisitor();
|
var conDos = new ConfigureForDosVisitor();
|
||||||
ConfigureForDosVisitor conDos = new ConfigureForDosVisitor();
|
|
||||||
|
var zoom = new Zoom();
|
||||||
Zoom zoom = new Zoom();
|
var hayes = new Hayes();
|
||||||
Hayes hayes = new Hayes();
|
|
||||||
|
|
||||||
hayes.accept(conDos); // Hayes modem with Dos configurator
|
hayes.accept(conDos); // Hayes modem with Dos configurator
|
||||||
zoom.accept(conDos); // Zoom modem with Dos configurator
|
zoom.accept(conDos); // Zoom modem with Dos configurator
|
||||||
hayes.accept(conUnix); // Hayes modem with Unix configurator
|
hayes.accept(conUnix); // Hayes modem with Unix configurator
|
||||||
|
@ -1,6 +1,6 @@
|
|||||||
/**
|
/*
|
||||||
* The MIT License
|
* The MIT License
|
||||||
* Copyright (c) 2014-2016 Ilkka Seppälä
|
* Copyright © 2014-2019 Ilkka Seppälä
|
||||||
*
|
*
|
||||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||||
* of this software and associated documentation files (the "Software"), to deal
|
* of this software and associated documentation files (the "Software"), to deal
|
||||||
@ -20,14 +20,15 @@
|
|||||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||||
* THE SOFTWARE.
|
* THE SOFTWARE.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
package com.iluwatar.acyclicvisitor;
|
package com.iluwatar.acyclicvisitor;
|
||||||
|
|
||||||
import org.slf4j.Logger;
|
import org.slf4j.Logger;
|
||||||
import org.slf4j.LoggerFactory;
|
import org.slf4j.LoggerFactory;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* ConfigureForDosVisitor class implements both zoom's and hayes' visit method
|
* ConfigureForDosVisitor class implements both zoom's and hayes' visit method for Dos
|
||||||
* for Dos manufacturer
|
* manufacturer.
|
||||||
*/
|
*/
|
||||||
public class ConfigureForDosVisitor implements AllModemVisitor {
|
public class ConfigureForDosVisitor implements AllModemVisitor {
|
||||||
|
|
||||||
|
@ -1,6 +1,6 @@
|
|||||||
/**
|
/*
|
||||||
* The MIT License
|
* The MIT License
|
||||||
* Copyright (c) 2014-2016 Ilkka Seppälä
|
* Copyright © 2014-2019 Ilkka Seppälä
|
||||||
*
|
*
|
||||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||||
* of this software and associated documentation files (the "Software"), to deal
|
* of this software and associated documentation files (the "Software"), to deal
|
||||||
@ -20,15 +20,15 @@
|
|||||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||||
* THE SOFTWARE.
|
* THE SOFTWARE.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
package com.iluwatar.acyclicvisitor;
|
package com.iluwatar.acyclicvisitor;
|
||||||
|
|
||||||
import org.slf4j.Logger;
|
import org.slf4j.Logger;
|
||||||
import org.slf4j.LoggerFactory;
|
import org.slf4j.LoggerFactory;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* ConfigureForUnixVisitor class implements zoom's visit method for Unix
|
* ConfigureForUnixVisitor class implements zoom's visit method for Unix manufacturer, unlike
|
||||||
* manufacturer, unlike traditional visitor pattern, this class may selectively implement
|
* traditional visitor pattern, this class may selectively implement visit for other modems.
|
||||||
* visit for other modems.
|
|
||||||
*/
|
*/
|
||||||
public class ConfigureForUnixVisitor implements ZoomVisitor {
|
public class ConfigureForUnixVisitor implements ZoomVisitor {
|
||||||
|
|
||||||
|
@ -1,6 +1,6 @@
|
|||||||
/**
|
/*
|
||||||
* The MIT License
|
* The MIT License
|
||||||
* Copyright (c) 2014-2016 Ilkka Seppälä
|
* Copyright © 2014-2019 Ilkka Seppälä
|
||||||
*
|
*
|
||||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||||
* of this software and associated documentation files (the "Software"), to deal
|
* of this software and associated documentation files (the "Software"), to deal
|
||||||
@ -20,20 +20,21 @@
|
|||||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||||
* THE SOFTWARE.
|
* THE SOFTWARE.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
package com.iluwatar.acyclicvisitor;
|
package com.iluwatar.acyclicvisitor;
|
||||||
|
|
||||||
import org.slf4j.Logger;
|
import org.slf4j.Logger;
|
||||||
import org.slf4j.LoggerFactory;
|
import org.slf4j.LoggerFactory;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Hayes class implements its accept method
|
* Hayes class implements its accept method.
|
||||||
*/
|
*/
|
||||||
public class Hayes extends Modem {
|
public class Hayes extends Modem {
|
||||||
|
|
||||||
private static final Logger LOGGER = LoggerFactory.getLogger(ConfigureForDosVisitor.class);
|
private static final Logger LOGGER = LoggerFactory.getLogger(ConfigureForDosVisitor.class);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Accepts all visitors but honors only HayesVisitor
|
* Accepts all visitors but honors only HayesVisitor.
|
||||||
*/
|
*/
|
||||||
@Override
|
@Override
|
||||||
public void accept(ModemVisitor modemVisitor) {
|
public void accept(ModemVisitor modemVisitor) {
|
||||||
@ -44,10 +45,9 @@ public class Hayes extends Modem {
|
|||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Hayes' modem's toString
|
* Hayes' modem's toString method.
|
||||||
* method
|
|
||||||
*/
|
*/
|
||||||
@Override
|
@Override
|
||||||
public String toString() {
|
public String toString() {
|
||||||
|
@ -1,6 +1,6 @@
|
|||||||
/**
|
/*
|
||||||
* The MIT License
|
* The MIT License
|
||||||
* Copyright (c) 2014-2016 Ilkka Seppälä
|
* Copyright © 2014-2019 Ilkka Seppälä
|
||||||
*
|
*
|
||||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||||
* of this software and associated documentation files (the "Software"), to deal
|
* of this software and associated documentation files (the "Software"), to deal
|
||||||
@ -20,10 +20,11 @@
|
|||||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||||
* THE SOFTWARE.
|
* THE SOFTWARE.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
package com.iluwatar.acyclicvisitor;
|
package com.iluwatar.acyclicvisitor;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* HayesVisitor interface
|
* HayesVisitor interface.
|
||||||
*/
|
*/
|
||||||
public interface HayesVisitor extends ModemVisitor {
|
public interface HayesVisitor extends ModemVisitor {
|
||||||
void visit(Hayes hayes);
|
void visit(Hayes hayes);
|
||||||
|
@ -1,6 +1,6 @@
|
|||||||
/**
|
/*
|
||||||
* The MIT License
|
* The MIT License
|
||||||
* Copyright (c) 2014-2016 Ilkka Seppälä
|
* Copyright © 2014-2019 Ilkka Seppälä
|
||||||
*
|
*
|
||||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||||
* of this software and associated documentation files (the "Software"), to deal
|
* of this software and associated documentation files (the "Software"), to deal
|
||||||
@ -20,10 +20,11 @@
|
|||||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||||
* THE SOFTWARE.
|
* THE SOFTWARE.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
package com.iluwatar.acyclicvisitor;
|
package com.iluwatar.acyclicvisitor;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Modem abstract class
|
* Modem abstract class.
|
||||||
*/
|
*/
|
||||||
public abstract class Modem {
|
public abstract class Modem {
|
||||||
public abstract void accept(ModemVisitor modemVisitor);
|
public abstract void accept(ModemVisitor modemVisitor);
|
||||||
|
@ -1,6 +1,6 @@
|
|||||||
/**
|
/*
|
||||||
* The MIT License
|
* The MIT License
|
||||||
* Copyright (c) 2014-2016 Ilkka Seppälä
|
* Copyright © 2014-2019 Ilkka Seppälä
|
||||||
*
|
*
|
||||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||||
* of this software and associated documentation files (the "Software"), to deal
|
* of this software and associated documentation files (the "Software"), to deal
|
||||||
@ -20,12 +20,12 @@
|
|||||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||||
* THE SOFTWARE.
|
* THE SOFTWARE.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
package com.iluwatar.acyclicvisitor;
|
package com.iluwatar.acyclicvisitor;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* ModemVisitor interface does not contain any visit methods so that it does not
|
* ModemVisitor interface does not contain any visit methods so that it does not depend on the
|
||||||
* depend on the visited hierarchy. Each derivative's visit method is declared in
|
* visited hierarchy. Each derivative's visit method is declared in its own visitor interface
|
||||||
* its own visitor interface
|
|
||||||
*/
|
*/
|
||||||
public interface ModemVisitor {
|
public interface ModemVisitor {
|
||||||
// Visitor is a degenerate base class for all visitors.
|
// Visitor is a degenerate base class for all visitors.
|
||||||
|
@ -1,6 +1,6 @@
|
|||||||
/**
|
/*
|
||||||
* The MIT License
|
* The MIT License
|
||||||
* Copyright (c) 2014-2016 Ilkka Seppälä
|
* Copyright © 2014-2019 Ilkka Seppälä
|
||||||
*
|
*
|
||||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||||
* of this software and associated documentation files (the "Software"), to deal
|
* of this software and associated documentation files (the "Software"), to deal
|
||||||
@ -20,33 +20,33 @@
|
|||||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||||
* THE SOFTWARE.
|
* THE SOFTWARE.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
package com.iluwatar.acyclicvisitor;
|
package com.iluwatar.acyclicvisitor;
|
||||||
|
|
||||||
import org.slf4j.Logger;
|
import org.slf4j.Logger;
|
||||||
import org.slf4j.LoggerFactory;
|
import org.slf4j.LoggerFactory;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Zoom class implements its accept method
|
* Zoom class implements its accept method.
|
||||||
*/
|
*/
|
||||||
public class Zoom extends Modem {
|
public class Zoom extends Modem {
|
||||||
|
|
||||||
private static final Logger LOGGER = LoggerFactory.getLogger(ConfigureForDosVisitor.class);
|
private static final Logger LOGGER = LoggerFactory.getLogger(ConfigureForDosVisitor.class);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Accepts all visitors but honors only ZoomVisitor
|
* Accepts all visitors but honors only ZoomVisitor.
|
||||||
*/
|
*/
|
||||||
@Override
|
@Override
|
||||||
public void accept(ModemVisitor modemVisitor) {
|
public void accept(ModemVisitor modemVisitor) {
|
||||||
if (modemVisitor instanceof ZoomVisitor) {
|
if (modemVisitor instanceof ZoomVisitor) {
|
||||||
((ZoomVisitor) modemVisitor).visit(this);
|
((ZoomVisitor) modemVisitor).visit(this);
|
||||||
} else {
|
} else {
|
||||||
LOGGER.info("Only ZoomVisitor is allowed to visit Zoom modem");
|
LOGGER.info("Only ZoomVisitor is allowed to visit Zoom modem");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Zoom modem's toString
|
* Zoom modem's toString method.
|
||||||
* method
|
|
||||||
*/
|
*/
|
||||||
@Override
|
@Override
|
||||||
public String toString() {
|
public String toString() {
|
||||||
|
@ -1,6 +1,6 @@
|
|||||||
/**
|
/*
|
||||||
* The MIT License
|
* The MIT License
|
||||||
* Copyright (c) 2014-2016 Ilkka Seppälä
|
* Copyright © 2014-2019 Ilkka Seppälä
|
||||||
*
|
*
|
||||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||||
* of this software and associated documentation files (the "Software"), to deal
|
* of this software and associated documentation files (the "Software"), to deal
|
||||||
@ -20,10 +20,11 @@
|
|||||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||||
* THE SOFTWARE.
|
* THE SOFTWARE.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
package com.iluwatar.acyclicvisitor;
|
package com.iluwatar.acyclicvisitor;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* ZoomVisitor interface
|
* ZoomVisitor interface.
|
||||||
*/
|
*/
|
||||||
public interface ZoomVisitor extends ModemVisitor {
|
public interface ZoomVisitor extends ModemVisitor {
|
||||||
void visit(Zoom zoom);
|
void visit(Zoom zoom);
|
||||||
|
@ -1,6 +1,6 @@
|
|||||||
/**
|
/*
|
||||||
* The MIT License
|
* The MIT License
|
||||||
* Copyright (c) 2014-2016 Ilkka Seppälä
|
* Copyright © 2014-2019 Ilkka Seppälä
|
||||||
*
|
*
|
||||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||||
* of this software and associated documentation files (the "Software"), to deal
|
* of this software and associated documentation files (the "Software"), to deal
|
||||||
@ -20,20 +20,18 @@
|
|||||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||||
* THE SOFTWARE.
|
* THE SOFTWARE.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
package com.iluwatar.acyclicvisitor;
|
package com.iluwatar.acyclicvisitor;
|
||||||
|
|
||||||
import org.junit.jupiter.api.Test;
|
import org.junit.jupiter.api.Test;
|
||||||
|
|
||||||
import com.iluwatar.acyclicvisitor.App;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Tests that the Acyclic Visitor example runs without errors.
|
* Tests that the Acyclic Visitor example runs without errors.
|
||||||
*/
|
*/
|
||||||
public class AppTest {
|
public class AppTest {
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void test() {
|
public void test() {
|
||||||
String[] args = {};
|
App.main(new String[]{});
|
||||||
App.main(args);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
@ -1,6 +1,6 @@
|
|||||||
/**
|
/*
|
||||||
* The MIT License
|
* The MIT License
|
||||||
* Copyright (c) 2014-2016 Ilkka Seppälä
|
* Copyright © 2014-2019 Ilkka Seppälä
|
||||||
*
|
*
|
||||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||||
* of this software and associated documentation files (the "Software"), to deal
|
* of this software and associated documentation files (the "Software"), to deal
|
||||||
@ -20,22 +20,15 @@
|
|||||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||||
* THE SOFTWARE.
|
* THE SOFTWARE.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
package com.iluwatar.acyclicvisitor;
|
package com.iluwatar.acyclicvisitor;
|
||||||
|
|
||||||
import static org.assertj.core.api.Assertions.assertThat;
|
import static org.assertj.core.api.Assertions.assertThat;
|
||||||
import static org.assertj.core.groups.Tuple.tuple;
|
import static org.assertj.core.groups.Tuple.tuple;
|
||||||
import static org.mockito.Mockito.mock;
|
|
||||||
import static uk.org.lidalia.slf4jext.Level.INFO;
|
import static uk.org.lidalia.slf4jext.Level.INFO;
|
||||||
|
|
||||||
import org.junit.jupiter.api.AfterEach;
|
import org.junit.jupiter.api.AfterEach;
|
||||||
import org.junit.jupiter.api.Test;
|
import org.junit.jupiter.api.Test;
|
||||||
|
|
||||||
import com.iluwatar.acyclicvisitor.ConfigureForDosVisitor;
|
|
||||||
import com.iluwatar.acyclicvisitor.Hayes;
|
|
||||||
import com.iluwatar.acyclicvisitor.HayesVisitor;
|
|
||||||
import com.iluwatar.acyclicvisitor.Zoom;
|
|
||||||
import com.iluwatar.acyclicvisitor.ZoomVisitor;
|
|
||||||
|
|
||||||
import uk.org.lidalia.slf4jtest.TestLogger;
|
import uk.org.lidalia.slf4jtest.TestLogger;
|
||||||
import uk.org.lidalia.slf4jtest.TestLoggerFactory;
|
import uk.org.lidalia.slf4jtest.TestLoggerFactory;
|
||||||
|
|
||||||
@ -44,28 +37,30 @@ import uk.org.lidalia.slf4jtest.TestLoggerFactory;
|
|||||||
*/
|
*/
|
||||||
public class ConfigureForDosVisitorTest {
|
public class ConfigureForDosVisitorTest {
|
||||||
|
|
||||||
TestLogger logger = TestLoggerFactory.getTestLogger(ConfigureForDosVisitor.class);
|
private TestLogger logger = TestLoggerFactory.getTestLogger(ConfigureForDosVisitor.class);
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testVisitForZoom() {
|
public void testVisitForZoom() {
|
||||||
ConfigureForDosVisitor conDos = new ConfigureForDosVisitor();
|
var conDos = new ConfigureForDosVisitor();
|
||||||
Zoom zoom = new Zoom();
|
var zoom = new Zoom();
|
||||||
|
|
||||||
conDos.visit(zoom);
|
conDos.visit(zoom);
|
||||||
|
|
||||||
assertThat(logger.getLoggingEvents()).extracting("level", "message").contains(
|
assertThat(logger.getLoggingEvents())
|
||||||
tuple(INFO, zoom + " used with Dos configurator."));
|
.extracting("level", "message")
|
||||||
|
.contains(tuple(INFO, zoom + " used with Dos configurator."));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testVisitForHayes() {
|
public void testVisitForHayes() {
|
||||||
ConfigureForDosVisitor conDos = new ConfigureForDosVisitor();
|
var conDos = new ConfigureForDosVisitor();
|
||||||
Hayes hayes = new Hayes();
|
var hayes = new Hayes();
|
||||||
|
|
||||||
conDos.visit(hayes);
|
conDos.visit(hayes);
|
||||||
|
|
||||||
assertThat(logger.getLoggingEvents()).extracting("level", "message").contains(
|
assertThat(logger.getLoggingEvents())
|
||||||
tuple(INFO, hayes + " used with Dos configurator."));
|
.extracting("level", "message")
|
||||||
|
.contains(tuple(INFO, hayes + " used with Dos configurator."));
|
||||||
}
|
}
|
||||||
|
|
||||||
@AfterEach
|
@AfterEach
|
||||||
|
@ -1,6 +1,6 @@
|
|||||||
/**
|
/*
|
||||||
* The MIT License
|
* The MIT License
|
||||||
* Copyright (c) 2014-2016 Ilkka Seppälä
|
* Copyright © 2014-2019 Ilkka Seppälä
|
||||||
*
|
*
|
||||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||||
* of this software and associated documentation files (the "Software"), to deal
|
* of this software and associated documentation files (the "Software"), to deal
|
||||||
@ -20,6 +20,7 @@
|
|||||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||||
* THE SOFTWARE.
|
* THE SOFTWARE.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
package com.iluwatar.acyclicvisitor;
|
package com.iluwatar.acyclicvisitor;
|
||||||
|
|
||||||
import static org.assertj.core.api.Assertions.assertThat;
|
import static org.assertj.core.api.Assertions.assertThat;
|
||||||
@ -46,12 +47,13 @@ public class ConfigureForUnixVisitorTest {
|
|||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testVisitForZoom() {
|
public void testVisitForZoom() {
|
||||||
ConfigureForUnixVisitor conUnix = new ConfigureForUnixVisitor();
|
var conUnix = new ConfigureForUnixVisitor();
|
||||||
Zoom zoom = new Zoom();
|
var zoom = new Zoom();
|
||||||
|
|
||||||
conUnix.visit(zoom);
|
conUnix.visit(zoom);
|
||||||
|
|
||||||
assertThat(LOGGER.getLoggingEvents()).extracting("level", "message").contains(
|
assertThat(LOGGER.getLoggingEvents())
|
||||||
tuple(INFO, zoom + " used with Unix configurator."));
|
.extracting("level", "message")
|
||||||
|
.contains(tuple(INFO, zoom + " used with Unix configurator."));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,6 +1,6 @@
|
|||||||
/**
|
/*
|
||||||
* The MIT License
|
* The MIT License
|
||||||
* Copyright (c) 2014-2016 Ilkka Seppälä
|
* Copyright © 2014-2019 Ilkka Seppälä
|
||||||
*
|
*
|
||||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||||
* of this software and associated documentation files (the "Software"), to deal
|
* of this software and associated documentation files (the "Software"), to deal
|
||||||
@ -20,6 +20,7 @@
|
|||||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||||
* THE SOFTWARE.
|
* THE SOFTWARE.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
package com.iluwatar.acyclicvisitor;
|
package com.iluwatar.acyclicvisitor;
|
||||||
|
|
||||||
import static org.mockito.Matchers.eq;
|
import static org.mockito.Matchers.eq;
|
||||||
@ -29,11 +30,6 @@ import static org.mockito.Mockito.verifyZeroInteractions;
|
|||||||
|
|
||||||
import org.junit.jupiter.api.Test;
|
import org.junit.jupiter.api.Test;
|
||||||
|
|
||||||
import com.iluwatar.acyclicvisitor.ConfigureForDosVisitor;
|
|
||||||
import com.iluwatar.acyclicvisitor.ConfigureForUnixVisitor;
|
|
||||||
import com.iluwatar.acyclicvisitor.Hayes;
|
|
||||||
import com.iluwatar.acyclicvisitor.HayesVisitor;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Hayes test class
|
* Hayes test class
|
||||||
*/
|
*/
|
||||||
@ -41,8 +37,8 @@ public class HayesTest {
|
|||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testAcceptForDos() {
|
public void testAcceptForDos() {
|
||||||
Hayes hayes = new Hayes();
|
var hayes = new Hayes();
|
||||||
ConfigureForDosVisitor mockVisitor = mock(ConfigureForDosVisitor.class);
|
var mockVisitor = mock(ConfigureForDosVisitor.class);
|
||||||
|
|
||||||
hayes.accept(mockVisitor);
|
hayes.accept(mockVisitor);
|
||||||
verify((HayesVisitor)mockVisitor).visit(eq(hayes));
|
verify((HayesVisitor)mockVisitor).visit(eq(hayes));
|
||||||
@ -50,8 +46,8 @@ public class HayesTest {
|
|||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testAcceptForUnix() {
|
public void testAcceptForUnix() {
|
||||||
Hayes hayes = new Hayes();
|
var hayes = new Hayes();
|
||||||
ConfigureForUnixVisitor mockVisitor = mock(ConfigureForUnixVisitor.class);
|
var mockVisitor = mock(ConfigureForUnixVisitor.class);
|
||||||
|
|
||||||
hayes.accept(mockVisitor);
|
hayes.accept(mockVisitor);
|
||||||
|
|
||||||
|
@ -1,6 +1,6 @@
|
|||||||
/**
|
/*
|
||||||
* The MIT License
|
* The MIT License
|
||||||
* Copyright (c) 2014-2016 Ilkka Seppälä
|
* Copyright © 2014-2019 Ilkka Seppälä
|
||||||
*
|
*
|
||||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||||
* of this software and associated documentation files (the "Software"), to deal
|
* of this software and associated documentation files (the "Software"), to deal
|
||||||
@ -20,6 +20,7 @@
|
|||||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||||
* THE SOFTWARE.
|
* THE SOFTWARE.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
package com.iluwatar.acyclicvisitor;
|
package com.iluwatar.acyclicvisitor;
|
||||||
|
|
||||||
|
|
||||||
@ -29,11 +30,6 @@ import static org.mockito.Mockito.mock;
|
|||||||
|
|
||||||
import org.junit.jupiter.api.Test;
|
import org.junit.jupiter.api.Test;
|
||||||
|
|
||||||
import com.iluwatar.acyclicvisitor.ConfigureForDosVisitor;
|
|
||||||
import com.iluwatar.acyclicvisitor.ConfigureForUnixVisitor;
|
|
||||||
import com.iluwatar.acyclicvisitor.Zoom;
|
|
||||||
import com.iluwatar.acyclicvisitor.ZoomVisitor;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Zoom test class
|
* Zoom test class
|
||||||
*/
|
*/
|
||||||
@ -41,8 +37,8 @@ public class ZoomTest {
|
|||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testAcceptForDos() {
|
public void testAcceptForDos() {
|
||||||
Zoom zoom = new Zoom();
|
var zoom = new Zoom();
|
||||||
ConfigureForDosVisitor mockVisitor = mock(ConfigureForDosVisitor.class);
|
var mockVisitor = mock(ConfigureForDosVisitor.class);
|
||||||
|
|
||||||
zoom.accept(mockVisitor);
|
zoom.accept(mockVisitor);
|
||||||
verify((ZoomVisitor)mockVisitor).visit(eq(zoom));
|
verify((ZoomVisitor)mockVisitor).visit(eq(zoom));
|
||||||
@ -50,8 +46,8 @@ public class ZoomTest {
|
|||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testAcceptForUnix() {
|
public void testAcceptForUnix() {
|
||||||
Zoom zoom = new Zoom();
|
var zoom = new Zoom();
|
||||||
ConfigureForUnixVisitor mockVisitor = mock(ConfigureForUnixVisitor.class);
|
var mockVisitor = mock(ConfigureForUnixVisitor.class);
|
||||||
|
|
||||||
zoom.accept(mockVisitor);
|
zoom.accept(mockVisitor);
|
||||||
verify((ZoomVisitor)mockVisitor).visit(eq(zoom));
|
verify((ZoomVisitor)mockVisitor).visit(eq(zoom));
|
||||||
|
@ -56,15 +56,14 @@ public class FishingBoat {
|
|||||||
And captain expects an implementation of `RowingBoat` interface to be able to move
|
And captain expects an implementation of `RowingBoat` interface to be able to move
|
||||||
|
|
||||||
```java
|
```java
|
||||||
public class Captain implements RowingBoat {
|
public class Captain {
|
||||||
|
|
||||||
private RowingBoat rowingBoat;
|
private RowingBoat rowingBoat;
|
||||||
|
// default constructor and setter for rowingBoat
|
||||||
public Captain(RowingBoat rowingBoat) {
|
public Captain(RowingBoat rowingBoat) {
|
||||||
this.rowingBoat = rowingBoat;
|
this.rowingBoat = rowingBoat;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
|
||||||
public void row() {
|
public void row() {
|
||||||
rowingBoat.row();
|
rowingBoat.row();
|
||||||
}
|
}
|
||||||
@ -94,7 +93,7 @@ public class FishingBoatAdapter implements RowingBoat {
|
|||||||
And now the `Captain` can use the `FishingBoat` to escape the pirates.
|
And now the `Captain` can use the `FishingBoat` to escape the pirates.
|
||||||
|
|
||||||
```java
|
```java
|
||||||
Captain captain = new Captain(new FishingBoatAdapter());
|
var captain = new Captain(new FishingBoatAdapter());
|
||||||
captain.row();
|
captain.row();
|
||||||
```
|
```
|
||||||
|
|
||||||
|
@ -2,7 +2,7 @@
|
|||||||
<!--
|
<!--
|
||||||
|
|
||||||
The MIT License
|
The MIT License
|
||||||
Copyright (c) 2014-2016 Ilkka Seppälä
|
Copyright © 2014-2019 Ilkka Seppälä
|
||||||
|
|
||||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||||
of this software and associated documentation files (the "Software"), to deal
|
of this software and associated documentation files (the "Software"), to deal
|
||||||
@ -29,7 +29,7 @@
|
|||||||
<parent>
|
<parent>
|
||||||
<groupId>com.iluwatar</groupId>
|
<groupId>com.iluwatar</groupId>
|
||||||
<artifactId>java-design-patterns</artifactId>
|
<artifactId>java-design-patterns</artifactId>
|
||||||
<version>1.21.0</version>
|
<version>1.22.0-SNAPSHOT</version>
|
||||||
</parent>
|
</parent>
|
||||||
<artifactId>adapter</artifactId>
|
<artifactId>adapter</artifactId>
|
||||||
<dependencies>
|
<dependencies>
|
||||||
|
@ -1,6 +1,6 @@
|
|||||||
/**
|
/*
|
||||||
* The MIT License
|
* The MIT License
|
||||||
* Copyright (c) 2014-2016 Ilkka Seppälä
|
* Copyright © 2014-2019 Ilkka Seppälä
|
||||||
*
|
*
|
||||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||||
* of this software and associated documentation files (the "Software"), to deal
|
* of this software and associated documentation files (the "Software"), to deal
|
||||||
@ -20,6 +20,7 @@
|
|||||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||||
* THE SOFTWARE.
|
* THE SOFTWARE.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
package com.iluwatar.adapter;
|
package com.iluwatar.adapter;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -28,33 +29,33 @@ package com.iluwatar.adapter;
|
|||||||
* The Adapter design pattern allows otherwise incompatible classes to work together by converting
|
* The Adapter design pattern allows otherwise incompatible classes to work together by converting
|
||||||
* the interface of one class into an interface expected by the clients.
|
* the interface of one class into an interface expected by the clients.
|
||||||
*
|
*
|
||||||
* <p>
|
* <p>There are two variations of the Adapter pattern: The class adapter implements the adaptee's
|
||||||
* There are two variations of the Adapter pattern: The class adapter implements the adaptee's
|
|
||||||
* interface whereas the object adapter uses composition to contain the adaptee in the adapter
|
* interface whereas the object adapter uses composition to contain the adaptee in the adapter
|
||||||
* object. This example uses the object adapter approach.
|
* object. This example uses the object adapter approach.
|
||||||
*
|
*
|
||||||
* <p>
|
* <p>The Adapter ({@link FishingBoatAdapter}) converts the interface of the adaptee class ({@link
|
||||||
* The Adapter ({@link FishingBoatAdapter}) converts the interface of the adaptee class (
|
* FishingBoat}) into a suitable one expected by the client ({@link RowingBoat}).
|
||||||
* {@link FishingBoat}) into a suitable one expected by the client ( {@link RowingBoat} ).
|
|
||||||
*
|
|
||||||
* <p>
|
|
||||||
* The story of this implementation is this. <br>
|
|
||||||
* Pirates are coming! we need a {@link RowingBoat} to flee! We have a {@link FishingBoat} and our
|
|
||||||
* captain. We have no time to make up a new ship! we need to reuse this {@link FishingBoat}. The
|
|
||||||
* captain needs a rowing boat which he can operate. The spec is in {@link RowingBoat}. We will
|
|
||||||
* use the Adapter pattern to reuse {@link FishingBoat}.
|
|
||||||
*
|
*
|
||||||
|
* <p>The story of this implementation is this. <br> Pirates are coming! we need a {@link
|
||||||
|
* RowingBoat} to flee! We have a {@link FishingBoat} and our captain. We have no time to make up a
|
||||||
|
* new ship! we need to reuse this {@link FishingBoat}. The captain needs a rowing boat which he can
|
||||||
|
* operate. The spec is in {@link RowingBoat}. We will use the Adapter pattern to reuse {@link
|
||||||
|
* FishingBoat}.
|
||||||
*/
|
*/
|
||||||
public class App {
|
public final class App {
|
||||||
|
|
||||||
|
private App() {
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Program entry point.
|
* Program entry point.
|
||||||
*
|
*
|
||||||
* @param args command line args
|
* @param args command line args
|
||||||
*/
|
*/
|
||||||
public static void main(String[] args) {
|
public static void main(final String[] args) {
|
||||||
// The captain can only operate rowing boats but with adapter he is able to use fishing boats as well
|
// The captain can only operate rowing boats but with adapter he is able to
|
||||||
Captain captain = new Captain(new FishingBoatAdapter());
|
// use fishing boats as well
|
||||||
|
var captain = new Captain(new FishingBoatAdapter());
|
||||||
captain.row();
|
captain.row();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,6 +1,6 @@
|
|||||||
/**
|
/*
|
||||||
* The MIT License
|
* The MIT License
|
||||||
* Copyright (c) 2014-2016 Ilkka Seppälä
|
* Copyright © 2014-2019 Ilkka Seppälä
|
||||||
*
|
*
|
||||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||||
* of this software and associated documentation files (the "Software"), to deal
|
* of this software and associated documentation files (the "Software"), to deal
|
||||||
@ -20,27 +20,28 @@
|
|||||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||||
* THE SOFTWARE.
|
* THE SOFTWARE.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
package com.iluwatar.adapter;
|
package com.iluwatar.adapter;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* The Captain uses {@link RowingBoat} to sail. <br>
|
* The Captain uses {@link RowingBoat} to sail. <br> This is the client in the pattern.
|
||||||
* This is the client in the pattern.
|
|
||||||
*/
|
*/
|
||||||
public class Captain {
|
public final class Captain {
|
||||||
|
|
||||||
private RowingBoat rowingBoat;
|
private RowingBoat rowingBoat;
|
||||||
|
|
||||||
public Captain() {}
|
public Captain() {
|
||||||
|
|
||||||
public Captain(RowingBoat rowingBoat) {
|
|
||||||
this.rowingBoat = rowingBoat;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setRowingBoat(RowingBoat rowingBoat) {
|
public Captain(final RowingBoat boat) {
|
||||||
this.rowingBoat = rowingBoat;
|
this.rowingBoat = boat;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void row() {
|
void setRowingBoat(final RowingBoat boat) {
|
||||||
|
this.rowingBoat = boat;
|
||||||
|
}
|
||||||
|
|
||||||
|
void row() {
|
||||||
rowingBoat.row();
|
rowingBoat.row();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1,6 +1,6 @@
|
|||||||
/**
|
/*
|
||||||
* The MIT License
|
* The MIT License
|
||||||
* Copyright (c) 2014-2016 Ilkka Seppälä
|
* Copyright © 2014-2019 Ilkka Seppälä
|
||||||
*
|
*
|
||||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||||
* of this software and associated documentation files (the "Software"), to deal
|
* of this software and associated documentation files (the "Software"), to deal
|
||||||
@ -20,22 +20,22 @@
|
|||||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||||
* THE SOFTWARE.
|
* THE SOFTWARE.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
package com.iluwatar.adapter;
|
package com.iluwatar.adapter;
|
||||||
|
|
||||||
|
import static org.slf4j.LoggerFactory.getLogger;
|
||||||
|
|
||||||
import org.slf4j.Logger;
|
import org.slf4j.Logger;
|
||||||
import org.slf4j.LoggerFactory;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
*
|
* Device class (adaptee in the pattern). We want to reuse this class. Fishing boat moves by
|
||||||
* Device class (adaptee in the pattern). We want to reuse this class.
|
* sailing.
|
||||||
* Fishing boat moves by sailing.
|
|
||||||
*
|
|
||||||
*/
|
*/
|
||||||
public class FishingBoat {
|
final class FishingBoat {
|
||||||
|
|
||||||
private static final Logger LOGGER = LoggerFactory.getLogger(FishingBoat.class);
|
private static final Logger LOGGER = getLogger(FishingBoat.class);
|
||||||
|
|
||||||
public void sail() {
|
void sail() {
|
||||||
LOGGER.info("The fishing boat is sailing");
|
LOGGER.info("The fishing boat is sailing");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1,6 +1,6 @@
|
|||||||
/**
|
/*
|
||||||
* The MIT License
|
* The MIT License
|
||||||
* Copyright (c) 2014-2016 Ilkka Seppälä
|
* Copyright © 2014-2019 Ilkka Seppälä
|
||||||
*
|
*
|
||||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||||
* of this software and associated documentation files (the "Software"), to deal
|
* of this software and associated documentation files (the "Software"), to deal
|
||||||
@ -20,13 +20,12 @@
|
|||||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||||
* THE SOFTWARE.
|
* THE SOFTWARE.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
package com.iluwatar.adapter;
|
package com.iluwatar.adapter;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
*
|
|
||||||
* Adapter class. Adapts the interface of the device ({@link FishingBoat}) into {@link RowingBoat}
|
* Adapter class. Adapts the interface of the device ({@link FishingBoat}) into {@link RowingBoat}
|
||||||
* interface expected by the client ({@link Captain}).
|
* interface expected by the client ({@link Captain}).
|
||||||
*
|
|
||||||
*/
|
*/
|
||||||
public class FishingBoatAdapter implements RowingBoat {
|
public class FishingBoatAdapter implements RowingBoat {
|
||||||
|
|
||||||
@ -36,8 +35,7 @@ public class FishingBoatAdapter implements RowingBoat {
|
|||||||
boat = new FishingBoat();
|
boat = new FishingBoat();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
public final void row() {
|
||||||
public void row() {
|
|
||||||
boat.sail();
|
boat.sail();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,6 +1,6 @@
|
|||||||
/**
|
/*
|
||||||
* The MIT License
|
* The MIT License
|
||||||
* Copyright (c) 2014-2016 Ilkka Seppälä
|
* Copyright © 2014-2019 Ilkka Seppälä
|
||||||
*
|
*
|
||||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||||
* of this software and associated documentation files (the "Software"), to deal
|
* of this software and associated documentation files (the "Software"), to deal
|
||||||
@ -20,12 +20,11 @@
|
|||||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||||
* THE SOFTWARE.
|
* THE SOFTWARE.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
package com.iluwatar.adapter;
|
package com.iluwatar.adapter;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* The interface expected by the client.<br>
|
* The interface expected by the client.<br> A rowing boat is rowed to move.
|
||||||
* A rowing boat is rowed to move.
|
|
||||||
*
|
|
||||||
*/
|
*/
|
||||||
public interface RowingBoat {
|
public interface RowingBoat {
|
||||||
|
|
||||||
|
24
adapter/src/main/java/com/iluwatar/adapter/package-info.java
Normal file
24
adapter/src/main/java/com/iluwatar/adapter/package-info.java
Normal file
@ -0,0 +1,24 @@
|
|||||||
|
/*
|
||||||
|
* 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.adapter;
|
@ -1,6 +1,6 @@
|
|||||||
/**
|
/*
|
||||||
* The MIT License
|
* The MIT License
|
||||||
* Copyright (c) 2014-2016 Ilkka Seppälä
|
* Copyright © 2014-2019 Ilkka Seppälä
|
||||||
*
|
*
|
||||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||||
* of this software and associated documentation files (the "Software"), to deal
|
* of this software and associated documentation files (the "Software"), to deal
|
||||||
@ -20,20 +20,19 @@
|
|||||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||||
* THE SOFTWARE.
|
* THE SOFTWARE.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
package com.iluwatar.adapter;
|
package com.iluwatar.adapter;
|
||||||
|
|
||||||
import org.junit.jupiter.api.BeforeEach;
|
|
||||||
import org.junit.jupiter.api.Test;
|
|
||||||
|
|
||||||
import java.util.HashMap;
|
|
||||||
import java.util.Map;
|
|
||||||
|
|
||||||
import static org.mockito.Mockito.spy;
|
import static org.mockito.Mockito.spy;
|
||||||
import static org.mockito.Mockito.verify;
|
import static org.mockito.Mockito.verify;
|
||||||
|
|
||||||
|
import java.util.HashMap;
|
||||||
|
import java.util.Map;
|
||||||
|
import org.junit.jupiter.api.BeforeEach;
|
||||||
|
import org.junit.jupiter.api.Test;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Test class
|
* Test class
|
||||||
*
|
|
||||||
*/
|
*/
|
||||||
public class AdapterPatternTest {
|
public class AdapterPatternTest {
|
||||||
|
|
||||||
@ -50,29 +49,29 @@ public class AdapterPatternTest {
|
|||||||
public void setup() {
|
public void setup() {
|
||||||
beans = new HashMap<>();
|
beans = new HashMap<>();
|
||||||
|
|
||||||
FishingBoatAdapter fishingBoatAdapter = spy(new FishingBoatAdapter());
|
var fishingBoatAdapter = spy(new FishingBoatAdapter());
|
||||||
beans.put(FISHING_BEAN, fishingBoatAdapter);
|
beans.put(FISHING_BEAN, fishingBoatAdapter);
|
||||||
|
|
||||||
Captain captain = new Captain();
|
var captain = new Captain();
|
||||||
captain.setRowingBoat((FishingBoatAdapter) beans.get(FISHING_BEAN));
|
captain.setRowingBoat((FishingBoatAdapter) beans.get(FISHING_BEAN));
|
||||||
beans.put(ROWING_BEAN, captain);
|
beans.put(ROWING_BEAN, captain);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* This test asserts that when we use the row() method on a captain bean(client), it is
|
* This test asserts that when we use the row() method on a captain bean(client), it is internally
|
||||||
* internally calling sail method on the fishing boat object. The Adapter ({@link FishingBoatAdapter}
|
* calling sail method on the fishing boat object. The Adapter ({@link FishingBoatAdapter} )
|
||||||
* ) converts the interface of the target class ( {@link FishingBoat}) into a suitable one
|
* converts the interface of the target class ( {@link FishingBoat}) into a suitable one expected
|
||||||
* expected by the client ({@link Captain} ).
|
* by the client ({@link Captain} ).
|
||||||
*/
|
*/
|
||||||
@Test
|
@Test
|
||||||
public void testAdapter() {
|
public void testAdapter() {
|
||||||
Captain captain = (Captain) beans.get(ROWING_BEAN);
|
var captain = (Captain) beans.get(ROWING_BEAN);
|
||||||
|
|
||||||
// when captain moves
|
// when captain moves
|
||||||
captain.row();
|
captain.row();
|
||||||
|
|
||||||
// the captain internally calls the battleship object to move
|
// the captain internally calls the battleship object to move
|
||||||
RowingBoat adapter = (RowingBoat) beans.get(FISHING_BEAN);
|
var adapter = (RowingBoat) beans.get(FISHING_BEAN);
|
||||||
verify(adapter).row();
|
verify(adapter).row();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,6 +1,6 @@
|
|||||||
/**
|
/*
|
||||||
* The MIT License
|
* The MIT License
|
||||||
* Copyright (c) 2014-2016 Ilkka Seppälä
|
* Copyright © 2014-2019 Ilkka Seppälä
|
||||||
*
|
*
|
||||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||||
* of this software and associated documentation files (the "Software"), to deal
|
* of this software and associated documentation files (the "Software"), to deal
|
||||||
@ -20,19 +20,17 @@
|
|||||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||||
* THE SOFTWARE.
|
* THE SOFTWARE.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
package com.iluwatar.adapter;
|
package com.iluwatar.adapter;
|
||||||
|
|
||||||
import org.junit.jupiter.api.Test;
|
import org.junit.jupiter.api.Test;
|
||||||
|
|
||||||
import java.io.IOException;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Tests that Adapter example runs without errors.
|
* Tests that Adapter example runs without errors.
|
||||||
*/
|
*/
|
||||||
public class AppTest {
|
public class AppTest {
|
||||||
@Test
|
@Test
|
||||||
public void test() throws IOException {
|
public void test() {
|
||||||
String[] args = {};
|
App.main(new String[]{});
|
||||||
App.main(args);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -2,7 +2,7 @@
|
|||||||
<!--
|
<!--
|
||||||
|
|
||||||
The MIT License
|
The MIT License
|
||||||
Copyright (c) 2014-2016 Ilkka Seppälä
|
Copyright © 2014-2019 Ilkka Seppälä
|
||||||
|
|
||||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||||
of this software and associated documentation files (the "Software"), to deal
|
of this software and associated documentation files (the "Software"), to deal
|
||||||
@ -29,21 +29,11 @@
|
|||||||
<parent>
|
<parent>
|
||||||
<artifactId>aggregator-microservices</artifactId>
|
<artifactId>aggregator-microservices</artifactId>
|
||||||
<groupId>com.iluwatar</groupId>
|
<groupId>com.iluwatar</groupId>
|
||||||
<version>1.21.0</version>
|
<version>1.22.0-SNAPSHOT</version>
|
||||||
</parent>
|
</parent>
|
||||||
<modelVersion>4.0.0</modelVersion>
|
<modelVersion>4.0.0</modelVersion>
|
||||||
|
|
||||||
<artifactId>aggregator-service</artifactId>
|
<artifactId>aggregator-service</artifactId>
|
||||||
<packaging>jar</packaging>
|
<packaging>jar</packaging>
|
||||||
|
|
||||||
<dependencyManagement>
|
|
||||||
<dependencies>
|
|
||||||
<dependency>
|
|
||||||
<groupId>org.springframework.boot</groupId>
|
|
||||||
<artifactId>spring-boot-dependencies</artifactId>
|
|
||||||
</dependency>
|
|
||||||
</dependencies>
|
|
||||||
</dependencyManagement>
|
|
||||||
<dependencies>
|
<dependencies>
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>org.springframework</groupId>
|
<groupId>org.springframework</groupId>
|
||||||
@ -63,10 +53,6 @@
|
|||||||
<artifactId>mockito-core</artifactId>
|
<artifactId>mockito-core</artifactId>
|
||||||
<scope>test</scope>
|
<scope>test</scope>
|
||||||
</dependency>
|
</dependency>
|
||||||
<dependency>
|
|
||||||
<groupId>org.apache.httpcomponents</groupId>
|
|
||||||
<artifactId>httpclient</artifactId>
|
|
||||||
</dependency>
|
|
||||||
</dependencies>
|
</dependencies>
|
||||||
|
|
||||||
<build>
|
<build>
|
||||||
@ -74,7 +60,6 @@
|
|||||||
<plugin>
|
<plugin>
|
||||||
<groupId>org.springframework.boot</groupId>
|
<groupId>org.springframework.boot</groupId>
|
||||||
<artifactId>spring-boot-maven-plugin</artifactId>
|
<artifactId>spring-boot-maven-plugin</artifactId>
|
||||||
<version>${spring-boot.version}</version>
|
|
||||||
<executions>
|
<executions>
|
||||||
<execution>
|
<execution>
|
||||||
<goals>
|
<goals>
|
||||||
|
@ -1,17 +1,17 @@
|
|||||||
/**
|
/*
|
||||||
* The MIT License
|
* The MIT License
|
||||||
* Copyright (c) 2014-2016 Ilkka Seppälä
|
* Copyright © 2014-2019 Ilkka Seppälä
|
||||||
* <p/>
|
*
|
||||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||||
* of this software and associated documentation files (the "Software"), to deal
|
* of this software and associated documentation files (the "Software"), to deal
|
||||||
* in the Software without restriction, including without limitation the rights
|
* in the Software without restriction, including without limitation the rights
|
||||||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||||
* copies of the Software, and to permit persons to whom the Software is
|
* copies of the Software, and to permit persons to whom the Software is
|
||||||
* furnished to do so, subject to the following conditions:
|
* furnished to do so, subject to the following conditions:
|
||||||
* <p/>
|
*
|
||||||
* The above copyright notice and this permission notice shall be included in
|
* The above copyright notice and this permission notice shall be included in
|
||||||
* all copies or substantial portions of the Software.
|
* all copies or substantial portions of the Software.
|
||||||
* <p/>
|
*
|
||||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||||
@ -20,16 +20,19 @@
|
|||||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||||
* THE SOFTWARE.
|
* THE SOFTWARE.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
package com.iluwatar.aggregator.microservices;
|
package com.iluwatar.aggregator.microservices;
|
||||||
|
|
||||||
import org.springframework.web.bind.annotation.RequestMapping;
|
import static java.util.Objects.requireNonNullElse;
|
||||||
import org.springframework.web.bind.annotation.RestController;
|
|
||||||
|
|
||||||
import javax.annotation.Resource;
|
import javax.annotation.Resource;
|
||||||
|
import org.springframework.web.bind.annotation.RequestMapping;
|
||||||
|
import org.springframework.web.bind.annotation.RequestMethod;
|
||||||
|
import org.springframework.web.bind.annotation.RestController;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* The aggregator aggregates calls on various micro-services, collects
|
* The aggregator aggregates calls on various micro-services, collects data and further publishes
|
||||||
* data and further publishes them under a REST endpoint.
|
* them under a REST endpoint.
|
||||||
*/
|
*/
|
||||||
@RestController
|
@RestController
|
||||||
public class Aggregator {
|
public class Aggregator {
|
||||||
@ -47,11 +50,19 @@ public class Aggregator {
|
|||||||
*
|
*
|
||||||
* @return a Product.
|
* @return a Product.
|
||||||
*/
|
*/
|
||||||
@RequestMapping("/product")
|
@RequestMapping(path = "/product", method = RequestMethod.GET)
|
||||||
public Product getProduct() {
|
public Product getProduct() {
|
||||||
Product product = new Product();
|
|
||||||
product.setTitle(informationClient.getProductTitle());
|
var product = new Product();
|
||||||
product.setProductInventories(inventoryClient.getProductInventories());
|
var productTitle = informationClient.getProductTitle();
|
||||||
|
var productInventory = inventoryClient.getProductInventories();
|
||||||
|
|
||||||
|
//Fallback to error message
|
||||||
|
product.setTitle(requireNonNullElse(productTitle, "Error: Fetching Product Title Failed"));
|
||||||
|
|
||||||
|
//Fallback to default error inventory
|
||||||
|
product.setProductInventories(requireNonNullElse(productInventory, -1));
|
||||||
|
|
||||||
return product;
|
return product;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1,17 +1,17 @@
|
|||||||
/**
|
/*
|
||||||
* The MIT License
|
* The MIT License
|
||||||
* Copyright (c) 2014-2016 Ilkka Seppälä
|
* Copyright © 2014-2019 Ilkka Seppälä
|
||||||
* <p/>
|
*
|
||||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||||
* of this software and associated documentation files (the "Software"), to deal
|
* of this software and associated documentation files (the "Software"), to deal
|
||||||
* in the Software without restriction, including without limitation the rights
|
* in the Software without restriction, including without limitation the rights
|
||||||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||||
* copies of the Software, and to permit persons to whom the Software is
|
* copies of the Software, and to permit persons to whom the Software is
|
||||||
* furnished to do so, subject to the following conditions:
|
* furnished to do so, subject to the following conditions:
|
||||||
* <p/>
|
*
|
||||||
* The above copyright notice and this permission notice shall be included in
|
* The above copyright notice and this permission notice shall be included in
|
||||||
* all copies or substantial portions of the Software.
|
* all copies or substantial portions of the Software.
|
||||||
* <p/>
|
*
|
||||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||||
@ -20,19 +20,20 @@
|
|||||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||||
* THE SOFTWARE.
|
* THE SOFTWARE.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
package com.iluwatar.aggregator.microservices;
|
package com.iluwatar.aggregator.microservices;
|
||||||
|
|
||||||
import org.springframework.boot.SpringApplication;
|
import org.springframework.boot.SpringApplication;
|
||||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Spring Boot EntryPoint Class
|
* Spring Boot EntryPoint Class.
|
||||||
*/
|
*/
|
||||||
@SpringBootApplication
|
@SpringBootApplication
|
||||||
public class App {
|
public class App {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Program entry point
|
* Program entry point.
|
||||||
*
|
*
|
||||||
* @param args command line args
|
* @param args command line args
|
||||||
*/
|
*/
|
||||||
|
@ -1,17 +1,17 @@
|
|||||||
/**
|
/*
|
||||||
* The MIT License
|
* The MIT License
|
||||||
* Copyright (c) 2014-2016 Ilkka Seppälä
|
* Copyright © 2014-2019 Ilkka Seppälä
|
||||||
* <p/>
|
*
|
||||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||||
* of this software and associated documentation files (the "Software"), to deal
|
* of this software and associated documentation files (the "Software"), to deal
|
||||||
* in the Software without restriction, including without limitation the rights
|
* in the Software without restriction, including without limitation the rights
|
||||||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||||
* copies of the Software, and to permit persons to whom the Software is
|
* copies of the Software, and to permit persons to whom the Software is
|
||||||
* furnished to do so, subject to the following conditions:
|
* furnished to do so, subject to the following conditions:
|
||||||
* <p/>
|
*
|
||||||
* The above copyright notice and this permission notice shall be included in
|
* The above copyright notice and this permission notice shall be included in
|
||||||
* all copies or substantial portions of the Software.
|
* all copies or substantial portions of the Software.
|
||||||
* <p/>
|
*
|
||||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||||
@ -20,6 +20,7 @@
|
|||||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||||
* THE SOFTWARE.
|
* THE SOFTWARE.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
package com.iluwatar.aggregator.microservices;
|
package com.iluwatar.aggregator.microservices;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -1,17 +1,17 @@
|
|||||||
/**
|
/*
|
||||||
* The MIT License
|
* The MIT License
|
||||||
* Copyright (c) 2014-2016 Ilkka Seppälä
|
* Copyright © 2014-2019 Ilkka Seppälä
|
||||||
* <p/>
|
*
|
||||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||||
* of this software and associated documentation files (the "Software"), to deal
|
* of this software and associated documentation files (the "Software"), to deal
|
||||||
* in the Software without restriction, including without limitation the rights
|
* in the Software without restriction, including without limitation the rights
|
||||||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||||
* copies of the Software, and to permit persons to whom the Software is
|
* copies of the Software, and to permit persons to whom the Software is
|
||||||
* furnished to do so, subject to the following conditions:
|
* furnished to do so, subject to the following conditions:
|
||||||
* <p/>
|
*
|
||||||
* The above copyright notice and this permission notice shall be included in
|
* The above copyright notice and this permission notice shall be included in
|
||||||
* all copies or substantial portions of the Software.
|
* all copies or substantial portions of the Software.
|
||||||
* <p/>
|
*
|
||||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||||
@ -20,6 +20,7 @@
|
|||||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||||
* THE SOFTWARE.
|
* THE SOFTWARE.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
package com.iluwatar.aggregator.microservices;
|
package com.iluwatar.aggregator.microservices;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -1,17 +1,17 @@
|
|||||||
/**
|
/*
|
||||||
* The MIT License
|
* The MIT License
|
||||||
* Copyright (c) 2014-2016 Ilkka Seppälä
|
* Copyright © 2014-2019 Ilkka Seppälä
|
||||||
* <p/>
|
*
|
||||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||||
* of this software and associated documentation files (the "Software"), to deal
|
* of this software and associated documentation files (the "Software"), to deal
|
||||||
* in the Software without restriction, including without limitation the rights
|
* in the Software without restriction, including without limitation the rights
|
||||||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||||
* copies of the Software, and to permit persons to whom the Software is
|
* copies of the Software, and to permit persons to whom the Software is
|
||||||
* furnished to do so, subject to the following conditions:
|
* furnished to do so, subject to the following conditions:
|
||||||
* <p/>
|
*
|
||||||
* The above copyright notice and this permission notice shall be included in
|
* The above copyright notice and this permission notice shall be included in
|
||||||
* all copies or substantial portions of the Software.
|
* all copies or substantial portions of the Software.
|
||||||
* <p/>
|
*
|
||||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||||
@ -20,19 +20,18 @@
|
|||||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||||
* THE SOFTWARE.
|
* THE SOFTWARE.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
package com.iluwatar.aggregator.microservices;
|
package com.iluwatar.aggregator.microservices;
|
||||||
|
|
||||||
import org.apache.http.client.methods.CloseableHttpResponse;
|
import java.io.IOException;
|
||||||
import org.apache.http.client.methods.HttpGet;
|
import java.net.URI;
|
||||||
import org.apache.http.impl.client.CloseableHttpClient;
|
import java.net.http.HttpClient;
|
||||||
import org.apache.http.impl.client.HttpClients;
|
import java.net.http.HttpRequest;
|
||||||
import org.apache.http.util.EntityUtils;
|
import java.net.http.HttpResponse;
|
||||||
import org.slf4j.Logger;
|
import org.slf4j.Logger;
|
||||||
import org.slf4j.LoggerFactory;
|
import org.slf4j.LoggerFactory;
|
||||||
import org.springframework.stereotype.Component;
|
import org.springframework.stereotype.Component;
|
||||||
|
|
||||||
import java.io.IOException;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* An adapter to communicate with information micro-service.
|
* An adapter to communicate with information micro-service.
|
||||||
*/
|
*/
|
||||||
@ -43,15 +42,19 @@ public class ProductInformationClientImpl implements ProductInformationClient {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public String getProductTitle() {
|
public String getProductTitle() {
|
||||||
String response = null;
|
var request = HttpRequest.newBuilder()
|
||||||
try (CloseableHttpClient httpClient = HttpClients.createDefault()) {
|
.GET()
|
||||||
HttpGet httpGet = new HttpGet("http://localhost:51515/information");
|
.uri(URI.create("http://localhost:51515/information"))
|
||||||
try (CloseableHttpResponse httpResponse = httpClient.execute(httpGet)) {
|
.build();
|
||||||
response = EntityUtils.toString(httpResponse.getEntity());
|
var client = HttpClient.newHttpClient();
|
||||||
}
|
try {
|
||||||
} catch (IOException e) {
|
var httpResponse = client.send(request, HttpResponse.BodyHandlers.ofString());
|
||||||
LOGGER.error("Exception caught.", e);
|
return httpResponse.body();
|
||||||
|
} catch (IOException ioe) {
|
||||||
|
LOGGER.error("IOException Occurred", ioe);
|
||||||
|
} catch (InterruptedException ie) {
|
||||||
|
LOGGER.error("InterruptedException Occurred", ie);
|
||||||
}
|
}
|
||||||
return response;
|
return null;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,17 +1,17 @@
|
|||||||
/**
|
/*
|
||||||
* The MIT License
|
* The MIT License
|
||||||
* Copyright (c) 2014-2016 Ilkka Seppälä
|
* Copyright © 2014-2019 Ilkka Seppälä
|
||||||
* <p/>
|
*
|
||||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||||
* of this software and associated documentation files (the "Software"), to deal
|
* of this software and associated documentation files (the "Software"), to deal
|
||||||
* in the Software without restriction, including without limitation the rights
|
* in the Software without restriction, including without limitation the rights
|
||||||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||||
* copies of the Software, and to permit persons to whom the Software is
|
* copies of the Software, and to permit persons to whom the Software is
|
||||||
* furnished to do so, subject to the following conditions:
|
* furnished to do so, subject to the following conditions:
|
||||||
* <p/>
|
*
|
||||||
* The above copyright notice and this permission notice shall be included in
|
* The above copyright notice and this permission notice shall be included in
|
||||||
* all copies or substantial portions of the Software.
|
* all copies or substantial portions of the Software.
|
||||||
* <p/>
|
*
|
||||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||||
@ -20,6 +20,7 @@
|
|||||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||||
* THE SOFTWARE.
|
* THE SOFTWARE.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
package com.iluwatar.aggregator.microservices;
|
package com.iluwatar.aggregator.microservices;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -27,5 +28,5 @@ package com.iluwatar.aggregator.microservices;
|
|||||||
*/
|
*/
|
||||||
public interface ProductInventoryClient {
|
public interface ProductInventoryClient {
|
||||||
|
|
||||||
int getProductInventories();
|
Integer getProductInventories();
|
||||||
}
|
}
|
||||||
|
@ -1,17 +1,17 @@
|
|||||||
/**
|
/*
|
||||||
* The MIT License
|
* The MIT License
|
||||||
* Copyright (c) 2014-2016 Ilkka Seppälä
|
* Copyright © 2014-2019 Ilkka Seppälä
|
||||||
* <p/>
|
*
|
||||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||||
* of this software and associated documentation files (the "Software"), to deal
|
* of this software and associated documentation files (the "Software"), to deal
|
||||||
* in the Software without restriction, including without limitation the rights
|
* in the Software without restriction, including without limitation the rights
|
||||||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||||
* copies of the Software, and to permit persons to whom the Software is
|
* copies of the Software, and to permit persons to whom the Software is
|
||||||
* furnished to do so, subject to the following conditions:
|
* furnished to do so, subject to the following conditions:
|
||||||
* <p/>
|
*
|
||||||
* The above copyright notice and this permission notice shall be included in
|
* The above copyright notice and this permission notice shall be included in
|
||||||
* all copies or substantial portions of the Software.
|
* all copies or substantial portions of the Software.
|
||||||
* <p/>
|
*
|
||||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||||
@ -20,19 +20,18 @@
|
|||||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||||
* THE SOFTWARE.
|
* THE SOFTWARE.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
package com.iluwatar.aggregator.microservices;
|
package com.iluwatar.aggregator.microservices;
|
||||||
|
|
||||||
import org.apache.http.client.methods.CloseableHttpResponse;
|
import java.io.IOException;
|
||||||
import org.apache.http.client.methods.HttpGet;
|
import java.net.URI;
|
||||||
import org.apache.http.impl.client.CloseableHttpClient;
|
import java.net.http.HttpClient;
|
||||||
import org.apache.http.impl.client.HttpClients;
|
import java.net.http.HttpRequest;
|
||||||
import org.apache.http.util.EntityUtils;
|
import java.net.http.HttpResponse;
|
||||||
import org.slf4j.Logger;
|
import org.slf4j.Logger;
|
||||||
import org.slf4j.LoggerFactory;
|
import org.slf4j.LoggerFactory;
|
||||||
import org.springframework.stereotype.Component;
|
import org.springframework.stereotype.Component;
|
||||||
|
|
||||||
import java.io.IOException;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* An adapter to communicate with inventory micro-service.
|
* An adapter to communicate with inventory micro-service.
|
||||||
*/
|
*/
|
||||||
@ -42,16 +41,26 @@ public class ProductInventoryClientImpl implements ProductInventoryClient {
|
|||||||
private static final Logger LOGGER = LoggerFactory.getLogger(ProductInventoryClientImpl.class);
|
private static final Logger LOGGER = LoggerFactory.getLogger(ProductInventoryClientImpl.class);
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public int getProductInventories() {
|
public Integer getProductInventories() {
|
||||||
String response = "0";
|
var response = "";
|
||||||
try (CloseableHttpClient httpClient = HttpClients.createDefault()) {
|
|
||||||
HttpGet httpGet = new HttpGet("http://localhost:51516/inventories");
|
var request = HttpRequest.newBuilder()
|
||||||
try (CloseableHttpResponse httpResponse = httpClient.execute(httpGet)) {
|
.GET()
|
||||||
response = EntityUtils.toString(httpResponse.getEntity());
|
.uri(URI.create("http://localhost:51516/inventories"))
|
||||||
}
|
.build();
|
||||||
} catch (IOException e) {
|
var client = HttpClient.newHttpClient();
|
||||||
LOGGER.error("Exception caught.", e);
|
try {
|
||||||
|
var httpResponse = client.send(request, HttpResponse.BodyHandlers.ofString());
|
||||||
|
response = httpResponse.body();
|
||||||
|
} catch (IOException ioe) {
|
||||||
|
LOGGER.error("IOException Occurred", ioe);
|
||||||
|
} catch (InterruptedException ie) {
|
||||||
|
LOGGER.error("InterruptedException Occurred", ie);
|
||||||
|
}
|
||||||
|
if ("".equalsIgnoreCase(response)) {
|
||||||
|
return null;
|
||||||
|
} else {
|
||||||
|
return Integer.parseInt(response);
|
||||||
}
|
}
|
||||||
return Integer.parseInt(response);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,6 +1,6 @@
|
|||||||
#
|
#
|
||||||
# The MIT License
|
# The MIT License
|
||||||
# Copyright (c) 2014-2016 Ilkka Seppälä
|
# Copyright © 2014-2019 Ilkka Seppälä
|
||||||
#
|
#
|
||||||
# Permission is hereby granted, free of charge, to any person obtaining a copy
|
# Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||||
# of this software and associated documentation files (the "Software"), to deal
|
# of this software and associated documentation files (the "Software"), to deal
|
||||||
@ -20,5 +20,4 @@
|
|||||||
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||||
# THE SOFTWARE.
|
# THE SOFTWARE.
|
||||||
#
|
#
|
||||||
|
|
||||||
server.port=50004
|
server.port=50004
|
@ -1,17 +1,17 @@
|
|||||||
/**
|
/*
|
||||||
* The MIT License
|
* The MIT License
|
||||||
* Copyright (c) 2014-2016 Ilkka Seppälä
|
* Copyright © 2014-2019 Ilkka Seppälä
|
||||||
* <p/>
|
*
|
||||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||||
* of this software and associated documentation files (the "Software"), to deal
|
* of this software and associated documentation files (the "Software"), to deal
|
||||||
* in the Software without restriction, including without limitation the rights
|
* in the Software without restriction, including without limitation the rights
|
||||||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||||
* copies of the Software, and to permit persons to whom the Software is
|
* copies of the Software, and to permit persons to whom the Software is
|
||||||
* furnished to do so, subject to the following conditions:
|
* furnished to do so, subject to the following conditions:
|
||||||
* <p/>
|
*
|
||||||
* The above copyright notice and this permission notice shall be included in
|
* The above copyright notice and this permission notice shall be included in
|
||||||
* all copies or substantial portions of the Software.
|
* all copies or substantial portions of the Software.
|
||||||
* <p/>
|
*
|
||||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||||
@ -20,17 +20,18 @@
|
|||||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||||
* THE SOFTWARE.
|
* THE SOFTWARE.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
package com.iluwatar.aggregator.microservices;
|
package com.iluwatar.aggregator.microservices;
|
||||||
|
|
||||||
|
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||||
|
import static org.mockito.Mockito.when;
|
||||||
|
|
||||||
import org.junit.jupiter.api.BeforeEach;
|
import org.junit.jupiter.api.BeforeEach;
|
||||||
import org.junit.jupiter.api.Test;
|
import org.junit.jupiter.api.Test;
|
||||||
import org.mockito.InjectMocks;
|
import org.mockito.InjectMocks;
|
||||||
import org.mockito.Mock;
|
import org.mockito.Mock;
|
||||||
import org.mockito.MockitoAnnotations;
|
import org.mockito.MockitoAnnotations;
|
||||||
|
|
||||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
|
||||||
import static org.mockito.Mockito.when;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Test Aggregation of domain objects
|
* Test Aggregation of domain objects
|
||||||
*/
|
*/
|
||||||
@ -55,13 +56,13 @@ public class AggregatorTest {
|
|||||||
*/
|
*/
|
||||||
@Test
|
@Test
|
||||||
public void testGetProduct() {
|
public void testGetProduct() {
|
||||||
String title = "The Product Title.";
|
var title = "The Product Title.";
|
||||||
int inventories = 5;
|
var inventories = 5;
|
||||||
|
|
||||||
when(informationClient.getProductTitle()).thenReturn(title);
|
when(informationClient.getProductTitle()).thenReturn(title);
|
||||||
when(inventoryClient.getProductInventories()).thenReturn(inventories);
|
when(inventoryClient.getProductInventories()).thenReturn(inventories);
|
||||||
|
|
||||||
Product testProduct = aggregator.getProduct();
|
var testProduct = aggregator.getProduct();
|
||||||
|
|
||||||
assertEquals(title, testProduct.getTitle());
|
assertEquals(title, testProduct.getTitle());
|
||||||
assertEquals(inventories, testProduct.getProductInventories());
|
assertEquals(inventories, testProduct.getProductInventories());
|
||||||
|
@ -2,7 +2,7 @@
|
|||||||
<!--
|
<!--
|
||||||
|
|
||||||
The MIT License
|
The MIT License
|
||||||
Copyright (c) 2014-2016 Ilkka Seppälä
|
Copyright © 2014-2019 Ilkka Seppälä
|
||||||
|
|
||||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||||
of this software and associated documentation files (the "Software"), to deal
|
of this software and associated documentation files (the "Software"), to deal
|
||||||
@ -29,21 +29,13 @@
|
|||||||
<parent>
|
<parent>
|
||||||
<artifactId>aggregator-microservices</artifactId>
|
<artifactId>aggregator-microservices</artifactId>
|
||||||
<groupId>com.iluwatar</groupId>
|
<groupId>com.iluwatar</groupId>
|
||||||
<version>1.21.0</version>
|
<version>1.22.0-SNAPSHOT</version>
|
||||||
</parent>
|
</parent>
|
||||||
<modelVersion>4.0.0</modelVersion>
|
<modelVersion>4.0.0</modelVersion>
|
||||||
|
|
||||||
<artifactId>information-microservice</artifactId>
|
<artifactId>information-microservice</artifactId>
|
||||||
<packaging>jar</packaging>
|
<packaging>jar</packaging>
|
||||||
|
|
||||||
<dependencyManagement>
|
|
||||||
<dependencies>
|
|
||||||
<dependency>
|
|
||||||
<groupId>org.springframework.boot</groupId>
|
|
||||||
<artifactId>spring-boot-dependencies</artifactId>
|
|
||||||
</dependency>
|
|
||||||
</dependencies>
|
|
||||||
</dependencyManagement>
|
|
||||||
<dependencies>
|
<dependencies>
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>org.springframework</groupId>
|
<groupId>org.springframework</groupId>
|
||||||
@ -65,7 +57,6 @@
|
|||||||
<plugin>
|
<plugin>
|
||||||
<groupId>org.springframework.boot</groupId>
|
<groupId>org.springframework.boot</groupId>
|
||||||
<artifactId>spring-boot-maven-plugin</artifactId>
|
<artifactId>spring-boot-maven-plugin</artifactId>
|
||||||
<version>${spring-boot.version}</version>
|
|
||||||
<executions>
|
<executions>
|
||||||
<execution>
|
<execution>
|
||||||
<goals>
|
<goals>
|
||||||
|
@ -1,17 +1,17 @@
|
|||||||
/**
|
/*
|
||||||
* The MIT License
|
* The MIT License
|
||||||
* Copyright (c) 2014-2016 Ilkka Seppälä
|
* Copyright © 2014-2019 Ilkka Seppälä
|
||||||
* <p/>
|
*
|
||||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||||
* of this software and associated documentation files (the "Software"), to deal
|
* of this software and associated documentation files (the "Software"), to deal
|
||||||
* in the Software without restriction, including without limitation the rights
|
* in the Software without restriction, including without limitation the rights
|
||||||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||||
* copies of the Software, and to permit persons to whom the Software is
|
* copies of the Software, and to permit persons to whom the Software is
|
||||||
* furnished to do so, subject to the following conditions:
|
* furnished to do so, subject to the following conditions:
|
||||||
* <p/>
|
*
|
||||||
* The above copyright notice and this permission notice shall be included in
|
* The above copyright notice and this permission notice shall be included in
|
||||||
* all copies or substantial portions of the Software.
|
* all copies or substantial portions of the Software.
|
||||||
* <p/>
|
*
|
||||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||||
@ -20,6 +20,7 @@
|
|||||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||||
* THE SOFTWARE.
|
* THE SOFTWARE.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
package com.iluwatar.information.microservice;
|
package com.iluwatar.information.microservice;
|
||||||
|
|
||||||
import org.springframework.boot.SpringApplication;
|
import org.springframework.boot.SpringApplication;
|
||||||
|
@ -1,17 +1,17 @@
|
|||||||
/**
|
/*
|
||||||
* The MIT License
|
* The MIT License
|
||||||
* Copyright (c) 2014-2016 Ilkka Seppälä
|
* Copyright © 2014-2019 Ilkka Seppälä
|
||||||
* <p/>
|
*
|
||||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||||
* of this software and associated documentation files (the "Software"), to deal
|
* of this software and associated documentation files (the "Software"), to deal
|
||||||
* in the Software without restriction, including without limitation the rights
|
* in the Software without restriction, including without limitation the rights
|
||||||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||||
* copies of the Software, and to permit persons to whom the Software is
|
* copies of the Software, and to permit persons to whom the Software is
|
||||||
* furnished to do so, subject to the following conditions:
|
* furnished to do so, subject to the following conditions:
|
||||||
* <p/>
|
*
|
||||||
* The above copyright notice and this permission notice shall be included in
|
* The above copyright notice and this permission notice shall be included in
|
||||||
* all copies or substantial portions of the Software.
|
* all copies or substantial portions of the Software.
|
||||||
* <p/>
|
*
|
||||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||||
@ -20,6 +20,7 @@
|
|||||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||||
* THE SOFTWARE.
|
* THE SOFTWARE.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
package com.iluwatar.information.microservice;
|
package com.iluwatar.information.microservice;
|
||||||
|
|
||||||
import org.springframework.web.bind.annotation.RequestMapping;
|
import org.springframework.web.bind.annotation.RequestMapping;
|
||||||
@ -27,7 +28,7 @@ import org.springframework.web.bind.annotation.RequestMethod;
|
|||||||
import org.springframework.web.bind.annotation.RestController;
|
import org.springframework.web.bind.annotation.RestController;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Controller providing endpoints to retrieve information about products
|
* Controller providing endpoints to retrieve information about products.
|
||||||
*/
|
*/
|
||||||
@RestController
|
@RestController
|
||||||
public class InformationController {
|
public class InformationController {
|
||||||
|
@ -1,6 +1,6 @@
|
|||||||
#
|
#
|
||||||
# The MIT License
|
# The MIT License
|
||||||
# Copyright (c) 2014-2016 Ilkka Seppälä
|
# Copyright © 2014-2019 Ilkka Seppälä
|
||||||
#
|
#
|
||||||
# Permission is hereby granted, free of charge, to any person obtaining a copy
|
# Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||||
# of this software and associated documentation files (the "Software"), to deal
|
# of this software and associated documentation files (the "Software"), to deal
|
||||||
@ -20,5 +20,4 @@
|
|||||||
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||||
# THE SOFTWARE.
|
# THE SOFTWARE.
|
||||||
#
|
#
|
||||||
|
|
||||||
server.port=51515
|
server.port=51515
|
@ -1,17 +1,17 @@
|
|||||||
/**
|
/*
|
||||||
* The MIT License
|
* The MIT License
|
||||||
* Copyright (c) 2014-2016 Ilkka Seppälä
|
* Copyright © 2014-2019 Ilkka Seppälä
|
||||||
* <p/>
|
*
|
||||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||||
* of this software and associated documentation files (the "Software"), to deal
|
* of this software and associated documentation files (the "Software"), to deal
|
||||||
* in the Software without restriction, including without limitation the rights
|
* in the Software without restriction, including without limitation the rights
|
||||||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||||
* copies of the Software, and to permit persons to whom the Software is
|
* copies of the Software, and to permit persons to whom the Software is
|
||||||
* furnished to do so, subject to the following conditions:
|
* furnished to do so, subject to the following conditions:
|
||||||
* <p/>
|
*
|
||||||
* The above copyright notice and this permission notice shall be included in
|
* The above copyright notice and this permission notice shall be included in
|
||||||
* all copies or substantial portions of the Software.
|
* all copies or substantial portions of the Software.
|
||||||
* <p/>
|
*
|
||||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||||
@ -20,12 +20,13 @@
|
|||||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||||
* THE SOFTWARE.
|
* THE SOFTWARE.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
package com.iluwatar.information.microservice;
|
package com.iluwatar.information.microservice;
|
||||||
|
|
||||||
import org.junit.jupiter.api.Test;
|
|
||||||
|
|
||||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||||
|
|
||||||
|
import org.junit.jupiter.api.Test;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Test for Information Rest Controller
|
* Test for Information Rest Controller
|
||||||
*/
|
*/
|
||||||
@ -33,10 +34,8 @@ public class InformationControllerTest {
|
|||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void shouldGetProductTitle() {
|
public void shouldGetProductTitle() {
|
||||||
InformationController infoController = new InformationController();
|
var infoController = new InformationController();
|
||||||
|
var title = infoController.getProductTitle();
|
||||||
String title = infoController.getProductTitle();
|
|
||||||
|
|
||||||
assertEquals("The Product Title.", title);
|
assertEquals("The Product Title.", title);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -2,7 +2,7 @@
|
|||||||
<!--
|
<!--
|
||||||
|
|
||||||
The MIT License
|
The MIT License
|
||||||
Copyright (c) 2014-2016 Ilkka Seppälä
|
Copyright © 2014-2019 Ilkka Seppälä
|
||||||
|
|
||||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||||
of this software and associated documentation files (the "Software"), to deal
|
of this software and associated documentation files (the "Software"), to deal
|
||||||
@ -29,21 +29,12 @@
|
|||||||
<parent>
|
<parent>
|
||||||
<artifactId>aggregator-microservices</artifactId>
|
<artifactId>aggregator-microservices</artifactId>
|
||||||
<groupId>com.iluwatar</groupId>
|
<groupId>com.iluwatar</groupId>
|
||||||
<version>1.21.0</version>
|
<version>1.22.0-SNAPSHOT</version>
|
||||||
</parent>
|
</parent>
|
||||||
<modelVersion>4.0.0</modelVersion>
|
<modelVersion>4.0.0</modelVersion>
|
||||||
|
|
||||||
<artifactId>inventory-microservice</artifactId>
|
<artifactId>inventory-microservice</artifactId>
|
||||||
<packaging>jar</packaging>
|
|
||||||
|
|
||||||
<dependencyManagement>
|
<packaging>jar</packaging>
|
||||||
<dependencies>
|
|
||||||
<dependency>
|
|
||||||
<groupId>org.springframework.boot</groupId>
|
|
||||||
<artifactId>spring-boot-dependencies</artifactId>
|
|
||||||
</dependency>
|
|
||||||
</dependencies>
|
|
||||||
</dependencyManagement>
|
|
||||||
<dependencies>
|
<dependencies>
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>org.springframework</groupId>
|
<groupId>org.springframework</groupId>
|
||||||
@ -65,7 +56,6 @@
|
|||||||
<plugin>
|
<plugin>
|
||||||
<groupId>org.springframework.boot</groupId>
|
<groupId>org.springframework.boot</groupId>
|
||||||
<artifactId>spring-boot-maven-plugin</artifactId>
|
<artifactId>spring-boot-maven-plugin</artifactId>
|
||||||
<version>${spring-boot.version}</version>
|
|
||||||
<executions>
|
<executions>
|
||||||
<execution>
|
<execution>
|
||||||
<goals>
|
<goals>
|
||||||
|
@ -1,17 +1,17 @@
|
|||||||
/**
|
/*
|
||||||
* The MIT License
|
* The MIT License
|
||||||
* Copyright (c) 2014-2016 Ilkka Seppälä
|
* Copyright © 2014-2019 Ilkka Seppälä
|
||||||
* <p/>
|
*
|
||||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||||
* of this software and associated documentation files (the "Software"), to deal
|
* of this software and associated documentation files (the "Software"), to deal
|
||||||
* in the Software without restriction, including without limitation the rights
|
* in the Software without restriction, including without limitation the rights
|
||||||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||||
* copies of the Software, and to permit persons to whom the Software is
|
* copies of the Software, and to permit persons to whom the Software is
|
||||||
* furnished to do so, subject to the following conditions:
|
* furnished to do so, subject to the following conditions:
|
||||||
* <p/>
|
*
|
||||||
* The above copyright notice and this permission notice shall be included in
|
* The above copyright notice and this permission notice shall be included in
|
||||||
* all copies or substantial portions of the Software.
|
* all copies or substantial portions of the Software.
|
||||||
* <p/>
|
*
|
||||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||||
@ -20,6 +20,7 @@
|
|||||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||||
* THE SOFTWARE.
|
* THE SOFTWARE.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
package com.iluwatar.inventory.microservice;
|
package com.iluwatar.inventory.microservice;
|
||||||
|
|
||||||
import org.springframework.boot.SpringApplication;
|
import org.springframework.boot.SpringApplication;
|
||||||
|
@ -1,17 +1,17 @@
|
|||||||
/**
|
/*
|
||||||
* The MIT License
|
* The MIT License
|
||||||
* Copyright (c) 2014-2016 Ilkka Seppälä
|
* Copyright © 2014-2019 Ilkka Seppälä
|
||||||
* <p/>
|
*
|
||||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||||
* of this software and associated documentation files (the "Software"), to deal
|
* of this software and associated documentation files (the "Software"), to deal
|
||||||
* in the Software without restriction, including without limitation the rights
|
* in the Software without restriction, including without limitation the rights
|
||||||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||||
* copies of the Software, and to permit persons to whom the Software is
|
* copies of the Software, and to permit persons to whom the Software is
|
||||||
* furnished to do so, subject to the following conditions:
|
* furnished to do so, subject to the following conditions:
|
||||||
* <p/>
|
*
|
||||||
* The above copyright notice and this permission notice shall be included in
|
* The above copyright notice and this permission notice shall be included in
|
||||||
* all copies or substantial portions of the Software.
|
* all copies or substantial portions of the Software.
|
||||||
* <p/>
|
*
|
||||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||||
@ -20,6 +20,7 @@
|
|||||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||||
* THE SOFTWARE.
|
* THE SOFTWARE.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
package com.iluwatar.inventory.microservice;
|
package com.iluwatar.inventory.microservice;
|
||||||
|
|
||||||
import org.springframework.web.bind.annotation.RequestMapping;
|
import org.springframework.web.bind.annotation.RequestMapping;
|
||||||
@ -27,7 +28,7 @@ import org.springframework.web.bind.annotation.RequestMethod;
|
|||||||
import org.springframework.web.bind.annotation.RestController;
|
import org.springframework.web.bind.annotation.RestController;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Controller providing endpoints to retrieve product inventories
|
* Controller providing endpoints to retrieve product inventories.
|
||||||
*/
|
*/
|
||||||
@RestController
|
@RestController
|
||||||
public class InventoryController {
|
public class InventoryController {
|
||||||
|
@ -1,6 +1,6 @@
|
|||||||
#
|
#
|
||||||
# The MIT License
|
# The MIT License
|
||||||
# Copyright (c) 2014-2016 Ilkka Seppälä
|
# Copyright © 2014-2019 Ilkka Seppälä
|
||||||
#
|
#
|
||||||
# Permission is hereby granted, free of charge, to any person obtaining a copy
|
# Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||||
# of this software and associated documentation files (the "Software"), to deal
|
# of this software and associated documentation files (the "Software"), to deal
|
||||||
@ -20,5 +20,4 @@
|
|||||||
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||||
# THE SOFTWARE.
|
# THE SOFTWARE.
|
||||||
#
|
#
|
||||||
|
|
||||||
server.port=51516
|
server.port=51516
|
@ -1,17 +1,17 @@
|
|||||||
/**
|
/*
|
||||||
* The MIT License
|
* The MIT License
|
||||||
* Copyright (c) 2014-2016 Ilkka Seppälä
|
* Copyright © 2014-2019 Ilkka Seppälä
|
||||||
* <p/>
|
*
|
||||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||||
* of this software and associated documentation files (the "Software"), to deal
|
* of this software and associated documentation files (the "Software"), to deal
|
||||||
* in the Software without restriction, including without limitation the rights
|
* in the Software without restriction, including without limitation the rights
|
||||||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||||
* copies of the Software, and to permit persons to whom the Software is
|
* copies of the Software, and to permit persons to whom the Software is
|
||||||
* furnished to do so, subject to the following conditions:
|
* furnished to do so, subject to the following conditions:
|
||||||
* <p/>
|
*
|
||||||
* The above copyright notice and this permission notice shall be included in
|
* The above copyright notice and this permission notice shall be included in
|
||||||
* all copies or substantial portions of the Software.
|
* all copies or substantial portions of the Software.
|
||||||
* <p/>
|
*
|
||||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||||
@ -20,22 +20,21 @@
|
|||||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||||
* THE SOFTWARE.
|
* THE SOFTWARE.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
package com.iluwatar.inventory.microservice;
|
package com.iluwatar.inventory.microservice;
|
||||||
|
|
||||||
import org.junit.jupiter.api.Test;
|
|
||||||
|
|
||||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||||
|
|
||||||
|
import org.junit.jupiter.api.Test;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Test Inventory Rest Controller
|
* Test Inventory Rest Controller
|
||||||
*/
|
*/
|
||||||
public class InventoryControllerTest {
|
public class InventoryControllerTest {
|
||||||
@Test
|
@Test
|
||||||
public void testGetProductInventories() {
|
public void testGetProductInventories() {
|
||||||
InventoryController inventoryController = new InventoryController();
|
var inventoryController = new InventoryController();
|
||||||
|
var numberOfInventories = inventoryController.getProductInventories();
|
||||||
int numberOfInventories = inventoryController.getProductInventories();
|
|
||||||
|
|
||||||
assertEquals(5, numberOfInventories);
|
assertEquals(5, numberOfInventories);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -2,7 +2,7 @@
|
|||||||
<!--
|
<!--
|
||||||
|
|
||||||
The MIT License
|
The MIT License
|
||||||
Copyright (c) 2014-2016 Ilkka Seppälä
|
Copyright © 2014-2019 Ilkka Seppälä
|
||||||
|
|
||||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||||
of this software and associated documentation files (the "Software"), to deal
|
of this software and associated documentation files (the "Software"), to deal
|
||||||
@ -29,7 +29,7 @@
|
|||||||
<parent>
|
<parent>
|
||||||
<artifactId>java-design-patterns</artifactId>
|
<artifactId>java-design-patterns</artifactId>
|
||||||
<groupId>com.iluwatar</groupId>
|
<groupId>com.iluwatar</groupId>
|
||||||
<version>1.21.0</version>
|
<version>1.22.0-SNAPSHOT</version>
|
||||||
</parent>
|
</parent>
|
||||||
<modelVersion>4.0.0</modelVersion>
|
<modelVersion>4.0.0</modelVersion>
|
||||||
<artifactId>aggregator-microservices</artifactId>
|
<artifactId>aggregator-microservices</artifactId>
|
||||||
|
@ -42,7 +42,7 @@ A remote services represented as a singleton.
|
|||||||
```java
|
```java
|
||||||
public class RemoteService implements RemoteServiceInterface {
|
public class RemoteService implements RemoteServiceInterface {
|
||||||
|
|
||||||
private static final Logger LOGGER = LoggerFactory.getLogger(RemoteService.class);
|
private static final Logger LOGGER = LoggerFactory.getLogger(RemoteService.class);
|
||||||
private static RemoteService service = null;
|
private static RemoteService service = null;
|
||||||
|
|
||||||
static synchronized RemoteService getRemoteService() {
|
static synchronized RemoteService getRemoteService() {
|
||||||
@ -56,14 +56,14 @@ public class RemoteService implements RemoteServiceInterface {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public long doRemoteFunction(int value) {
|
public long doRemoteFunction(int value) {
|
||||||
|
|
||||||
long waitTime = (long) Math.floor(Math.random() * 1000);
|
long waitTime = (long) Math.floor(Math.random() * 1000);
|
||||||
|
|
||||||
try {
|
try {
|
||||||
sleep(waitTime);
|
sleep(waitTime);
|
||||||
} catch (InterruptedException e) {
|
} catch (InterruptedException e) {
|
||||||
LOGGER.error("Thread sleep interrupted", e)
|
LOGGER.error("Thread sleep interrupted", e);
|
||||||
}
|
}
|
||||||
|
|
||||||
return waitTime >= 200 ? value * 10 : -1;
|
return waitTime >= 200 ? value * 10 : -1;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -74,53 +74,50 @@ A service ambassador adding additional features such as logging, latency checks
|
|||||||
```java
|
```java
|
||||||
public class ServiceAmbassador implements RemoteServiceInterface {
|
public class ServiceAmbassador implements RemoteServiceInterface {
|
||||||
|
|
||||||
private static final Logger LOGGER = LoggerFactory.getLogger(ServiceAmbassador.class);
|
private static final Logger LOGGER = LoggerFactory.getLogger(ServiceAmbassador.class);
|
||||||
private static final int RETRIES = 3;
|
private static final int RETRIES = 3;
|
||||||
private static final int DELAY_MS = 3000;
|
private static final int DELAY_MS = 3000;
|
||||||
|
|
||||||
ServiceAmbassador() {}
|
ServiceAmbassador() {
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public long doRemoteFunction(int value) {
|
public long doRemoteFunction(int value) {
|
||||||
|
return safeCall(value);
|
||||||
|
}
|
||||||
|
|
||||||
return safeCall(value);
|
private long checkLatency(int value) {
|
||||||
}
|
var startTime = System.currentTimeMillis();
|
||||||
|
var result = RemoteService.getRemoteService().doRemoteFunction(value);
|
||||||
|
var timeTaken = System.currentTimeMillis() - startTime;
|
||||||
|
|
||||||
private long checkLatency(int value) {
|
LOGGER.info("Time taken (ms): " + timeTaken);
|
||||||
RemoteService service = RemoteService.getRemoteService();
|
return result;
|
||||||
long startTime = System.currentTimeMillis();
|
}
|
||||||
long result = service.doRemoteFunction(value);
|
|
||||||
long timeTaken = System.currentTimeMillis() - startTime;
|
|
||||||
|
|
||||||
LOGGER.info("Time taken (ms): " + timeTaken);
|
private long safeCall(int value) {
|
||||||
return result;
|
var retries = 0;
|
||||||
}
|
var result = (long) FAILURE;
|
||||||
|
|
||||||
private long safeCall(int value) {
|
for (int i = 0; i < RETRIES; i++) {
|
||||||
|
if (retries >= RETRIES) {
|
||||||
|
return FAILURE;
|
||||||
|
}
|
||||||
|
|
||||||
int retries = 0;
|
if ((result = checkLatency(value)) == FAILURE) {
|
||||||
long result = -1;
|
LOGGER.info("Failed to reach remote: (" + (i + 1) + ")");
|
||||||
|
retries++;
|
||||||
for (int i = 0; i < RETRIES; i++) {
|
try {
|
||||||
|
sleep(DELAY_MS);
|
||||||
if (retries >= RETRIES) {
|
} catch (InterruptedException e) {
|
||||||
return -1;
|
LOGGER.error("Thread sleep state interrupted", e);
|
||||||
}
|
|
||||||
|
|
||||||
if ((result = checkLatency(value)) == -1) {
|
|
||||||
LOGGER.info("Failed to reach remote: (" + (i + 1) + ")");
|
|
||||||
retries++;
|
|
||||||
try {
|
|
||||||
sleep(DELAY_MS);
|
|
||||||
} catch (InterruptedException e) {
|
|
||||||
LOGGER.error("Thread sleep state interrupted", e);
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
return result;
|
} else {
|
||||||
|
break;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
return result;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
@ -129,27 +126,28 @@ A client has a local service ambassador used to interact with the remote service
|
|||||||
```java
|
```java
|
||||||
public class Client {
|
public class Client {
|
||||||
|
|
||||||
private ServiceAmbassador serviceAmbassador;
|
private static final Logger LOGGER = LoggerFactory.getLogger(Client.class);
|
||||||
|
private final ServiceAmbassador serviceAmbassador = new ServiceAmbassador();
|
||||||
|
|
||||||
Client() {
|
long useService(int value) {
|
||||||
serviceAmbassador = new ServiceAmbassador();
|
var result = serviceAmbassador.doRemoteFunction(value);
|
||||||
}
|
LOGGER.info("Service result: " + result);
|
||||||
|
return result;
|
||||||
long useService(int value) {
|
}
|
||||||
long result = serviceAmbassador.doRemoteFunction(value);
|
|
||||||
LOGGER.info("Service result: " + result)
|
|
||||||
return result;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
And here are two clients using the service.
|
And here are two clients using the service.
|
||||||
|
|
||||||
```java
|
```java
|
||||||
Client host1 = new Client();
|
public class App {
|
||||||
Client host2 = new Client();
|
public static void main(String[] args) {
|
||||||
host1.useService(12);
|
var host1 = new Client();
|
||||||
host2.useService(73);
|
var host2 = new Client();
|
||||||
|
host1.useService(12);
|
||||||
|
host2.useService(73);
|
||||||
|
}
|
||||||
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
## Applicability
|
## Applicability
|
||||||
|
@ -2,7 +2,7 @@
|
|||||||
<!--
|
<!--
|
||||||
|
|
||||||
The MIT License
|
The MIT License
|
||||||
Copyright (c) 2014-2016 Ilkka Seppälä
|
Copyright © 2014-2019 Ilkka Seppälä
|
||||||
|
|
||||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||||
of this software and associated documentation files (the "Software"), to deal
|
of this software and associated documentation files (the "Software"), to deal
|
||||||
@ -29,7 +29,7 @@
|
|||||||
<parent>
|
<parent>
|
||||||
<artifactId>java-design-patterns</artifactId>
|
<artifactId>java-design-patterns</artifactId>
|
||||||
<groupId>com.iluwatar</groupId>
|
<groupId>com.iluwatar</groupId>
|
||||||
<version>1.21.0</version>
|
<version>1.22.0-SNAPSHOT</version>
|
||||||
</parent>
|
</parent>
|
||||||
<modelVersion>4.0.0</modelVersion>
|
<modelVersion>4.0.0</modelVersion>
|
||||||
<artifactId>ambassador</artifactId>
|
<artifactId>ambassador</artifactId>
|
||||||
|
@ -1,6 +1,6 @@
|
|||||||
/**
|
/*
|
||||||
* The MIT License
|
* The MIT License
|
||||||
* Copyright (c) 2014-2016 Ilkka Seppälä
|
* Copyright © 2014-2019 Ilkka Seppälä
|
||||||
*
|
*
|
||||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||||
* of this software and associated documentation files (the "Software"), to deal
|
* of this software and associated documentation files (the "Software"), to deal
|
||||||
@ -20,33 +20,33 @@
|
|||||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||||
* THE SOFTWARE.
|
* THE SOFTWARE.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
package com.iluwatar.ambassador;
|
package com.iluwatar.ambassador;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
*
|
|
||||||
* The ambassador pattern creates a helper service that sends network requests on behalf of a
|
* The ambassador pattern creates a helper service that sends network requests on behalf of a
|
||||||
* client. It is often used in cloud-based applications to offload features of a remote service.
|
* client. It is often used in cloud-based applications to offload features of a remote service.
|
||||||
*
|
*
|
||||||
* An ambassador service can be thought of as an out-of-process proxy that is co-located with
|
* <p>An ambassador service can be thought of as an out-of-process proxy that is co-located with
|
||||||
* the client. Similar to the proxy design pattern, the ambassador service provides an interface
|
* the client. Similar to the proxy design pattern, the ambassador service provides an interface for
|
||||||
* for another remote service. In addition to the interface, the ambassador provides extra
|
* another remote service. In addition to the interface, the ambassador provides extra functionality
|
||||||
* functionality and features, specifically offloaded common connectivity tasks. This usually
|
* and features, specifically offloaded common connectivity tasks. This usually consists of
|
||||||
* consists of monitoring, logging, routing, security etc. This is extremely useful in
|
* monitoring, logging, routing, security etc. This is extremely useful in legacy applications where
|
||||||
* legacy applications where the codebase is difficult to modify and allows for improvements
|
* the codebase is difficult to modify and allows for improvements in the application's networking
|
||||||
* in the application's networking capabilities.
|
* capabilities.
|
||||||
*
|
*
|
||||||
* In this example, we will the ({@link ServiceAmbassador}) class represents the ambassador while the
|
* <p>In this example, we will the ({@link ServiceAmbassador}) class represents the ambassador while
|
||||||
|
* the
|
||||||
* ({@link RemoteService}) class represents a remote application.
|
* ({@link RemoteService}) class represents a remote application.
|
||||||
*
|
|
||||||
*/
|
*/
|
||||||
public class App {
|
public class App {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Entry point
|
* Entry point.
|
||||||
*/
|
*/
|
||||||
public static void main(String[] args) {
|
public static void main(String[] args) {
|
||||||
Client host1 = new Client();
|
var host1 = new Client();
|
||||||
Client host2 = new Client();
|
var host2 = new Client();
|
||||||
host1.useService(12);
|
host1.useService(12);
|
||||||
host2.useService(73);
|
host2.useService(73);
|
||||||
}
|
}
|
||||||
|
@ -1,6 +1,6 @@
|
|||||||
/**
|
/*
|
||||||
* The MIT License
|
* The MIT License
|
||||||
* Copyright (c) 2014-2016 Ilkka Seppälä
|
* Copyright © 2014-2019 Ilkka Seppälä
|
||||||
*
|
*
|
||||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||||
* of this software and associated documentation files (the "Software"), to deal
|
* of this software and associated documentation files (the "Software"), to deal
|
||||||
@ -20,14 +20,14 @@
|
|||||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||||
* THE SOFTWARE.
|
* THE SOFTWARE.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
package com.iluwatar.ambassador;
|
package com.iluwatar.ambassador;
|
||||||
|
|
||||||
|
import org.slf4j.Logger;
|
||||||
import org.slf4j.LoggerFactory;
|
import org.slf4j.LoggerFactory;
|
||||||
|
|
||||||
import org.slf4j.Logger;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* A simple Client
|
* A simple Client.
|
||||||
*/
|
*/
|
||||||
public class Client {
|
public class Client {
|
||||||
|
|
||||||
@ -35,7 +35,7 @@ public class Client {
|
|||||||
private final ServiceAmbassador serviceAmbassador = new ServiceAmbassador();
|
private final ServiceAmbassador serviceAmbassador = new ServiceAmbassador();
|
||||||
|
|
||||||
long useService(int value) {
|
long useService(int value) {
|
||||||
long result = serviceAmbassador.doRemoteFunction(value);
|
var result = serviceAmbassador.doRemoteFunction(value);
|
||||||
LOGGER.info("Service result: " + result);
|
LOGGER.info("Service result: " + result);
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
@ -1,6 +1,6 @@
|
|||||||
/**
|
/*
|
||||||
* The MIT License
|
* The MIT License
|
||||||
* Copyright (c) 2014-2016 Ilkka Seppälä
|
* Copyright © 2014-2019 Ilkka Seppälä
|
||||||
*
|
*
|
||||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||||
* of this software and associated documentation files (the "Software"), to deal
|
* of this software and associated documentation files (the "Software"), to deal
|
||||||
@ -20,19 +20,20 @@
|
|||||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||||
* THE SOFTWARE.
|
* THE SOFTWARE.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
package com.iluwatar.ambassador;
|
package com.iluwatar.ambassador;
|
||||||
|
|
||||||
|
import static java.lang.Thread.sleep;
|
||||||
|
|
||||||
import com.iluwatar.ambassador.util.RandomProvider;
|
import com.iluwatar.ambassador.util.RandomProvider;
|
||||||
import org.slf4j.Logger;
|
import org.slf4j.Logger;
|
||||||
import org.slf4j.LoggerFactory;
|
import org.slf4j.LoggerFactory;
|
||||||
|
|
||||||
import static java.lang.Thread.sleep;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* A remote legacy application represented by a Singleton implementation.
|
* A remote legacy application represented by a Singleton implementation.
|
||||||
*/
|
*/
|
||||||
public class RemoteService implements RemoteServiceInterface {
|
public class RemoteService implements RemoteServiceInterface {
|
||||||
static final int THRESHOLD = 200;
|
private static final int THRESHOLD = 200;
|
||||||
private static final Logger LOGGER = LoggerFactory.getLogger(RemoteService.class);
|
private static final Logger LOGGER = LoggerFactory.getLogger(RemoteService.class);
|
||||||
private static RemoteService service = null;
|
private static RemoteService service = null;
|
||||||
private final RandomProvider randomProvider;
|
private final RandomProvider randomProvider;
|
||||||
@ -49,14 +50,16 @@ public class RemoteService implements RemoteServiceInterface {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* This constuctor is used for testing purposes only.
|
* This constructor is used for testing purposes only.
|
||||||
*/
|
*/
|
||||||
RemoteService(RandomProvider randomProvider) {
|
RemoteService(RandomProvider randomProvider) {
|
||||||
this.randomProvider = randomProvider;
|
this.randomProvider = randomProvider;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Remote function takes a value and multiplies it by 10 taking a random amount of time.
|
* Remote function takes a value and multiplies it by 10 taking a random amount of time. Will
|
||||||
* Will sometimes return -1. This imitates connectivity issues a client might have to account for.
|
* sometimes return -1. This imitates connectivity issues a client might have to account for.
|
||||||
|
*
|
||||||
* @param value integer value to be multiplied.
|
* @param value integer value to be multiplied.
|
||||||
* @return if waitTime is less than {@link RemoteService#THRESHOLD}, it returns value * 10,
|
* @return if waitTime is less than {@link RemoteService#THRESHOLD}, it returns value * 10,
|
||||||
* otherwise {@link RemoteServiceInterface#FAILURE}.
|
* otherwise {@link RemoteServiceInterface#FAILURE}.
|
||||||
|
@ -1,6 +1,6 @@
|
|||||||
/**
|
/*
|
||||||
* The MIT License
|
* The MIT License
|
||||||
* Copyright (c) 2014-2016 Ilkka Seppälä
|
* Copyright © 2014-2019 Ilkka Seppälä
|
||||||
*
|
*
|
||||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||||
* of this software and associated documentation files (the "Software"), to deal
|
* of this software and associated documentation files (the "Software"), to deal
|
||||||
@ -20,6 +20,7 @@
|
|||||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||||
* THE SOFTWARE.
|
* THE SOFTWARE.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
package com.iluwatar.ambassador;
|
package com.iluwatar.ambassador;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -1,6 +1,6 @@
|
|||||||
/**
|
/*
|
||||||
* The MIT License
|
* The MIT License
|
||||||
* Copyright (c) 2014-2016 Ilkka Seppälä
|
* Copyright © 2014-2019 Ilkka Seppälä
|
||||||
*
|
*
|
||||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||||
* of this software and associated documentation files (the "Software"), to deal
|
* of this software and associated documentation files (the "Software"), to deal
|
||||||
@ -20,19 +20,18 @@
|
|||||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||||
* THE SOFTWARE.
|
* THE SOFTWARE.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
package com.iluwatar.ambassador;
|
package com.iluwatar.ambassador;
|
||||||
|
|
||||||
|
import static java.lang.Thread.sleep;
|
||||||
|
|
||||||
import org.slf4j.Logger;
|
import org.slf4j.Logger;
|
||||||
import org.slf4j.LoggerFactory;
|
import org.slf4j.LoggerFactory;
|
||||||
|
|
||||||
import static java.lang.Thread.sleep;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
*
|
|
||||||
* ServiceAmbassador provides an interface for a ({@link Client}) to access ({@link RemoteService}).
|
* ServiceAmbassador provides an interface for a ({@link Client}) to access ({@link RemoteService}).
|
||||||
* The interface adds logging, latency testing and usage of the service in a safe way that will not
|
* The interface adds logging, latency testing and usage of the service in a safe way that will not
|
||||||
* add stress to the remote service when connectivity issues occur.
|
* add stress to the remote service when connectivity issues occur.
|
||||||
*
|
|
||||||
*/
|
*/
|
||||||
public class ServiceAmbassador implements RemoteServiceInterface {
|
public class ServiceAmbassador implements RemoteServiceInterface {
|
||||||
|
|
||||||
@ -40,7 +39,8 @@ public class ServiceAmbassador implements RemoteServiceInterface {
|
|||||||
private static final int RETRIES = 3;
|
private static final int RETRIES = 3;
|
||||||
private static final int DELAY_MS = 3000;
|
private static final int DELAY_MS = 3000;
|
||||||
|
|
||||||
ServiceAmbassador() {}
|
ServiceAmbassador() {
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public long doRemoteFunction(int value) {
|
public long doRemoteFunction(int value) {
|
||||||
@ -48,21 +48,19 @@ public class ServiceAmbassador implements RemoteServiceInterface {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private long checkLatency(int value) {
|
private long checkLatency(int value) {
|
||||||
long startTime = System.currentTimeMillis();
|
var startTime = System.currentTimeMillis();
|
||||||
long result = RemoteService.getRemoteService().doRemoteFunction(value);
|
var result = RemoteService.getRemoteService().doRemoteFunction(value);
|
||||||
long timeTaken = System.currentTimeMillis() - startTime;
|
var timeTaken = System.currentTimeMillis() - startTime;
|
||||||
|
|
||||||
LOGGER.info("Time taken (ms): " + timeTaken);
|
LOGGER.info("Time taken (ms): " + timeTaken);
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
private long safeCall(int value) {
|
private long safeCall(int value) {
|
||||||
|
var retries = 0;
|
||||||
int retries = 0;
|
var result = (long) FAILURE;
|
||||||
long result = FAILURE;
|
|
||||||
|
|
||||||
for (int i = 0; i < RETRIES; i++) {
|
for (int i = 0; i < RETRIES; i++) {
|
||||||
|
|
||||||
if (retries >= RETRIES) {
|
if (retries >= RETRIES) {
|
||||||
return FAILURE;
|
return FAILURE;
|
||||||
}
|
}
|
||||||
|
@ -1,6 +1,6 @@
|
|||||||
/**
|
/*
|
||||||
* The MIT License
|
* The MIT License
|
||||||
* Copyright (c) 2014 Ilkka Seppälä
|
* Copyright © 2014-2019 Ilkka Seppälä
|
||||||
*
|
*
|
||||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||||
* of this software and associated documentation files (the "Software"), to deal
|
* of this software and associated documentation files (the "Software"), to deal
|
||||||
@ -20,6 +20,7 @@
|
|||||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||||
* THE SOFTWARE.
|
* THE SOFTWARE.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
package com.iluwatar.ambassador.util;
|
package com.iluwatar.ambassador.util;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -1,6 +1,6 @@
|
|||||||
/**
|
/*
|
||||||
* The MIT License
|
* The MIT License
|
||||||
* Copyright (c) 2014-2016 Ilkka Seppälä
|
* Copyright © 2014-2019 Ilkka Seppälä
|
||||||
*
|
*
|
||||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||||
* of this software and associated documentation files (the "Software"), to deal
|
* of this software and associated documentation files (the "Software"), to deal
|
||||||
@ -20,6 +20,7 @@
|
|||||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||||
* THE SOFTWARE.
|
* THE SOFTWARE.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
package com.iluwatar.ambassador;
|
package com.iluwatar.ambassador;
|
||||||
|
|
||||||
import org.junit.jupiter.api.Test;
|
import org.junit.jupiter.api.Test;
|
||||||
@ -27,10 +28,10 @@ import org.junit.jupiter.api.Test;
|
|||||||
/**
|
/**
|
||||||
* Application test
|
* Application test
|
||||||
*/
|
*/
|
||||||
public class AppTest {
|
class AppTest {
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void test() {
|
void test() {
|
||||||
App.main(new String[]{});
|
App.main(new String[]{});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,6 +1,6 @@
|
|||||||
/**
|
/*
|
||||||
* The MIT License
|
* The MIT License
|
||||||
* Copyright (c) 2014-2016 Ilkka Seppälä
|
* Copyright © 2014-2019 Ilkka Seppälä
|
||||||
*
|
*
|
||||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||||
* of this software and associated documentation files (the "Software"), to deal
|
* of this software and associated documentation files (the "Software"), to deal
|
||||||
@ -20,6 +20,7 @@
|
|||||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||||
* THE SOFTWARE.
|
* THE SOFTWARE.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
package com.iluwatar.ambassador;
|
package com.iluwatar.ambassador;
|
||||||
|
|
||||||
import org.junit.jupiter.api.Test;
|
import org.junit.jupiter.api.Test;
|
||||||
@ -29,13 +30,12 @@ import static org.junit.jupiter.api.Assertions.assertTrue;
|
|||||||
/**
|
/**
|
||||||
* Test for {@link Client}
|
* Test for {@link Client}
|
||||||
*/
|
*/
|
||||||
public class ClientTest {
|
class ClientTest {
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void test() {
|
void test() {
|
||||||
|
|
||||||
Client client = new Client();
|
Client client = new Client();
|
||||||
long result = client.useService(10);
|
var result = client.useService(10);
|
||||||
|
|
||||||
assertTrue(result == 100 || result == RemoteService.FAILURE);
|
assertTrue(result == 100 || result == RemoteService.FAILURE);
|
||||||
}
|
}
|
||||||
|
@ -1,6 +1,6 @@
|
|||||||
/**
|
/*
|
||||||
* The MIT License
|
* The MIT License
|
||||||
* Copyright (c) 2014-2016 Ilkka Seppälä
|
* Copyright © 2014-2019 Ilkka Seppälä
|
||||||
*
|
*
|
||||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||||
* of this software and associated documentation files (the "Software"), to deal
|
* of this software and associated documentation files (the "Software"), to deal
|
||||||
@ -20,36 +20,34 @@
|
|||||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||||
* THE SOFTWARE.
|
* THE SOFTWARE.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
package com.iluwatar.ambassador;
|
package com.iluwatar.ambassador;
|
||||||
|
|
||||||
|
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||||
|
|
||||||
import com.iluwatar.ambassador.util.RandomProvider;
|
import com.iluwatar.ambassador.util.RandomProvider;
|
||||||
import org.junit.jupiter.api.Test;
|
import org.junit.jupiter.api.Test;
|
||||||
|
|
||||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
|
||||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Test for {@link RemoteService}
|
* Test for {@link RemoteService}
|
||||||
*/
|
*/
|
||||||
public class RemoteServiceTest {
|
class RemoteServiceTest {
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testFailedCall() {
|
void testFailedCall() {
|
||||||
RemoteService remoteService = new RemoteService(
|
var remoteService = new RemoteService(new StaticRandomProvider(0.21));
|
||||||
new StaticRandomProvider(0.21));
|
var result = remoteService.doRemoteFunction(10);
|
||||||
long result = remoteService.doRemoteFunction(10);
|
|
||||||
assertEquals(RemoteServiceInterface.FAILURE, result);
|
assertEquals(RemoteServiceInterface.FAILURE, result);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testSuccessfulCall() {
|
void testSuccessfulCall() {
|
||||||
RemoteService remoteService = new RemoteService(
|
var remoteService = new RemoteService(new StaticRandomProvider(0.2));
|
||||||
new StaticRandomProvider(0.2));
|
var result = remoteService.doRemoteFunction(10);
|
||||||
long result = remoteService.doRemoteFunction(10);
|
|
||||||
assertEquals(100, result);
|
assertEquals(100, result);
|
||||||
}
|
}
|
||||||
|
|
||||||
private class StaticRandomProvider implements RandomProvider {
|
private static class StaticRandomProvider implements RandomProvider {
|
||||||
private double value;
|
private double value;
|
||||||
|
|
||||||
StaticRandomProvider(double value) {
|
StaticRandomProvider(double value) {
|
||||||
|
@ -1,6 +1,6 @@
|
|||||||
/**
|
/*
|
||||||
* The MIT License
|
* The MIT License
|
||||||
* Copyright (c) 2014-2016 Ilkka Seppälä
|
* Copyright © 2014-2019 Ilkka Seppälä
|
||||||
*
|
*
|
||||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||||
* of this software and associated documentation files (the "Software"), to deal
|
* of this software and associated documentation files (the "Software"), to deal
|
||||||
@ -20,6 +20,7 @@
|
|||||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||||
* THE SOFTWARE.
|
* THE SOFTWARE.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
package com.iluwatar.ambassador;
|
package com.iluwatar.ambassador;
|
||||||
|
|
||||||
import org.junit.jupiter.api.Test;
|
import org.junit.jupiter.api.Test;
|
||||||
@ -29,10 +30,10 @@ import static org.junit.jupiter.api.Assertions.assertTrue;
|
|||||||
/**
|
/**
|
||||||
* Test for {@link ServiceAmbassador}
|
* Test for {@link ServiceAmbassador}
|
||||||
*/
|
*/
|
||||||
public class ServiceAmbassadorTest {
|
class ServiceAmbassadorTest {
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void test() {
|
void test() {
|
||||||
long result = new ServiceAmbassador().doRemoteFunction(10);
|
long result = new ServiceAmbassador().doRemoteFunction(10);
|
||||||
assertTrue(result == 100 || result == RemoteServiceInterface.FAILURE);
|
assertTrue(result == 100 || result == RemoteServiceInterface.FAILURE);
|
||||||
}
|
}
|
||||||
|
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user