Compare commits
2 Commits
all-contri
...
Issue#643
Author | SHA1 | Date | |
---|---|---|---|
0ed4747a71 | |||
ced1dfe937 |
@ -1,93 +0,0 @@
|
|||||||
{
|
|
||||||
"files": [
|
|
||||||
"README.md"
|
|
||||||
],
|
|
||||||
"imageSize": 100,
|
|
||||||
"commit": false,
|
|
||||||
"contributors": [
|
|
||||||
{
|
|
||||||
"login": "iluwatar",
|
|
||||||
"name": "Ilkka Seppälä",
|
|
||||||
"avatar_url": "https://avatars1.githubusercontent.com/u/582346?v=4",
|
|
||||||
"profile": "https://github.com/iluwatar",
|
|
||||||
"contributions": [
|
|
||||||
"projectManagement",
|
|
||||||
"maintenance",
|
|
||||||
"content"
|
|
||||||
]
|
|
||||||
},
|
|
||||||
{
|
|
||||||
"login": "amit1307",
|
|
||||||
"name": "amit1307",
|
|
||||||
"avatar_url": "https://avatars0.githubusercontent.com/u/23420222?v=4",
|
|
||||||
"profile": "https://github.com/amit1307",
|
|
||||||
"contributions": [
|
|
||||||
"code"
|
|
||||||
]
|
|
||||||
},
|
|
||||||
{
|
|
||||||
"login": "npathai",
|
|
||||||
"name": "Narendra Pathai",
|
|
||||||
"avatar_url": "https://avatars2.githubusercontent.com/u/1792515?v=4",
|
|
||||||
"profile": "https://github.com/npathai",
|
|
||||||
"contributions": [
|
|
||||||
"code",
|
|
||||||
"ideas",
|
|
||||||
"review"
|
|
||||||
]
|
|
||||||
},
|
|
||||||
{
|
|
||||||
"login": "fluxw42",
|
|
||||||
"name": "Jeroen Meulemeester",
|
|
||||||
"avatar_url": "https://avatars1.githubusercontent.com/u/1545460?v=4",
|
|
||||||
"profile": "https://github.com/fluxw42",
|
|
||||||
"contributions": [
|
|
||||||
"code"
|
|
||||||
]
|
|
||||||
},
|
|
||||||
{
|
|
||||||
"login": "mikulucky",
|
|
||||||
"name": "Joseph McCarthy",
|
|
||||||
"avatar_url": "https://avatars0.githubusercontent.com/u/4526195?v=4",
|
|
||||||
"profile": "http://www.joemccarthy.co.uk",
|
|
||||||
"contributions": [
|
|
||||||
"code"
|
|
||||||
]
|
|
||||||
},
|
|
||||||
{
|
|
||||||
"login": "thomasoss",
|
|
||||||
"name": "Thomas",
|
|
||||||
"avatar_url": "https://avatars1.githubusercontent.com/u/22516154?v=4",
|
|
||||||
"profile": "https://github.com/thomasoss",
|
|
||||||
"contributions": [
|
|
||||||
"code"
|
|
||||||
]
|
|
||||||
},
|
|
||||||
{
|
|
||||||
"login": "anuragagarwal561994",
|
|
||||||
"name": "Anurag Agarwal",
|
|
||||||
"avatar_url": "https://avatars1.githubusercontent.com/u/6075379?v=4",
|
|
||||||
"profile": "https://github.com/anuragagarwal561994",
|
|
||||||
"contributions": [
|
|
||||||
"code"
|
|
||||||
]
|
|
||||||
},
|
|
||||||
{
|
|
||||||
"login": "markusmo3",
|
|
||||||
"name": "Markus Moser",
|
|
||||||
"avatar_url": "https://avatars1.githubusercontent.com/u/3317416?v=4",
|
|
||||||
"profile": "https://markusmo3.github.io",
|
|
||||||
"contributions": [
|
|
||||||
"design",
|
|
||||||
"code",
|
|
||||||
"ideas"
|
|
||||||
]
|
|
||||||
}
|
|
||||||
],
|
|
||||||
"contributorsPerLine": 7,
|
|
||||||
"projectName": "java-design-patterns",
|
|
||||||
"projectOwner": "iluwatar",
|
|
||||||
"repoType": "github",
|
|
||||||
"repoHost": "https://github.com",
|
|
||||||
"skipCi": true
|
|
||||||
}
|
|
1
.github/FUNDING.yml
vendored
1
.github/FUNDING.yml
vendored
@ -1 +0,0 @@
|
|||||||
github: [iluwatar]
|
|
60
.github/workflows/maven.yml
vendored
60
.github/workflows/maven.yml
vendored
@ -1,60 +0,0 @@
|
|||||||
#
|
|
||||||
# The MIT License
|
|
||||||
# Copyright © 2014-2019 Ilkka Seppälä
|
|
||||||
#
|
|
||||||
# Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
||||||
# of this software and associated documentation files (the "Software"), to deal
|
|
||||||
# in the Software without restriction, including without limitation the rights
|
|
||||||
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
||||||
# copies of the Software, and to permit persons to whom the Software is
|
|
||||||
# furnished to do so, subject to the following conditions:
|
|
||||||
#
|
|
||||||
# The above copyright notice and this permission notice shall be included in
|
|
||||||
# all copies or substantial portions of the Software.
|
|
||||||
#
|
|
||||||
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
||||||
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
||||||
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
||||||
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
||||||
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
||||||
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
||||||
# THE SOFTWARE.
|
|
||||||
#
|
|
||||||
|
|
||||||
# This workflow will build a Java project with Maven
|
|
||||||
# For more information see: https://help.github.com/actions/language-and-framework-guides/building-and-testing-java-with-maven
|
|
||||||
|
|
||||||
name: Java CI with Maven
|
|
||||||
|
|
||||||
on:
|
|
||||||
push:
|
|
||||||
branches: [ master ]
|
|
||||||
pull_request:
|
|
||||||
branches: [ master ]
|
|
||||||
|
|
||||||
jobs:
|
|
||||||
build:
|
|
||||||
|
|
||||||
runs-on: ubuntu-18.04
|
|
||||||
|
|
||||||
steps:
|
|
||||||
- uses: actions/checkout@v2
|
|
||||||
- name: Set up JDK 11
|
|
||||||
uses: actions/setup-java@v1
|
|
||||||
with:
|
|
||||||
java-version: 11
|
|
||||||
# Some tests need screen access
|
|
||||||
- name: Install xvfb
|
|
||||||
run: sudo apt-get install xvfb
|
|
||||||
# SonarQube scan does not work for forked repositories
|
|
||||||
# See https://jira.sonarsource.com/browse/MMF-1371
|
|
||||||
- name: Build with Maven
|
|
||||||
if: github.ref != 'refs/heads/master'
|
|
||||||
run: xvfb-run mvn clean verify
|
|
||||||
- name: Build with Maven and run SonarQube analysis
|
|
||||||
if: github.ref == 'refs/heads/master'
|
|
||||||
run: xvfb-run mvn clean verify org.sonarsource.scanner.maven:sonar-maven-plugin:sonar
|
|
||||||
env:
|
|
||||||
# These two env variables are needed for sonar analysis
|
|
||||||
GITHUB_TOKEN: ${{ secrets.REPOSITORY_ACCESS_TOKEN }}
|
|
||||||
SONAR_TOKEN: ${{ secrets.SONAR_TOKEN }}
|
|
1
.gitignore
vendored
1
.gitignore
vendored
@ -16,4 +16,5 @@ datanucleus.log
|
|||||||
/bin/
|
/bin/
|
||||||
/bin/
|
/bin/
|
||||||
*.log
|
*.log
|
||||||
|
data-mapper/src/main/resources/log4j.xml
|
||||||
event-sourcing/Journal.json
|
event-sourcing/Journal.json
|
||||||
|
39
.travis.yml
Normal file
39
.travis.yml
Normal file
@ -0,0 +1,39 @@
|
|||||||
|
language: java
|
||||||
|
jdk:
|
||||||
|
- oraclejdk8
|
||||||
|
|
||||||
|
env:
|
||||||
|
global:
|
||||||
|
- GH_REF: github.com/iluwatar/java-design-patterns.git
|
||||||
|
- 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"
|
||||||
|
install:
|
||||||
|
- mvn install -DskipTests=true -Dmaven.javadoc.skip=true -B -V -e
|
||||||
|
|
||||||
|
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-ghpages.sh
|
||||||
|
|
||||||
|
# use latest java version available instead of travis default
|
||||||
|
addons:
|
||||||
|
apt:
|
||||||
|
packages:
|
||||||
|
- oracle-java8-installer
|
||||||
|
|
||||||
|
notifications:
|
||||||
|
email:
|
||||||
|
- iluwatar@gmail.com
|
||||||
|
webhooks:
|
||||||
|
urls:
|
||||||
|
- https://webhooks.gitter.im/e/3319623945358a093a6f
|
||||||
|
on_success: change # options: [always|never|change] default: always
|
||||||
|
on_failure: always # options: [always|never|change] default: always
|
||||||
|
on_start: never # options: [always|never|change] default: always
|
||||||
|
|
||||||
|
sudo: required
|
13
CODE_COVERAGE.md
Normal file
13
CODE_COVERAGE.md
Normal file
@ -0,0 +1,13 @@
|
|||||||
|
# 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
|
||||||
|
* busniess-delegate/target/site/jacoco/index.html
|
||||||
|
|
||||||
|
|
@ -1,6 +1,6 @@
|
|||||||
The MIT License (MIT)
|
The MIT License (MIT)
|
||||||
|
|
||||||
Copyright (c) 2014-2020 Ilkka Seppälä
|
Copyright (c) 2014-2016 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
|
||||||
|
64
README.md
64
README.md
@ -4,86 +4,44 @@
|
|||||||
|
|
||||||
# Design patterns implemented in Java
|
# Design patterns implemented in Java
|
||||||
|
|
||||||

|
[](https://travis-ci.org/iluwatar/java-design-patterns)
|
||||||
[](https://raw.githubusercontent.com/iluwatar/java-design-patterns/master/LICENSE.md)
|
[](https://raw.githubusercontent.com/iluwatar/java-design-patterns/master/LICENSE.md)
|
||||||
[](https://gitter.im/iluwatar/java-design-patterns?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge&utm_content=badge)
|
[](https://gitter.im/iluwatar/java-design-patterns?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge&utm_content=badge)
|
||||||
[](https://sonarcloud.io/dashboard?id=iluwatar_java-design-patterns)
|
[](https://sonarcloud.io/dashboard/index/com.iluwatar%3Ajava-design-patterns)
|
||||||
<!-- ALL-CONTRIBUTORS-BADGE:START - Do not remove or modify this section -->
|
|
||||||
[](#contributors-)
|
|
||||||
<!-- ALL-CONTRIBUTORS-BADGE:END -->
|
|
||||||
|
|
||||||
# Introduction
|
# Introduction
|
||||||
|
|
||||||
Design patterns are the best formalized practices a programmer can use to
|
Design patterns are formalized best practices that the programmer can use to
|
||||||
solve common problems when designing an application or system.
|
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 that cause major
|
Reusing design patterns helps to prevent subtle issues that can 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
|
||||||
[Software Design Principles](https://java-design-patterns.com/principles/).
|
Programming/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
|
||||||
patterns should only be introduced when they are needed for practical
|
patterns should only be introduced when they are needed for practical
|
||||||
extensibility.
|
extensibility.
|
||||||
|
|
||||||
Once you are familiar with these concepts you can start drilling down into the
|
Once you are familiar with these concepts you can start drilling down into
|
||||||
[available design patterns](https://java-design-patterns.com/patterns/) by any
|
patterns by any of the following approaches
|
||||||
of the following approaches
|
|
||||||
|
|
||||||
- Search for a specific pattern by name. Can't find one? Please report a new pattern [here](https://github.com/iluwatar/java-design-patterns/issues).
|
- Using difficulty tags, `Difficulty-Beginner`, `Difficulty-Intermediate` & `Difficulty-Expert`.
|
||||||
- Using tags such as `Performance`, `Gang of Four` or `Data access`.
|
- 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).
|
||||||
|
|
||||||
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
|
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).
|
||||||
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
|
||||||
|
|
||||||
This project is licensed under the terms of the MIT license.
|
This project is licensed under the terms of the MIT license.
|
||||||
|
|
||||||
# Contributors
|
|
||||||
|
|
||||||
<!-- ALL-CONTRIBUTORS-LIST:START - Do not remove or modify this section -->
|
|
||||||
<!-- prettier-ignore-start -->
|
|
||||||
<!-- markdownlint-disable -->
|
|
||||||
<table>
|
|
||||||
<tr>
|
|
||||||
<td align="center"><a href="https://github.com/iluwatar"><img src="https://avatars1.githubusercontent.com/u/582346?v=4" width="100px;" alt=""/><br /><sub><b>Ilkka Seppälä</b></sub></a><br /><a href="#projectManagement-iluwatar" title="Project Management">📆</a> <a href="#maintenance-iluwatar" title="Maintenance">🚧</a> <a href="#content-iluwatar" title="Content">🖋</a></td>
|
|
||||||
<td align="center"><a href="https://github.com/amit1307"><img src="https://avatars0.githubusercontent.com/u/23420222?v=4" width="100px;" alt=""/><br /><sub><b>amit1307</b></sub></a><br /><a href="https://github.com/iluwatar/java-design-patterns/commits?author=amit1307" title="Code">💻</a></td>
|
|
||||||
<td align="center"><a href="https://github.com/npathai"><img src="https://avatars2.githubusercontent.com/u/1792515?v=4" width="100px;" alt=""/><br /><sub><b>Narendra Pathai</b></sub></a><br /><a href="https://github.com/iluwatar/java-design-patterns/commits?author=npathai" title="Code">💻</a> <a href="#ideas-npathai" title="Ideas, Planning, & Feedback">🤔</a> <a href="https://github.com/iluwatar/java-design-patterns/pulls?q=is%3Apr+reviewed-by%3Anpathai" title="Reviewed Pull Requests">👀</a></td>
|
|
||||||
<td align="center"><a href="https://github.com/fluxw42"><img src="https://avatars1.githubusercontent.com/u/1545460?v=4" width="100px;" alt=""/><br /><sub><b>Jeroen Meulemeester</b></sub></a><br /><a href="https://github.com/iluwatar/java-design-patterns/commits?author=fluxw42" title="Code">💻</a></td>
|
|
||||||
<td align="center"><a href="http://www.joemccarthy.co.uk"><img src="https://avatars0.githubusercontent.com/u/4526195?v=4" width="100px;" alt=""/><br /><sub><b>Joseph McCarthy</b></sub></a><br /><a href="https://github.com/iluwatar/java-design-patterns/commits?author=mikulucky" title="Code">💻</a></td>
|
|
||||||
<td align="center"><a href="https://github.com/thomasoss"><img src="https://avatars1.githubusercontent.com/u/22516154?v=4" width="100px;" alt=""/><br /><sub><b>Thomas</b></sub></a><br /><a href="https://github.com/iluwatar/java-design-patterns/commits?author=thomasoss" title="Code">💻</a></td>
|
|
||||||
<td align="center"><a href="https://github.com/anuragagarwal561994"><img src="https://avatars1.githubusercontent.com/u/6075379?v=4" width="100px;" alt=""/><br /><sub><b>Anurag Agarwal</b></sub></a><br /><a href="https://github.com/iluwatar/java-design-patterns/commits?author=anuragagarwal561994" title="Code">💻</a></td>
|
|
||||||
</tr>
|
|
||||||
<tr>
|
|
||||||
<td align="center"><a href="https://markusmo3.github.io"><img src="https://avatars1.githubusercontent.com/u/3317416?v=4" width="100px;" alt=""/><br /><sub><b>Markus Moser</b></sub></a><br /><a href="#design-markusmo3" title="Design">🎨</a> <a href="https://github.com/iluwatar/java-design-patterns/commits?author=markusmo3" title="Code">💻</a> <a href="#ideas-markusmo3" title="Ideas, Planning, & Feedback">🤔</a></td>
|
|
||||||
</tr>
|
|
||||||
</table>
|
|
||||||
|
|
||||||
<!-- markdownlint-enable -->
|
|
||||||
<!-- prettier-ignore-end -->
|
|
||||||
<!-- ALL-CONTRIBUTORS-LIST:END -->
|
|
||||||
|
|
||||||
This project follows the [all-contributors](https://github.com/all-contributors/all-contributors) specification. Contributions of any kind welcome!
|
|
||||||
|
@ -5,13 +5,13 @@ folder: abstract-document
|
|||||||
permalink: /patterns/abstract-document/
|
permalink: /patterns/abstract-document/
|
||||||
categories: Structural
|
categories: Structural
|
||||||
tags:
|
tags:
|
||||||
- Extensibility
|
- Java
|
||||||
|
- Difficulty-Intermediate
|
||||||
---
|
---
|
||||||
|
|
||||||
## Intent
|
## Intent
|
||||||
Achieve flexibility of untyped languages and keep the type-safety
|
Achieve flexibility of untyped languages and keep the type-safety
|
||||||
|
|
||||||
## Class diagram
|
|
||||||

|

|
||||||
|
|
||||||
|
|
||||||
@ -26,4 +26,4 @@ Use the Abstract Document Pattern when
|
|||||||
## Credits
|
## Credits
|
||||||
|
|
||||||
* [Wikipedia: Abstract Document Pattern](https://en.wikipedia.org/wiki/Abstract_Document_Pattern)
|
* [Wikipedia: Abstract Document Pattern](https://en.wikipedia.org/wiki/Abstract_Document_Pattern)
|
||||||
* [Martin Fowler: Dealing with properties](http://martinfowler.com/apsupp/properties.pdf)
|
* [Martin Fowler: Dealing with properties](http://martinfowler.com/apsupp/properties.pdf)
|
@ -1,65 +0,0 @@
|
|||||||
@startuml
|
|
||||||
package com.iluwatar.abstractdocument.domain.enums {
|
|
||||||
enum Property {
|
|
||||||
+ MODEL {static}
|
|
||||||
+ PARTS {static}
|
|
||||||
+ PRICE {static}
|
|
||||||
+ TYPE {static}
|
|
||||||
+ valueOf(name : String) : Property {static}
|
|
||||||
+ values() : Property[] {static}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
package com.iluwatar.abstractdocument.domain {
|
|
||||||
class Car {
|
|
||||||
+ Car(properties : Map<String, Object>)
|
|
||||||
}
|
|
||||||
interface HasModel {
|
|
||||||
+ getModel() : Optional<String>
|
|
||||||
}
|
|
||||||
interface HasParts {
|
|
||||||
+ getParts() : Stream<Part>
|
|
||||||
}
|
|
||||||
interface HasPrice {
|
|
||||||
+ getPrice() : Optional<Number>
|
|
||||||
}
|
|
||||||
interface HasType {
|
|
||||||
+ getType() : Optional<String>
|
|
||||||
}
|
|
||||||
class Part {
|
|
||||||
+ Part(properties : Map<String, Object>)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
package com.iluwatar.abstractdocument {
|
|
||||||
abstract class AbstractDocument {
|
|
||||||
- properties : Map<String, Object>
|
|
||||||
# AbstractDocument(properties : Map<String, Object>)
|
|
||||||
+ children(key : String, constructor : Function<Map<String, Object>, T>) : Stream<T>
|
|
||||||
+ get(key : String) : Object
|
|
||||||
+ put(key : String, value : Object)
|
|
||||||
+ toString() : String
|
|
||||||
}
|
|
||||||
class App {
|
|
||||||
- LOGGER : Logger {static}
|
|
||||||
+ App()
|
|
||||||
+ main(args : String[]) {static}
|
|
||||||
}
|
|
||||||
interface Document {
|
|
||||||
+ children(String, Function<Map<String, Object>, T>) : Stream<T> {abstract}
|
|
||||||
+ get(String) : Object {abstract}
|
|
||||||
+ put(String, Object) {abstract}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
AbstractDocument ..|> Document
|
|
||||||
Car ..|> HasModel
|
|
||||||
Car ..|> HasPrice
|
|
||||||
Car ..|> HasParts
|
|
||||||
Car --|> AbstractDocument
|
|
||||||
HasModel --|> Document
|
|
||||||
HasParts --|> Document
|
|
||||||
HasPrice --|> Document
|
|
||||||
HasType --|> Document
|
|
||||||
Part ..|> HasType
|
|
||||||
Part ..|> HasModel
|
|
||||||
Part ..|> HasPrice
|
|
||||||
Part --|> AbstractDocument
|
|
||||||
@enduml
|
|
@ -1,55 +1,47 @@
|
|||||||
<?xml version="1.0" encoding="UTF-8"?>
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
<!-- 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
|
The MIT License
|
||||||
without restriction, including without limitation the rights to use, copy,
|
Copyright (c) 2014-2016 Ilkka Seppälä
|
||||||
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,
|
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||||
subject to the following conditions: The above copyright notice and this
|
of this software and associated documentation files (the "Software"), to deal
|
||||||
permission notice shall be included in all copies or substantial portions
|
in the Software without restriction, including without limitation the rights
|
||||||
of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY
|
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||||
KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
copies of the Software, and to permit persons to whom the Software is
|
||||||
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
|
furnished to do so, subject to the following conditions:
|
||||||
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,
|
The above copyright notice and this permission notice shall be included in
|
||||||
ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
|
all copies or substantial portions of the Software.
|
||||||
DEALINGS IN THE SOFTWARE. -->
|
|
||||||
<project xmlns="http://maven.apache.org/POM/4.0.0"
|
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||||
<modelVersion>4.0.0</modelVersion>
|
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||||
<parent>
|
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||||
<artifactId>java-design-patterns</artifactId>
|
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||||
<groupId>com.iluwatar</groupId>
|
THE SOFTWARE.
|
||||||
<version>1.23.0-SNAPSHOT</version>
|
|
||||||
</parent>
|
-->
|
||||||
<artifactId>abstract-document</artifactId>
|
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||||
<dependencies>
|
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||||
<dependency>
|
<modelVersion>4.0.0</modelVersion>
|
||||||
<groupId>org.junit.jupiter</groupId>
|
<parent>
|
||||||
<artifactId>junit-jupiter-engine</artifactId>
|
<artifactId>java-design-patterns</artifactId>
|
||||||
<scope>test</scope>
|
<groupId>com.iluwatar</groupId>
|
||||||
</dependency>
|
<version>1.21.0-SNAPSHOT</version>
|
||||||
</dependencies>
|
</parent>
|
||||||
<build>
|
<artifactId>abstract-document</artifactId>
|
||||||
<plugins>
|
<dependencies>
|
||||||
<!-- Maven assembly plugin is invoked with default setting which we have
|
<dependency>
|
||||||
in parent pom and specifying the class having main method -->
|
<groupId>org.junit.jupiter</groupId>
|
||||||
<plugin>
|
<artifactId>junit-jupiter-api</artifactId>
|
||||||
<groupId>org.apache.maven.plugins</groupId>
|
<scope>test</scope>
|
||||||
<artifactId>maven-assembly-plugin</artifactId>
|
</dependency>
|
||||||
<executions>
|
<dependency>
|
||||||
<execution>
|
<groupId>org.junit.jupiter</groupId>
|
||||||
<configuration>
|
<artifactId>junit-jupiter-engine</artifactId>
|
||||||
<archive>
|
<scope>test</scope>
|
||||||
<manifest>
|
</dependency>
|
||||||
<mainClass>com.iluwatar.abstractdocument.App</mainClass>
|
</dependencies>
|
||||||
</manifest>
|
</project>
|
||||||
</archive>
|
|
||||||
</configuration>
|
|
||||||
</execution>
|
|
||||||
</executions>
|
|
||||||
</plugin>
|
|
||||||
</plugins>
|
|
||||||
</build>
|
|
||||||
</project>
|
|
@ -1,17 +1,17 @@
|
|||||||
/*
|
/**
|
||||||
* The MIT License
|
* The MIT License
|
||||||
* Copyright © 2014-2019 Ilkka Seppälä
|
* Copyright (c) 2014-2016 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,18 +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 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 {
|
||||||
|
|
||||||
@ -55,21 +54,17 @@ 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) {
|
||||||
return Stream.ofNullable(get(key))
|
Optional<List<Map<String, Object>>> any = Stream.of(get(key)).filter(el -> el != null)
|
||||||
.filter(Objects::nonNull)
|
.map(el -> (List<Map<String, Object>>) el).findAny();
|
||||||
.map(el -> (List<Map<String, Object>>) el)
|
return any.isPresent() ? any.get().stream().map(constructor) : Stream.empty();
|
||||||
.findAny()
|
|
||||||
.stream()
|
|
||||||
.flatMap(Collection::stream)
|
|
||||||
.map(constructor);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public String toString() {
|
public String toString() {
|
||||||
var builder = new StringBuilder();
|
StringBuilder builder = new StringBuilder();
|
||||||
builder.append(getClass().getName()).append("[");
|
builder.append(getClass().getName()).append("[");
|
||||||
properties.forEach((key, value) -> builder.append("[").append(key).append(" : ").append(value)
|
properties.entrySet()
|
||||||
.append("]"));
|
.forEach(e -> builder.append("[").append(e.getKey()).append(" : ").append(e.getValue()).append("]"));
|
||||||
builder.append("]");
|
builder.append("]");
|
||||||
return builder.toString();
|
return builder.toString();
|
||||||
}
|
}
|
||||||
|
@ -1,17 +1,17 @@
|
|||||||
/*
|
/**
|
||||||
* The MIT License
|
* The MIT License
|
||||||
* Copyright © 2014-2019 Ilkka Seppälä
|
* Copyright (c) 2014-2016 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,64 +20,67 @@
|
|||||||
* 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 com.iluwatar.abstractdocument.domain.Car;
|
import com.iluwatar.abstractdocument.domain.Car;
|
||||||
import com.iluwatar.abstractdocument.domain.enums.Property;
|
import com.iluwatar.abstractdocument.domain.HasModel;
|
||||||
import java.util.List;
|
import com.iluwatar.abstractdocument.domain.HasParts;
|
||||||
import java.util.Map;
|
import com.iluwatar.abstractdocument.domain.HasPrice;
|
||||||
|
import com.iluwatar.abstractdocument.domain.HasType;
|
||||||
import org.slf4j.Logger;
|
import org.slf4j.Logger;
|
||||||
import org.slf4j.LoggerFactory;
|
import org.slf4j.LoggerFactory;
|
||||||
|
|
||||||
|
import java.util.Arrays;
|
||||||
|
import java.util.HashMap;
|
||||||
|
import java.util.Map;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* The Abstract Document pattern enables handling additional, non-static properties. This pattern
|
* The Abstract Document pattern enables handling additional, non-static
|
||||||
* uses concept of traits to enable type safety and separate properties of different classes into
|
* properties. This pattern uses concept of traits to enable type safety and
|
||||||
* set of interfaces.
|
* separate properties of different classes into set of interfaces.
|
||||||
*
|
* <p>
|
||||||
* <p>In Abstract Document pattern,({@link AbstractDocument}) fully implements {@link Document})
|
* <p>
|
||||||
* interface. Traits are then defined to enable access to properties in usual, static way.
|
* In Abstract Document pattern,({@link AbstractDocument}) fully implements
|
||||||
|
* {@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");
|
||||||
|
|
||||||
var wheelProperties = Map.of(
|
Map<String, Object> carProperties = new HashMap<>();
|
||||||
Property.TYPE.toString(), "wheel",
|
carProperties.put(HasModel.PROPERTY, "300SL");
|
||||||
Property.MODEL.toString(), "15C",
|
carProperties.put(HasPrice.PROPERTY, 10000L);
|
||||||
Property.PRICE.toString(), 100L);
|
|
||||||
|
|
||||||
var doorProperties = Map.of(
|
Map<String, Object> wheelProperties = new HashMap<>();
|
||||||
Property.TYPE.toString(), "door",
|
wheelProperties.put(HasType.PROPERTY, "wheel");
|
||||||
Property.MODEL.toString(), "Lambo",
|
wheelProperties.put(HasModel.PROPERTY, "15C");
|
||||||
Property.PRICE.toString(), 300L);
|
wheelProperties.put(HasPrice.PROPERTY, 100L);
|
||||||
|
|
||||||
var carProperties = Map.of(
|
Map<String, Object> doorProperties = new HashMap<>();
|
||||||
Property.MODEL.toString(), "300SL",
|
doorProperties.put(HasType.PROPERTY, "door");
|
||||||
Property.PRICE.toString(), 10000L,
|
doorProperties.put(HasModel.PROPERTY, "Lambo");
|
||||||
Property.PARTS.toString(), List.of(wheelProperties, doorProperties));
|
doorProperties.put(HasPrice.PROPERTY, 300L);
|
||||||
|
|
||||||
var car = new Car(carProperties);
|
carProperties.put(HasParts.PROPERTY, Arrays.asList(wheelProperties, doorProperties));
|
||||||
|
|
||||||
|
Car car = new Car(carProperties);
|
||||||
|
|
||||||
LOGGER.info("Here is our car:");
|
LOGGER.info("Here is our car:");
|
||||||
LOGGER.info("-> model: {}", car.getModel().orElseThrow());
|
LOGGER.info("-> model: {}", car.getModel().get());
|
||||||
LOGGER.info("-> price: {}", car.getPrice().orElseThrow());
|
LOGGER.info("-> price: {}", car.getPrice().get());
|
||||||
LOGGER.info("-> parts: ");
|
LOGGER.info("-> parts: ");
|
||||||
car.getParts().forEach(p -> LOGGER.info("\t{}/{}/{}",
|
car.getParts().forEach(p -> LOGGER.info("\t{}/{}/{}", p.getType().get(), p.getModel().get(), p.getPrice().get()));
|
||||||
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 © 2014-2019 Ilkka Seppälä
|
* Copyright (c) 2014-2016 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,7 +20,6 @@
|
|||||||
* 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;
|
||||||
@ -28,12 +27,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
|
||||||
@ -42,7 +41,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
|
||||||
@ -50,7 +49,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 © 2014-2019 Ilkka Seppälä
|
* Copyright (c) 2014-2016 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 © 2014-2019 Ilkka Seppälä
|
* Copyright (c) 2014-2016 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,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.abstractdocument.domain;
|
package com.iluwatar.abstractdocument.domain;
|
||||||
|
|
||||||
import com.iluwatar.abstractdocument.Document;
|
|
||||||
import com.iluwatar.abstractdocument.domain.enums.Property;
|
|
||||||
import java.util.Optional;
|
import java.util.Optional;
|
||||||
|
|
||||||
|
import com.iluwatar.abstractdocument.Document;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 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 {
|
||||||
|
|
||||||
|
String PROPERTY = "model";
|
||||||
|
|
||||||
default Optional<String> getModel() {
|
default Optional<String> getModel() {
|
||||||
return Optional.ofNullable((String) get(Property.MODEL.toString()));
|
return Optional.ofNullable((String) get(PROPERTY));
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -1,17 +1,17 @@
|
|||||||
/*
|
/**
|
||||||
* The MIT License
|
* The MIT License
|
||||||
* Copyright © 2014-2019 Ilkka Seppälä
|
* Copyright (c) 2014-2016 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,21 +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.abstractdocument.domain;
|
package com.iluwatar.abstractdocument.domain;
|
||||||
|
|
||||||
import com.iluwatar.abstractdocument.Document;
|
|
||||||
import com.iluwatar.abstractdocument.domain.enums.Property;
|
|
||||||
import java.util.stream.Stream;
|
import java.util.stream.Stream;
|
||||||
|
|
||||||
|
import com.iluwatar.abstractdocument.Document;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 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 {
|
||||||
|
|
||||||
|
String PROPERTY = "parts";
|
||||||
|
|
||||||
default Stream<Part> getParts() {
|
default Stream<Part> getParts() {
|
||||||
return children(Property.PARTS.toString(), Part::new);
|
return children(PROPERTY, Part::new);
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -1,17 +1,17 @@
|
|||||||
/*
|
/**
|
||||||
* The MIT License
|
* The MIT License
|
||||||
* Copyright © 2014-2019 Ilkka Seppälä
|
* Copyright (c) 2014-2016 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,21 +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.abstractdocument.domain;
|
package com.iluwatar.abstractdocument.domain;
|
||||||
|
|
||||||
import com.iluwatar.abstractdocument.Document;
|
|
||||||
import com.iluwatar.abstractdocument.domain.enums.Property;
|
|
||||||
import java.util.Optional;
|
import java.util.Optional;
|
||||||
|
|
||||||
|
import com.iluwatar.abstractdocument.Document;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 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 {
|
||||||
|
|
||||||
|
String PROPERTY = "price";
|
||||||
|
|
||||||
default Optional<Number> getPrice() {
|
default Optional<Number> getPrice() {
|
||||||
return Optional.ofNullable((Number) get(Property.PRICE.toString()));
|
return Optional.ofNullable((Number) get(PROPERTY));
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -1,17 +1,17 @@
|
|||||||
/*
|
/**
|
||||||
* The MIT License
|
* The MIT License
|
||||||
* Copyright © 2014-2019 Ilkka Seppälä
|
* Copyright (c) 2014-2016 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,21 +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.abstractdocument.domain;
|
package com.iluwatar.abstractdocument.domain;
|
||||||
|
|
||||||
import com.iluwatar.abstractdocument.Document;
|
import com.iluwatar.abstractdocument.Document;
|
||||||
import com.iluwatar.abstractdocument.domain.enums.Property;
|
|
||||||
import java.util.Optional;
|
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 {
|
||||||
|
|
||||||
|
String PROPERTY = "type";
|
||||||
|
|
||||||
default Optional<String> getType() {
|
default Optional<String> getType() {
|
||||||
return Optional.ofNullable((String) get(Property.TYPE.toString()));
|
return Optional.ofNullable((String) get(PROPERTY));
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -1,17 +1,17 @@
|
|||||||
/*
|
/**
|
||||||
* The MIT License
|
* The MIT License
|
||||||
* Copyright © 2014-2019 Ilkka Seppälä
|
* Copyright (c) 2014-2016 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,32 +0,0 @@
|
|||||||
/*
|
|
||||||
* The MIT License
|
|
||||||
* Copyright © 2014-2019 Ilkka Seppälä
|
|
||||||
*
|
|
||||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
||||||
* of this software and associated documentation files (the "Software"), to deal
|
|
||||||
* in the Software without restriction, including without limitation the rights
|
|
||||||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
||||||
* copies of the Software, and to permit persons to whom the Software is
|
|
||||||
* furnished to do so, subject to the following conditions:
|
|
||||||
*
|
|
||||||
* The above copyright notice and this permission notice shall be included in
|
|
||||||
* all copies or substantial portions of the Software.
|
|
||||||
*
|
|
||||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
||||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
||||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
||||||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
||||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
||||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
||||||
* THE SOFTWARE.
|
|
||||||
*/
|
|
||||||
|
|
||||||
package com.iluwatar.abstractdocument.domain.enums;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Enum To Describe Property type.
|
|
||||||
*/
|
|
||||||
public enum Property {
|
|
||||||
|
|
||||||
PARTS, TYPE, PRICE, MODEL
|
|
||||||
}
|
|
@ -1,26 +0,0 @@
|
|||||||
/*
|
|
||||||
* The MIT License
|
|
||||||
* Copyright © 2014-2019 Ilkka Seppälä
|
|
||||||
*
|
|
||||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
||||||
* of this software and associated documentation files (the "Software"), to deal
|
|
||||||
* in the Software without restriction, including without limitation the rights
|
|
||||||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
||||||
* copies of the Software, and to permit persons to whom the Software is
|
|
||||||
* furnished to do so, subject to the following conditions:
|
|
||||||
*
|
|
||||||
* The above copyright notice and this permission notice shall be included in
|
|
||||||
* all copies or substantial portions of the Software.
|
|
||||||
*
|
|
||||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
||||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
||||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
||||||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
||||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
||||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
||||||
* THE SOFTWARE.
|
|
||||||
*/
|
|
||||||
|
|
||||||
module com.iluwatar.abstractdocument {
|
|
||||||
requires org.slf4j;
|
|
||||||
}
|
|
@ -1,17 +1,17 @@
|
|||||||
/*
|
/**
|
||||||
* The MIT License
|
* The MIT License
|
||||||
* Copyright © 2014-2019 Ilkka Seppälä
|
* Copyright (c) 2014-2016 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 static org.junit.jupiter.api.Assertions.assertEquals;
|
import org.junit.jupiter.api.Test;
|
||||||
import static org.junit.jupiter.api.Assertions.assertNotNull;
|
|
||||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
|
||||||
|
|
||||||
|
import java.util.Arrays;
|
||||||
import java.util.HashMap;
|
import java.util.HashMap;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
import org.junit.jupiter.api.Test;
|
import java.util.stream.Stream;
|
||||||
|
|
||||||
|
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||||
|
import static org.junit.jupiter.api.Assertions.assertNotNull;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* AbstractDocument test class
|
* AbstractDocument test class
|
||||||
@ -57,28 +58,31 @@ public class AbstractDocumentTest {
|
|||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void shouldRetrieveChildren() {
|
public void shouldRetrieveChildren() {
|
||||||
var children = List.of(Map.of(), Map.of());
|
Map<String, Object> child1 = new HashMap<>();
|
||||||
|
Map<String, Object> child2 = new HashMap<>();
|
||||||
|
List<Map<String, Object>> children = Arrays.asList(child1, child2);
|
||||||
|
|
||||||
document.put(KEY, children);
|
document.put(KEY, children);
|
||||||
|
|
||||||
var childrenStream = document.children(KEY, DocumentImplementation::new);
|
Stream<DocumentImplementation> 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() {
|
||||||
var children = document.children(KEY, DocumentImplementation::new);
|
Stream<DocumentImplementation> 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() {
|
||||||
var props = Map.of(KEY, (Object) VALUE);
|
Map<String, Object> props = new HashMap<>();
|
||||||
var document = new DocumentImplementation(props);
|
props.put(KEY, VALUE);
|
||||||
assertTrue(document.toString().contains(KEY));
|
DocumentImplementation document = new DocumentImplementation(props);
|
||||||
assertTrue(document.toString().contains(VALUE));
|
assertNotNull(document.toString().contains(KEY));
|
||||||
|
assertNotNull(document.toString().contains(VALUE));
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -1,17 +1,17 @@
|
|||||||
/*
|
/**
|
||||||
* The MIT License
|
* The MIT License
|
||||||
* Copyright © 2014-2019 Ilkka Seppälä
|
* Copyright (c) 2014-2016 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,7 +20,6 @@
|
|||||||
* 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 © 2014-2019 Ilkka Seppälä
|
* Copyright (c) 2014-2016 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,18 +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.abstractdocument;
|
package com.iluwatar.abstractdocument;
|
||||||
|
|
||||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
|
||||||
|
|
||||||
import com.iluwatar.abstractdocument.domain.Car;
|
import com.iluwatar.abstractdocument.domain.Car;
|
||||||
|
import com.iluwatar.abstractdocument.domain.HasModel;
|
||||||
|
import com.iluwatar.abstractdocument.domain.HasParts;
|
||||||
|
import com.iluwatar.abstractdocument.domain.HasPrice;
|
||||||
|
import com.iluwatar.abstractdocument.domain.HasType;
|
||||||
import com.iluwatar.abstractdocument.domain.Part;
|
import com.iluwatar.abstractdocument.domain.Part;
|
||||||
import com.iluwatar.abstractdocument.domain.enums.Property;
|
|
||||||
import java.util.List;
|
|
||||||
import java.util.Map;
|
|
||||||
import org.junit.jupiter.api.Test;
|
import org.junit.jupiter.api.Test;
|
||||||
|
|
||||||
|
import java.util.Arrays;
|
||||||
|
import java.util.HashMap;
|
||||||
|
import java.util.Map;
|
||||||
|
|
||||||
|
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Test for Part and Car
|
* Test for Part and Car
|
||||||
*/
|
*/
|
||||||
@ -46,27 +50,27 @@ public class DomainTest {
|
|||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void shouldConstructPart() {
|
public void shouldConstructPart() {
|
||||||
var partProperties = Map.of(
|
Map<String, Object> partProperties = new HashMap<>();
|
||||||
Property.TYPE.toString(), TEST_PART_TYPE,
|
partProperties.put(HasType.PROPERTY, TEST_PART_TYPE);
|
||||||
Property.MODEL.toString(), TEST_PART_MODEL,
|
partProperties.put(HasModel.PROPERTY, TEST_PART_MODEL);
|
||||||
Property.PRICE.toString(), (Object) TEST_PART_PRICE
|
partProperties.put(HasPrice.PROPERTY, TEST_PART_PRICE);
|
||||||
);
|
Part part = new Part(partProperties);
|
||||||
var part = new Part(partProperties);
|
|
||||||
assertEquals(TEST_PART_TYPE, part.getType().orElseThrow());
|
assertEquals(TEST_PART_TYPE, part.getType().get());
|
||||||
assertEquals(TEST_PART_MODEL, part.getModel().orElseThrow());
|
assertEquals(TEST_PART_MODEL, part.getModel().get());
|
||||||
assertEquals(TEST_PART_PRICE, part.getPrice().orElseThrow());
|
assertEquals(TEST_PART_PRICE, part.getPrice().get());
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void shouldConstructCar() {
|
public void shouldConstructCar() {
|
||||||
var carProperties = Map.of(
|
Map<String, Object> carProperties = new HashMap<>();
|
||||||
Property.MODEL.toString(), TEST_CAR_MODEL,
|
carProperties.put(HasModel.PROPERTY, TEST_CAR_MODEL);
|
||||||
Property.PRICE.toString(), TEST_CAR_PRICE,
|
carProperties.put(HasPrice.PROPERTY, TEST_CAR_PRICE);
|
||||||
Property.PARTS.toString(), List.of(Map.of(), Map.of())
|
carProperties.put(HasParts.PROPERTY, Arrays.asList(new HashMap<>(), new HashMap<>()));
|
||||||
);
|
Car car = new Car(carProperties);
|
||||||
var car = new Car(carProperties);
|
|
||||||
assertEquals(TEST_CAR_MODEL, car.getModel().orElseThrow());
|
assertEquals(TEST_CAR_MODEL, car.getModel().get());
|
||||||
assertEquals(TEST_CAR_PRICE, car.getPrice().orElseThrow());
|
assertEquals(TEST_CAR_PRICE, car.getPrice().get());
|
||||||
assertEquals(2, car.getParts().count());
|
assertEquals(2, car.getParts().count());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -5,7 +5,9 @@ folder: abstract-factory
|
|||||||
permalink: /patterns/abstract-factory/
|
permalink: /patterns/abstract-factory/
|
||||||
categories: Creational
|
categories: Creational
|
||||||
tags:
|
tags:
|
||||||
- Gang of Four
|
- Java
|
||||||
|
- Gang Of Four
|
||||||
|
- Difficulty-Intermediate
|
||||||
---
|
---
|
||||||
|
|
||||||
## Also known as
|
## Also known as
|
||||||
@ -107,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
|
||||||
var factory = new ElfKingdomFactory();
|
KingdomFactory factory = new ElfKingdomFactory();
|
||||||
var castle = factory.createCastle();
|
Castle castle = factory.createCastle();
|
||||||
var king = factory.createKing();
|
King king = factory.createKing();
|
||||||
var army = factory.createArmy();
|
Army 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!
|
||||||
@ -141,7 +143,7 @@ public static class FactoryMaker {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public static void main(String[] args) {
|
public static void main(String[] args) {
|
||||||
var app = new App();
|
App app = new App();
|
||||||
|
|
||||||
LOGGER.info("Elf Kingdom");
|
LOGGER.info("Elf Kingdom");
|
||||||
app.createKingdom(FactoryMaker.makeFactory(KingdomType.ELF));
|
app.createKingdom(FactoryMaker.makeFactory(KingdomType.ELF));
|
||||||
@ -155,9 +157,6 @@ public static void main(String[] args) {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
## Class diagram
|
|
||||||

|
|
||||||
|
|
||||||
|
|
||||||
## Applicability
|
## Applicability
|
||||||
Use the Abstract Factory pattern when
|
Use the Abstract Factory pattern when
|
||||||
@ -170,25 +169,24 @@ 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
|
||||||
|
|
||||||
@ -198,5 +196,4 @@ Use the Abstract Factory pattern when
|
|||||||
|
|
||||||
## Credits
|
## Credits
|
||||||
|
|
||||||
* [Design Patterns: Elements of Reusable Object-Oriented Software](https://www.amazon.com/gp/product/0201633612/ref=as_li_tl?ie=UTF8&camp=1789&creative=9325&creativeASIN=0201633612&linkCode=as2&tag=javadesignpat-20&linkId=675d49790ce11db99d90bde47f1aeb59)
|
* [Design Patterns: Elements of Reusable Object-Oriented Software](http://www.amazon.com/Design-Patterns-Elements-Reusable-Object-Oriented/dp/0201633612)
|
||||||
* [Head First Design Patterns: A Brain-Friendly Guide](https://www.amazon.com/gp/product/0596007124/ref=as_li_tl?ie=UTF8&camp=1789&creative=9325&creativeASIN=0596007124&linkCode=as2&tag=javadesignpat-20&linkId=6b8b6eea86021af6c8e3cd3fc382cb5b)
|
|
||||||
|
Binary file not shown.
Before Width: | Height: | Size: 80 KiB |
@ -1,101 +0,0 @@
|
|||||||
@startuml
|
|
||||||
package com.iluwatar.abstractfactory {
|
|
||||||
class App {
|
|
||||||
- LOGGER : Logger {static}
|
|
||||||
- army : Army
|
|
||||||
- castle : Castle
|
|
||||||
- king : King
|
|
||||||
+ App()
|
|
||||||
+ createKingdom(factory : KingdomFactory)
|
|
||||||
+ getArmy() : Army
|
|
||||||
~ getArmy(factory : KingdomFactory) : Army
|
|
||||||
+ getCastle() : Castle
|
|
||||||
~ getCastle(factory : KingdomFactory) : Castle
|
|
||||||
+ getKing() : King
|
|
||||||
~ getKing(factory : KingdomFactory) : King
|
|
||||||
+ main(args : String[]) {static}
|
|
||||||
- setArmy(army : Army)
|
|
||||||
- setCastle(castle : Castle)
|
|
||||||
- setKing(king : King)
|
|
||||||
}
|
|
||||||
class FactoryMaker {
|
|
||||||
+ FactoryMaker()
|
|
||||||
+ makeFactory(type : KingdomType) : KingdomFactory {static}
|
|
||||||
}
|
|
||||||
enum KingdomType {
|
|
||||||
+ ELF {static}
|
|
||||||
+ ORC {static}
|
|
||||||
+ valueOf(name : String) : KingdomType {static}
|
|
||||||
+ values() : KingdomType[] {static}
|
|
||||||
}
|
|
||||||
interface Army {
|
|
||||||
+ getDescription() : String {abstract}
|
|
||||||
}
|
|
||||||
interface Castle {
|
|
||||||
+ getDescription() : String {abstract}
|
|
||||||
}
|
|
||||||
class ElfArmy {
|
|
||||||
~ DESCRIPTION : String {static}
|
|
||||||
+ ElfArmy()
|
|
||||||
+ getDescription() : String
|
|
||||||
}
|
|
||||||
class ElfCastle {
|
|
||||||
~ DESCRIPTION : String {static}
|
|
||||||
+ ElfCastle()
|
|
||||||
+ getDescription() : String
|
|
||||||
}
|
|
||||||
class ElfKing {
|
|
||||||
~ DESCRIPTION : String {static}
|
|
||||||
+ ElfKing()
|
|
||||||
+ getDescription() : String
|
|
||||||
}
|
|
||||||
class ElfKingdomFactory {
|
|
||||||
+ ElfKingdomFactory()
|
|
||||||
+ createArmy() : Army
|
|
||||||
+ createCastle() : Castle
|
|
||||||
+ createKing() : King
|
|
||||||
}
|
|
||||||
interface King {
|
|
||||||
+ getDescription() : String {abstract}
|
|
||||||
}
|
|
||||||
interface KingdomFactory {
|
|
||||||
+ createArmy() : Army {abstract}
|
|
||||||
+ createCastle() : Castle {abstract}
|
|
||||||
+ createKing() : King {abstract}
|
|
||||||
}
|
|
||||||
class OrcArmy {
|
|
||||||
~ DESCRIPTION : String {static}
|
|
||||||
+ OrcArmy()
|
|
||||||
+ getDescription() : String
|
|
||||||
}
|
|
||||||
class OrcCastle {
|
|
||||||
~ DESCRIPTION : String {static}
|
|
||||||
+ OrcCastle()
|
|
||||||
+ getDescription() : String
|
|
||||||
}
|
|
||||||
class OrcKing {
|
|
||||||
~ DESCRIPTION : String {static}
|
|
||||||
+ OrcKing()
|
|
||||||
+ getDescription() : String
|
|
||||||
}
|
|
||||||
class OrcKingdomFactory {
|
|
||||||
+ OrcKingdomFactory()
|
|
||||||
+ createArmy() : Army
|
|
||||||
+ createCastle() : Castle
|
|
||||||
+ createKing() : King
|
|
||||||
}
|
|
||||||
}
|
|
||||||
KingdomType ..+ FactoryMaker
|
|
||||||
App --> "-castle" Castle
|
|
||||||
FactoryMaker ..+ App
|
|
||||||
App --> "-king" King
|
|
||||||
App --> "-army" Army
|
|
||||||
ElfArmy ..|> Army
|
|
||||||
ElfCastle ..|> Castle
|
|
||||||
ElfKing ..|> King
|
|
||||||
ElfKingdomFactory ..|> KingdomFactory
|
|
||||||
OrcArmy ..|> Army
|
|
||||||
OrcCastle ..|> Castle
|
|
||||||
OrcKing ..|> King
|
|
||||||
OrcKingdomFactory ..|> KingdomFactory
|
|
||||||
@enduml
|
|
BIN
abstract-factory/etc/diagram1.png
Normal file
BIN
abstract-factory/etc/diagram1.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 57 KiB |
BIN
abstract-factory/etc/diagram2.png
Normal file
BIN
abstract-factory/etc/diagram2.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 26 KiB |
190
abstract-factory/etc/presentation.html
Normal file
190
abstract-factory/etc/presentation.html
Normal file
@ -0,0 +1,190 @@
|
|||||||
|
<!--
|
||||||
|
|
||||||
|
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://gnab.github.io/remark/downloads/remark-latest.min.js">
|
||||||
|
</script>
|
||||||
|
<script>
|
||||||
|
var slideshow = remark.create();
|
||||||
|
</script>
|
||||||
|
</body>
|
||||||
|
</html>
|
@ -1,56 +1,47 @@
|
|||||||
<?xml version="1.0"?>
|
<?xml version="1.0"?>
|
||||||
<!-- The MIT License Copyright © 2014-2019 Ilkka Seppälä Permission is hereby
|
<!--
|
||||||
granted, free of charge, to any person obtaining a copy of this software
|
|
||||||
and associated documentation files (the "Software"), to deal in the Software
|
The MIT License
|
||||||
without restriction, including without limitation the rights to use, copy,
|
Copyright (c) 2014-2016 Ilkka Seppälä
|
||||||
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,
|
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||||
subject to the following conditions: The above copyright notice and this
|
of this software and associated documentation files (the "Software"), to deal
|
||||||
permission notice shall be included in all copies or substantial portions
|
in the Software without restriction, including without limitation the rights
|
||||||
of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY
|
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||||
KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
copies of the Software, and to permit persons to whom the Software is
|
||||||
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
|
furnished to do so, subject to the following conditions:
|
||||||
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,
|
The above copyright notice and this permission notice shall be included in
|
||||||
ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
|
all copies or substantial portions of the Software.
|
||||||
DEALINGS IN THE SOFTWARE. -->
|
|
||||||
<project
|
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"
|
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||||
xmlns="http://maven.apache.org/POM/4.0.0"
|
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
|
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||||
<modelVersion>4.0.0</modelVersion>
|
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||||
<parent>
|
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||||
<groupId>com.iluwatar</groupId>
|
THE SOFTWARE.
|
||||||
<artifactId>java-design-patterns</artifactId>
|
|
||||||
<version>1.23.0-SNAPSHOT</version>
|
-->
|
||||||
</parent>
|
<project xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" xmlns="http://maven.apache.org/POM/4.0.0"
|
||||||
<artifactId>abstract-factory</artifactId>
|
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
|
||||||
<dependencies>
|
<modelVersion>4.0.0</modelVersion>
|
||||||
<dependency>
|
<parent>
|
||||||
<groupId>org.junit.jupiter</groupId>
|
<groupId>com.iluwatar</groupId>
|
||||||
<artifactId>junit-jupiter-engine</artifactId>
|
<artifactId>java-design-patterns</artifactId>
|
||||||
<scope>test</scope>
|
<version>1.21.0-SNAPSHOT</version>
|
||||||
</dependency>
|
</parent>
|
||||||
</dependencies>
|
<artifactId>abstract-factory</artifactId>
|
||||||
<build>
|
<dependencies>
|
||||||
<plugins>
|
<dependency>
|
||||||
<!-- Maven assembly plugin is invoked with default setting which we have
|
<groupId>org.junit.jupiter</groupId>
|
||||||
in parent pom and specifying the class having main method -->
|
<artifactId>junit-jupiter-api</artifactId>
|
||||||
<plugin>
|
<scope>test</scope>
|
||||||
<groupId>org.apache.maven.plugins</groupId>
|
</dependency>
|
||||||
<artifactId>maven-assembly-plugin</artifactId>
|
<dependency>
|
||||||
<executions>
|
<groupId>org.junit.jupiter</groupId>
|
||||||
<execution>
|
<artifactId>junit-jupiter-engine</artifactId>
|
||||||
<configuration>
|
<scope>test</scope>
|
||||||
<archive>
|
</dependency>
|
||||||
<manifest>
|
</dependencies>
|
||||||
<mainClass>com.iluwatar.abstractfactory.App</mainClass>
|
|
||||||
</manifest>
|
|
||||||
</archive>
|
|
||||||
</configuration>
|
|
||||||
</execution>
|
|
||||||
</executions>
|
|
||||||
</plugin>
|
|
||||||
</plugins>
|
|
||||||
</build>
|
|
||||||
</project>
|
</project>
|
||||||
|
@ -1,6 +1,6 @@
|
|||||||
/*
|
/**
|
||||||
* The MIT License
|
* The MIT License
|
||||||
* Copyright © 2014-2019 Ilkka Seppälä
|
* Copyright (c) 2014-2016 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,26 +20,27 @@
|
|||||||
* 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
|
*
|
||||||
* have a common theme without specifying their concrete classes. In normal usage, the client
|
* The Abstract Factory pattern provides a way to encapsulate a group of individual factories that have a common theme
|
||||||
* software creates a concrete implementation of the abstract factory and then uses the generic
|
* without specifying their concrete classes. In normal usage, the client software creates a concrete implementation of
|
||||||
* interface of the factory to create the concrete objects that are part of the theme. The client
|
* the abstract factory and then uses the generic interface of the factory to create the concrete objects that are part
|
||||||
* does not know (or care) which concrete objects it gets from each of these internal factories,
|
* of the theme. The client does not know (or care) which concrete objects it gets from each of these internal
|
||||||
* since it uses only the generic interfaces of their products. This pattern separates the details
|
* factories, since it uses only the generic interfaces of their products. This pattern separates the details of
|
||||||
* of implementation of a set of objects from their general usage and relies on object composition,
|
* implementation of a set of objects from their general usage and relies on object composition, as object creation is
|
||||||
* as object creation is implemented in methods exposed in the factory interface.
|
* implemented in methods exposed in the factory interface.
|
||||||
*
|
* <p>
|
||||||
* <p>The essence of the Abstract Factory pattern is a factory interface ({@link KingdomFactory})
|
* The essence of the Abstract Factory pattern is a factory interface ({@link KingdomFactory}) and its implementations (
|
||||||
* and its implementations ( {@link ElfKingdomFactory}, {@link OrcKingdomFactory}). The example uses
|
* {@link ElfKingdomFactory}, {@link OrcKingdomFactory}). The example uses both concrete implementations to create a
|
||||||
* both concrete implementations to create a king, a castle and an army.
|
* king, a castle and an army.
|
||||||
|
*
|
||||||
*/
|
*/
|
||||||
public class App {
|
public class App {
|
||||||
|
|
||||||
@ -50,14 +51,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();
|
||||||
}
|
}
|
||||||
@ -69,7 +70,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();
|
||||||
}
|
}
|
||||||
@ -81,7 +82,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();
|
||||||
}
|
}
|
||||||
@ -123,12 +124,13 @@ public class 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(String[] args) {
|
||||||
|
|
||||||
var app = new App();
|
App 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 © 2014-2019 Ilkka Seppälä
|
* Copyright (c) 2014-2016 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,11 +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.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 © 2014-2019 Ilkka Seppälä
|
* Copyright (c) 2014-2016 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,11 +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.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 © 2014-2019 Ilkka Seppälä
|
* Copyright (c) 2014-2016 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,11 +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.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 © 2014-2019 Ilkka Seppälä
|
* Copyright (c) 2014-2016 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,11 +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.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 © 2014-2019 Ilkka Seppälä
|
* Copyright (c) 2014-2016 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,11 +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.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 © 2014-2019 Ilkka Seppälä
|
* Copyright (c) 2014-2016 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,25 +20,23 @@
|
|||||||
* 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 {
|
||||||
|
|
||||||
@Override
|
|
||||||
public Castle createCastle() {
|
public Castle createCastle() {
|
||||||
return new ElfCastle();
|
return new ElfCastle();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
|
||||||
public King createKing() {
|
public King createKing() {
|
||||||
return new ElfKing();
|
return new ElfKing();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
|
||||||
public Army createArmy() {
|
public Army createArmy() {
|
||||||
return new ElfArmy();
|
return new ElfArmy();
|
||||||
}
|
}
|
||||||
|
@ -1,6 +1,6 @@
|
|||||||
/*
|
/**
|
||||||
* The MIT License
|
* The MIT License
|
||||||
* Copyright © 2014-2019 Ilkka Seppälä
|
* Copyright (c) 2014-2016 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,11 +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.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 © 2014-2019 Ilkka Seppälä
|
* Copyright (c) 2014-2016 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,11 +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.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 © 2014-2019 Ilkka Seppälä
|
* Copyright (c) 2014-2016 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,11 +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.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 © 2014-2019 Ilkka Seppälä
|
* Copyright (c) 2014-2016 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,11 +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.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 © 2014-2019 Ilkka Seppälä
|
* Copyright (c) 2014-2016 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,11 +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.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 © 2014-2019 Ilkka Seppälä
|
* Copyright (c) 2014-2016 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,25 +20,23 @@
|
|||||||
* 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 {
|
||||||
|
|
||||||
@Override
|
|
||||||
public Castle createCastle() {
|
public Castle createCastle() {
|
||||||
return new OrcCastle();
|
return new OrcCastle();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
|
||||||
public King createKing() {
|
public King createKing() {
|
||||||
return new OrcKing();
|
return new OrcKing();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
|
||||||
public Army createArmy() {
|
public Army createArmy() {
|
||||||
return new OrcArmy();
|
return new OrcArmy();
|
||||||
}
|
}
|
||||||
|
@ -1,26 +0,0 @@
|
|||||||
/*
|
|
||||||
* The MIT License
|
|
||||||
* Copyright © 2014-2019 Ilkka Seppälä
|
|
||||||
*
|
|
||||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
||||||
* of this software and associated documentation files (the "Software"), to deal
|
|
||||||
* in the Software without restriction, including without limitation the rights
|
|
||||||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
||||||
* copies of the Software, and to permit persons to whom the Software is
|
|
||||||
* furnished to do so, subject to the following conditions:
|
|
||||||
*
|
|
||||||
* The above copyright notice and this permission notice shall be included in
|
|
||||||
* all copies or substantial portions of the Software.
|
|
||||||
*
|
|
||||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
||||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
||||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
||||||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
||||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
||||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
||||||
* THE SOFTWARE.
|
|
||||||
*/
|
|
||||||
|
|
||||||
module com.iluwatar.abstractfactory {
|
|
||||||
requires org.slf4j;
|
|
||||||
}
|
|
@ -1,6 +1,6 @@
|
|||||||
/*
|
/**
|
||||||
* The MIT License
|
* The MIT License
|
||||||
* Copyright © 2014-2019 Ilkka Seppälä
|
* Copyright (c) 2014-2016 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,7 +20,6 @@
|
|||||||
* 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;
|
||||||
@ -28,11 +27,12 @@ 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 var elfKing = app.getKing(elfFactory);
|
final King elfKing = app.getKing(elfFactory);
|
||||||
assertTrue(elfKing instanceof ElfKing);
|
assertTrue(elfKing instanceof ElfKing);
|
||||||
assertEquals(ElfKing.DESCRIPTION, elfKing.getDescription());
|
assertEquals(ElfKing.DESCRIPTION, elfKing.getDescription());
|
||||||
final var orcKing = app.getKing(orcFactory);
|
final King 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 var elfCastle = app.getCastle(elfFactory);
|
final Castle elfCastle = app.getCastle(elfFactory);
|
||||||
assertTrue(elfCastle instanceof ElfCastle);
|
assertTrue(elfCastle instanceof ElfCastle);
|
||||||
assertEquals(ElfCastle.DESCRIPTION, elfCastle.getDescription());
|
assertEquals(ElfCastle.DESCRIPTION, elfCastle.getDescription());
|
||||||
final var orcCastle = app.getCastle(orcFactory);
|
final Castle 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 var elfArmy = app.getArmy(elfFactory);
|
final Army elfArmy = app.getArmy(elfFactory);
|
||||||
assertTrue(elfArmy instanceof ElfArmy);
|
assertTrue(elfArmy instanceof ElfArmy);
|
||||||
assertEquals(ElfArmy.DESCRIPTION, elfArmy.getDescription());
|
assertEquals(ElfArmy.DESCRIPTION, elfArmy.getDescription());
|
||||||
final var orcArmy = app.getArmy(orcFactory);
|
final Army 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 var king = app.getKing();
|
final King king = app.getKing();
|
||||||
final var castle = app.getCastle();
|
final Castle castle = app.getCastle();
|
||||||
final var army = app.getArmy();
|
final Army 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 var king = app.getKing();
|
final King king = app.getKing();
|
||||||
final var castle = app.getCastle();
|
final Castle castle = app.getCastle();
|
||||||
final var army = app.getArmy();
|
final Army 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 © 2014-2019 Ilkka Seppälä
|
* Copyright (c) 2014-2016 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,17 +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.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() {
|
public void test() throws IOException {
|
||||||
App.main(new String[]{});
|
String[] args = {};
|
||||||
|
App.main(args);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -5,18 +5,17 @@ folder: acyclic-visitor
|
|||||||
permalink: /patterns/acyclic-visitor/
|
permalink: /patterns/acyclic-visitor/
|
||||||
categories: Behavioral
|
categories: Behavioral
|
||||||
tags:
|
tags:
|
||||||
- Extensibility
|
- Java
|
||||||
|
- Difficulty-Intermediate
|
||||||
---
|
---
|
||||||
|
|
||||||
|

|
||||||
|
|
||||||
## Intent
|
## Intent
|
||||||
Allow new functions to be added to existing class hierarchies without affecting those hierarchies, and without creating the troublesome dependency cycles that are inherent to the GOF VISITOR Pattern.
|
Allow new functions to be added to existing class hierarchies without affecting those hierarchies, and without creating the troublesome dependency cycles that are inherent to the GOF VISITOR Pattern.
|
||||||
|
|
||||||
## Class diagram
|
|
||||||

|
|
||||||
|
|
||||||
## Applicability
|
## Applicability
|
||||||
This pattern can be used:
|
This pattern can be used:
|
||||||
|
|
||||||
* When you need to add a new function to an existing hierarchy without the need to alter or affect that hierarchy.
|
* When you need to add a new function to an existing hierarchy without the need to alter or affect that hierarchy.
|
||||||
* When there are functions that operate upon a hierarchy, but which do not belong in the hierarchy itself. e.g. the ConfigureForDOS / ConfigureForUnix / ConfigureForX issue.
|
* When there are functions that operate upon a hierarchy, but which do not belong in the hierarchy itself. e.g. the ConfigureForDOS / ConfigureForUnix / ConfigureForX issue.
|
||||||
* When you need to perform very different operations on an object depending upon its type.
|
* When you need to perform very different operations on an object depending upon its type.
|
||||||
@ -25,18 +24,16 @@ This pattern can be used:
|
|||||||
|
|
||||||
## Consequences
|
## Consequences
|
||||||
The good:
|
The good:
|
||||||
|
|
||||||
* No dependency cycles between class hierarchies.
|
* No dependency cycles between class hierarchies.
|
||||||
* No need to recompile all the visitors if a new one is added.
|
* No need to recompile all the visitors if a new one is added.
|
||||||
* Does not cause compilation failure in existing visitors if class hierarchy has a new member.
|
* Does not cause compilation failure in existing visitors if class hierarchy has a new member.
|
||||||
|
|
||||||
The bad:
|
The bad:
|
||||||
|
|
||||||
* Violates the principle of least surprise or Liskov's Substitution principle by showing that it can accept all visitors but actually only being interested in particular visitors.
|
* Violates the principle of least surprise or Liskov's Substitution principle by showing that it can accept all visitors but actually only being interested in particular visitors.
|
||||||
* 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/)
|
* [Visitor Pattern](../visitor/README.md)
|
||||||
|
|
||||||
## 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,53 +0,0 @@
|
|||||||
@startuml
|
|
||||||
package com.iluwatar.acyclicvisitor {
|
|
||||||
interface AllModemVisitor {
|
|
||||||
}
|
|
||||||
class App {
|
|
||||||
+ App()
|
|
||||||
+ main(args : String[]) {static}
|
|
||||||
}
|
|
||||||
class ConfigureForDosVisitor {
|
|
||||||
- LOGGER : Logger {static}
|
|
||||||
+ ConfigureForDosVisitor()
|
|
||||||
+ visit(hayes : Hayes)
|
|
||||||
+ visit(zoom : Zoom)
|
|
||||||
}
|
|
||||||
class ConfigureForUnixVisitor {
|
|
||||||
- LOGGER : Logger {static}
|
|
||||||
+ ConfigureForUnixVisitor()
|
|
||||||
+ visit(zoom : Zoom)
|
|
||||||
}
|
|
||||||
class Hayes {
|
|
||||||
- LOGGER : Logger {static}
|
|
||||||
+ Hayes()
|
|
||||||
+ accept(modemVisitor : ModemVisitor)
|
|
||||||
+ toString() : String
|
|
||||||
}
|
|
||||||
interface HayesVisitor {
|
|
||||||
+ visit(Hayes) {abstract}
|
|
||||||
}
|
|
||||||
abstract class Modem {
|
|
||||||
+ Modem()
|
|
||||||
+ accept(ModemVisitor) {abstract}
|
|
||||||
}
|
|
||||||
interface ModemVisitor {
|
|
||||||
}
|
|
||||||
class Zoom {
|
|
||||||
- LOGGER : Logger {static}
|
|
||||||
+ Zoom()
|
|
||||||
+ accept(modemVisitor : ModemVisitor)
|
|
||||||
+ toString() : String
|
|
||||||
}
|
|
||||||
interface ZoomVisitor {
|
|
||||||
+ visit(Zoom) {abstract}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
AllModemVisitor --|> ZoomVisitor
|
|
||||||
AllModemVisitor --|> HayesVisitor
|
|
||||||
ConfigureForDosVisitor ..|> AllModemVisitor
|
|
||||||
ConfigureForUnixVisitor ..|> ZoomVisitor
|
|
||||||
Hayes --|> Modem
|
|
||||||
HayesVisitor --|> ModemVisitor
|
|
||||||
Zoom --|> Modem
|
|
||||||
ZoomVisitor --|> ModemVisitor
|
|
||||||
@enduml
|
|
@ -1,29 +1,38 @@
|
|||||||
<!-- 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
|
The MIT License
|
||||||
without restriction, including without limitation the rights to use, copy,
|
Copyright (c) 2014 Ilkka Seppälä
|
||||||
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,
|
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||||
subject to the following conditions: The above copyright notice and this
|
of this software and associated documentation files (the "Software"), to deal
|
||||||
permission notice shall be included in all copies or substantial portions
|
in the Software without restriction, including without limitation the rights
|
||||||
of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY
|
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||||
KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
copies of the Software, and to permit persons to whom the Software is
|
||||||
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
|
furnished to do so, subject to the following conditions:
|
||||||
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,
|
The above copyright notice and this permission notice shall be included in
|
||||||
ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
|
all copies or substantial portions of the Software.
|
||||||
DEALINGS IN THE SOFTWARE. -->
|
|
||||||
|
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||||
|
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||||
|
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||||
|
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||||
|
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||||
|
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||||
|
THE SOFTWARE.
|
||||||
|
|
||||||
|
-->
|
||||||
<project xmlns="http://maven.apache.org/POM/4.0.0"
|
<project xmlns="http://maven.apache.org/POM/4.0.0"
|
||||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||||
<modelVersion>4.0.0</modelVersion>
|
<modelVersion>4.0.0</modelVersion>
|
||||||
|
|
||||||
<parent>
|
<parent>
|
||||||
<groupId>com.iluwatar</groupId>
|
<groupId>com.iluwatar</groupId>
|
||||||
<artifactId>java-design-patterns</artifactId>
|
<artifactId>java-design-patterns</artifactId>
|
||||||
<version>1.23.0-SNAPSHOT</version>
|
<version>1.21.0-SNAPSHOT</version>
|
||||||
</parent>
|
</parent>
|
||||||
|
|
||||||
<artifactId>acyclic-visitor</artifactId>
|
<artifactId>acyclic-visitor</artifactId>
|
||||||
|
|
||||||
<properties>
|
<properties>
|
||||||
@ -44,40 +53,24 @@
|
|||||||
<dependency>
|
<dependency>
|
||||||
<groupId>uk.org.lidalia</groupId>
|
<groupId>uk.org.lidalia</groupId>
|
||||||
<artifactId>slf4j-test</artifactId>
|
<artifactId>slf4j-test</artifactId>
|
||||||
<version>1.2.0</version>
|
<version>1.0.0</version>
|
||||||
<scope>test</scope>
|
<scope>test</scope>
|
||||||
</dependency>
|
</dependency>
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>org.junit.jupiter</groupId>
|
<groupId>org.junit.jupiter</groupId>
|
||||||
<artifactId>junit-jupiter-engine</artifactId>
|
<artifactId>junit-jupiter-api</artifactId>
|
||||||
<scope>test</scope>
|
<scope>test</scope>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.junit.jupiter</groupId>
|
||||||
|
<artifactId>junit-jupiter-engine</artifactId>
|
||||||
|
<scope>test</scope>
|
||||||
|
</dependency>
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>org.mockito</groupId>
|
<groupId>org.mockito</groupId>
|
||||||
<artifactId>mockito-all</artifactId>
|
<artifactId>mockito-all</artifactId>
|
||||||
<version>1.10.19</version>
|
<version>1.9.5</version>
|
||||||
<scope>test</scope>
|
<scope>test</scope>
|
||||||
</dependency>
|
</dependency>
|
||||||
</dependencies>
|
</dependencies>
|
||||||
<build>
|
|
||||||
<plugins>
|
|
||||||
<!-- Maven assembly plugin is invoked with default setting which we have
|
|
||||||
in parent pom and specifying the class having main method -->
|
|
||||||
<plugin>
|
|
||||||
<groupId>org.apache.maven.plugins</groupId>
|
|
||||||
<artifactId>maven-assembly-plugin</artifactId>
|
|
||||||
<executions>
|
|
||||||
<execution>
|
|
||||||
<configuration>
|
|
||||||
<archive>
|
|
||||||
<manifest>
|
|
||||||
<mainClass>com.iluwatar.acyclicvisitor.App</mainClass>
|
|
||||||
</manifest>
|
|
||||||
</archive>
|
|
||||||
</configuration>
|
|
||||||
</execution>
|
|
||||||
</executions>
|
|
||||||
</plugin>
|
|
||||||
</plugins>
|
|
||||||
</build>
|
|
||||||
</project>
|
</project>
|
||||||
|
@ -1,6 +1,6 @@
|
|||||||
/*
|
/**
|
||||||
* The MIT License
|
* The MIT License
|
||||||
* Copyright © 2014-2019 Ilkka Seppälä
|
* Copyright (c) 2014-2016 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.acyclicvisitor;
|
package com.iluwatar.acyclicvisitor;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* All ModemVisitor interface extends all visitor interfaces. This interface provides ease of use
|
* All ModemVisitor interface extends all visitor interfaces. This interface
|
||||||
* when a visitor needs to visit all modem types.
|
* provides ease of use 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 © 2014-2019 Ilkka Seppälä
|
* Copyright (c) 2014-2016 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,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.acyclicvisitor;
|
package com.iluwatar.acyclicvisitor;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* The Acyclic Visitor pattern allows new functions to be added to existing class hierarchies
|
* The Acyclic Visitor pattern allows new functions to be added to existing class
|
||||||
* without affecting those hierarchies, and without creating the dependency cycles that are inherent
|
* hierarchies without affecting those hierarchies, and without creating the dependency
|
||||||
* to the GoF Visitor pattern, by making the Visitor base class degenerate
|
* cycles that are inherent to the GoF Visitor pattern, by making the Visitor base class
|
||||||
*
|
* degenerate
|
||||||
* <p>In this example the visitor base class is {@link ModemVisitor}. The base class of the visited
|
* <p>
|
||||||
* hierarchy is {@link Modem} and has two children {@link Hayes} and {@link Zoom} each one having
|
* In this example the visitor base class is {@link ModemVisitor}. The base class of the
|
||||||
* its own visitor interface {@link HayesVisitor} and {@link ZoomVisitor} respectively. {@link
|
* visited hierarchy is {@link Modem} and has two children {@link Hayes} and {@link Zoom}
|
||||||
* ConfigureForUnixVisitor} and {@link ConfigureForDosVisitor} implement each derivative's visit
|
* each one having its own visitor interface {@link HayesVisitor} and {@link ZoomVisitor}
|
||||||
* method only if it is required
|
* respectively. {@link ConfigureForUnixVisitor} and {@link ConfigureForDosVisitor}
|
||||||
|
* 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) {
|
|
||||||
var conUnix = new ConfigureForUnixVisitor();
|
public static void main(String[] args) {
|
||||||
var conDos = new ConfigureForDosVisitor();
|
ConfigureForUnixVisitor conUnix = new ConfigureForUnixVisitor();
|
||||||
|
ConfigureForDosVisitor conDos = new ConfigureForDosVisitor();
|
||||||
var zoom = new Zoom();
|
|
||||||
var hayes = new Hayes();
|
Zoom zoom = new Zoom();
|
||||||
|
Hayes hayes = new Hayes();
|
||||||
hayes.accept(conDos); // Hayes modem with Dos configurator
|
|
||||||
|
hayes.accept(conDos); // Hayes modem with Unix 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
|
||||||
zoom.accept(conUnix); // Zoom modem with Unix configurator
|
zoom.accept(conUnix); // Zoom modem with Unix configurator
|
||||||
|
@ -1,6 +1,6 @@
|
|||||||
/*
|
/**
|
||||||
* The MIT License
|
* The MIT License
|
||||||
* Copyright © 2014-2019 Ilkka Seppälä
|
* Copyright (c) 2014-2016 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,26 +20,23 @@
|
|||||||
* 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 for Dos
|
* ConfigureForDosVisitor class implements both zoom's and hayes' visit method
|
||||||
* manufacturer.
|
* for Dos manufacturer
|
||||||
*/
|
*/
|
||||||
public class ConfigureForDosVisitor implements AllModemVisitor {
|
public class ConfigureForDosVisitor implements AllModemVisitor {
|
||||||
|
|
||||||
private static final Logger LOGGER = LoggerFactory.getLogger(ConfigureForDosVisitor.class);
|
private static final Logger LOGGER = LoggerFactory.getLogger(ConfigureForDosVisitor.class);
|
||||||
|
|
||||||
@Override
|
|
||||||
public void visit(Hayes hayes) {
|
public void visit(Hayes hayes) {
|
||||||
LOGGER.info(hayes + " used with Dos configurator.");
|
LOGGER.info(hayes + " used with Dos configurator.");
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
|
||||||
public void visit(Zoom zoom) {
|
public void visit(Zoom zoom) {
|
||||||
LOGGER.info(zoom + " used with Dos configurator.");
|
LOGGER.info(zoom + " used with Dos configurator.");
|
||||||
}
|
}
|
||||||
|
@ -1,6 +1,6 @@
|
|||||||
/*
|
/**
|
||||||
* The MIT License
|
* The MIT License
|
||||||
* Copyright © 2014-2019 Ilkka Seppälä
|
* Copyright (c) 2014-2016 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,21 +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.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 manufacturer, unlike
|
* ConfigureForUnixVisitor class implements zoom's visit method for Unix
|
||||||
* traditional visitor pattern, this class may selectively implement visit for other modems.
|
* manufacturer, unlike traditional visitor pattern, this class may selectively implement
|
||||||
|
* visit for other modems.
|
||||||
*/
|
*/
|
||||||
public class ConfigureForUnixVisitor implements ZoomVisitor {
|
public class ConfigureForUnixVisitor implements ZoomVisitor {
|
||||||
|
|
||||||
private static final Logger LOGGER = LoggerFactory.getLogger(ConfigureForUnixVisitor.class);
|
private static final Logger LOGGER = LoggerFactory.getLogger(ConfigureForUnixVisitor.class);
|
||||||
|
|
||||||
@Override
|
|
||||||
public void visit(Zoom zoom) {
|
public void visit(Zoom zoom) {
|
||||||
LOGGER.info(zoom + " used with Unix configurator.");
|
LOGGER.info(zoom + " used with Unix configurator.");
|
||||||
}
|
}
|
||||||
|
@ -1,6 +1,6 @@
|
|||||||
/*
|
/**
|
||||||
* The MIT License
|
* The MIT License
|
||||||
* Copyright © 2014-2019 Ilkka Seppälä
|
* Copyright (c) 2014-2016 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,21 +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.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) {
|
||||||
@ -45,9 +44,10 @@ public class Hayes extends Modem {
|
|||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Hayes' modem's toString method.
|
* Hayes' modem's toString
|
||||||
|
* method
|
||||||
*/
|
*/
|
||||||
@Override
|
@Override
|
||||||
public String toString() {
|
public String toString() {
|
||||||
|
@ -1,6 +1,6 @@
|
|||||||
/*
|
/**
|
||||||
* The MIT License
|
* The MIT License
|
||||||
* Copyright © 2014-2019 Ilkka Seppälä
|
* Copyright (c) 2014-2016 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,11 +20,10 @@
|
|||||||
* 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 © 2014-2019 Ilkka Seppälä
|
* Copyright (c) 2014-2016 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,11 +20,10 @@
|
|||||||
* 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 © 2014-2019 Ilkka Seppälä
|
* Copyright (c) 2014-2016 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 depend on the
|
* ModemVisitor interface does not contain any visit methods so that it does not
|
||||||
* visited hierarchy. Each derivative's visit method is declared in its own visitor interface
|
* depend on the visited hierarchy. Each derivative's visit method is declared in
|
||||||
|
* 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 © 2014-2019 Ilkka Seppälä
|
* Copyright (c) 2014-2016 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 method.
|
* Zoom modem's toString
|
||||||
|
* method
|
||||||
*/
|
*/
|
||||||
@Override
|
@Override
|
||||||
public String toString() {
|
public String toString() {
|
||||||
|
@ -1,6 +1,6 @@
|
|||||||
/*
|
/**
|
||||||
* The MIT License
|
* The MIT License
|
||||||
* Copyright © 2014-2019 Ilkka Seppälä
|
* Copyright (c) 2014-2016 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,11 +20,10 @@
|
|||||||
* 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,26 +0,0 @@
|
|||||||
/*
|
|
||||||
* The MIT License
|
|
||||||
* Copyright © 2014-2019 Ilkka Seppälä
|
|
||||||
*
|
|
||||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
||||||
* of this software and associated documentation files (the "Software"), to deal
|
|
||||||
* in the Software without restriction, including without limitation the rights
|
|
||||||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
||||||
* copies of the Software, and to permit persons to whom the Software is
|
|
||||||
* furnished to do so, subject to the following conditions:
|
|
||||||
*
|
|
||||||
* The above copyright notice and this permission notice shall be included in
|
|
||||||
* all copies or substantial portions of the Software.
|
|
||||||
*
|
|
||||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
||||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
||||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
||||||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
||||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
||||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
||||||
* THE SOFTWARE.
|
|
||||||
*/
|
|
||||||
|
|
||||||
module com.iluwatar.acyclicvisitor {
|
|
||||||
requires org.slf4j;
|
|
||||||
}
|
|
@ -1,6 +1,6 @@
|
|||||||
/*
|
/**
|
||||||
* The MIT License
|
* The MIT License
|
||||||
* Copyright © 2014-2019 Ilkka Seppälä
|
* Copyright (c) 2014-2016 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,18 +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.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() {
|
||||||
App.main(new String[]{});
|
String[] args = {};
|
||||||
|
App.main(args);
|
||||||
}
|
}
|
||||||
}
|
}
|
@ -1,6 +1,6 @@
|
|||||||
/*
|
/**
|
||||||
* The MIT License
|
* The MIT License
|
||||||
* Copyright © 2014-2019 Ilkka Seppälä
|
* Copyright (c) 2014-2016 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,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.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;
|
||||||
|
|
||||||
@ -37,30 +44,28 @@ import uk.org.lidalia.slf4jtest.TestLoggerFactory;
|
|||||||
*/
|
*/
|
||||||
public class ConfigureForDosVisitorTest {
|
public class ConfigureForDosVisitorTest {
|
||||||
|
|
||||||
private TestLogger logger = TestLoggerFactory.getTestLogger(ConfigureForDosVisitor.class);
|
TestLogger logger = TestLoggerFactory.getTestLogger(ConfigureForDosVisitor.class);
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testVisitForZoom() {
|
public void testVisitForZoom() {
|
||||||
var conDos = new ConfigureForDosVisitor();
|
ConfigureForDosVisitor conDos = new ConfigureForDosVisitor();
|
||||||
var zoom = new Zoom();
|
Zoom zoom = new Zoom();
|
||||||
|
|
||||||
conDos.visit(zoom);
|
conDos.visit(zoom);
|
||||||
|
|
||||||
assertThat(logger.getLoggingEvents())
|
assertThat(logger.getLoggingEvents()).extracting("level", "message").contains(
|
||||||
.extracting("level", "message")
|
tuple(INFO, zoom + " used with Dos configurator."));
|
||||||
.contains(tuple(INFO, zoom + " used with Dos configurator."));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testVisitForHayes() {
|
public void testVisitForHayes() {
|
||||||
var conDos = new ConfigureForDosVisitor();
|
ConfigureForDosVisitor conDos = new ConfigureForDosVisitor();
|
||||||
var hayes = new Hayes();
|
Hayes hayes = new Hayes();
|
||||||
|
|
||||||
conDos.visit(hayes);
|
conDos.visit(hayes);
|
||||||
|
|
||||||
assertThat(logger.getLoggingEvents())
|
assertThat(logger.getLoggingEvents()).extracting("level", "message").contains(
|
||||||
.extracting("level", "message")
|
tuple(INFO, hayes + " used with Dos configurator."));
|
||||||
.contains(tuple(INFO, hayes + " used with Dos configurator."));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@AfterEach
|
@AfterEach
|
||||||
|
@ -1,6 +1,6 @@
|
|||||||
/*
|
/**
|
||||||
* The MIT License
|
* The MIT License
|
||||||
* Copyright © 2014-2019 Ilkka Seppälä
|
* Copyright (c) 2014-2016 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,25 +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;
|
||||||
|
|
||||||
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 uk.org.lidalia.slf4jext.Level.INFO;
|
import static uk.org.lidalia.slf4jext.Level.INFO;
|
||||||
|
import static org.mockito.Mockito.mock;
|
||||||
import org.junit.jupiter.api.AfterEach;
|
|
||||||
import org.junit.jupiter.api.Test;
|
|
||||||
|
|
||||||
import uk.org.lidalia.slf4jtest.TestLogger;
|
import uk.org.lidalia.slf4jtest.TestLogger;
|
||||||
import uk.org.lidalia.slf4jtest.TestLoggerFactory;
|
import uk.org.lidalia.slf4jtest.TestLoggerFactory;
|
||||||
|
import org.junit.jupiter.api.Test;
|
||||||
|
|
||||||
|
import com.iluwatar.acyclicvisitor.ConfigureForUnixVisitor;
|
||||||
|
import com.iluwatar.acyclicvisitor.Hayes;
|
||||||
|
import com.iluwatar.acyclicvisitor.HayesVisitor;
|
||||||
|
import com.iluwatar.acyclicvisitor.Zoom;
|
||||||
|
import com.iluwatar.acyclicvisitor.ZoomVisitor;
|
||||||
|
|
||||||
|
import org.junit.jupiter.api.AfterEach;
|
||||||
|
import org.junit.jupiter.api.Assertions;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* ConfigureForUnixVisitor test class
|
* ConfigureForUnixVisitor test class
|
||||||
*/
|
*/
|
||||||
public class ConfigureForUnixVisitorTest {
|
public class ConfigureForUnixVisitorTest {
|
||||||
|
|
||||||
private static final TestLogger LOGGER = TestLoggerFactory.getTestLogger(ConfigureForUnixVisitor.class);
|
TestLogger logger = TestLoggerFactory.getTestLogger(ConfigureForUnixVisitor.class);
|
||||||
|
|
||||||
@AfterEach
|
@AfterEach
|
||||||
public void clearLoggers() {
|
public void clearLoggers() {
|
||||||
@ -47,13 +54,12 @@ public class ConfigureForUnixVisitorTest {
|
|||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testVisitForZoom() {
|
public void testVisitForZoom() {
|
||||||
var conUnix = new ConfigureForUnixVisitor();
|
ConfigureForUnixVisitor conUnix = new ConfigureForUnixVisitor();
|
||||||
var zoom = new Zoom();
|
Zoom zoom = new Zoom();
|
||||||
|
|
||||||
conUnix.visit(zoom);
|
conUnix.visit(zoom);
|
||||||
|
|
||||||
assertThat(LOGGER.getLoggingEvents())
|
assertThat(logger.getLoggingEvents()).extracting("level", "message").contains(
|
||||||
.extracting("level", "message")
|
tuple(INFO, zoom + " used with Unix configurator."));
|
||||||
.contains(tuple(INFO, zoom + " used with Unix configurator."));
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,6 +1,6 @@
|
|||||||
/*
|
/**
|
||||||
* The MIT License
|
* The MIT License
|
||||||
* Copyright © 2014-2019 Ilkka Seppälä
|
* Copyright (c) 2014-2016 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,7 +20,6 @@
|
|||||||
* 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;
|
||||||
@ -30,6 +29,11 @@ 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
|
||||||
*/
|
*/
|
||||||
@ -37,8 +41,8 @@ public class HayesTest {
|
|||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testAcceptForDos() {
|
public void testAcceptForDos() {
|
||||||
var hayes = new Hayes();
|
Hayes hayes = new Hayes();
|
||||||
var mockVisitor = mock(ConfigureForDosVisitor.class);
|
ConfigureForDosVisitor mockVisitor = mock(ConfigureForDosVisitor.class);
|
||||||
|
|
||||||
hayes.accept(mockVisitor);
|
hayes.accept(mockVisitor);
|
||||||
verify((HayesVisitor)mockVisitor).visit(eq(hayes));
|
verify((HayesVisitor)mockVisitor).visit(eq(hayes));
|
||||||
@ -46,8 +50,8 @@ public class HayesTest {
|
|||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testAcceptForUnix() {
|
public void testAcceptForUnix() {
|
||||||
var hayes = new Hayes();
|
Hayes hayes = new Hayes();
|
||||||
var mockVisitor = mock(ConfigureForUnixVisitor.class);
|
ConfigureForUnixVisitor mockVisitor = mock(ConfigureForUnixVisitor.class);
|
||||||
|
|
||||||
hayes.accept(mockVisitor);
|
hayes.accept(mockVisitor);
|
||||||
|
|
||||||
|
@ -1,6 +1,6 @@
|
|||||||
/*
|
/**
|
||||||
* The MIT License
|
* The MIT License
|
||||||
* Copyright © 2014-2019 Ilkka Seppälä
|
* Copyright (c) 2014-2016 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,7 +20,6 @@
|
|||||||
* 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;
|
||||||
|
|
||||||
|
|
||||||
@ -30,6 +29,11 @@ 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
|
||||||
*/
|
*/
|
||||||
@ -37,8 +41,8 @@ public class ZoomTest {
|
|||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testAcceptForDos() {
|
public void testAcceptForDos() {
|
||||||
var zoom = new Zoom();
|
Zoom zoom = new Zoom();
|
||||||
var mockVisitor = mock(ConfigureForDosVisitor.class);
|
ConfigureForDosVisitor mockVisitor = mock(ConfigureForDosVisitor.class);
|
||||||
|
|
||||||
zoom.accept(mockVisitor);
|
zoom.accept(mockVisitor);
|
||||||
verify((ZoomVisitor)mockVisitor).visit(eq(zoom));
|
verify((ZoomVisitor)mockVisitor).visit(eq(zoom));
|
||||||
@ -46,8 +50,8 @@ public class ZoomTest {
|
|||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testAcceptForUnix() {
|
public void testAcceptForUnix() {
|
||||||
var zoom = new Zoom();
|
Zoom zoom = new Zoom();
|
||||||
var mockVisitor = mock(ConfigureForUnixVisitor.class);
|
ConfigureForUnixVisitor mockVisitor = mock(ConfigureForUnixVisitor.class);
|
||||||
|
|
||||||
zoom.accept(mockVisitor);
|
zoom.accept(mockVisitor);
|
||||||
verify((ZoomVisitor)mockVisitor).visit(eq(zoom));
|
verify((ZoomVisitor)mockVisitor).visit(eq(zoom));
|
||||||
|
@ -5,7 +5,9 @@ folder: adapter
|
|||||||
permalink: /patterns/adapter/
|
permalink: /patterns/adapter/
|
||||||
categories: Structural
|
categories: Structural
|
||||||
tags:
|
tags:
|
||||||
- Gang of Four
|
- Java
|
||||||
|
- Gang Of Four
|
||||||
|
- Difficulty-Beginner
|
||||||
---
|
---
|
||||||
|
|
||||||
## Also known as
|
## Also known as
|
||||||
@ -54,14 +56,15 @@ 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 {
|
public class Captain implements RowingBoat {
|
||||||
|
|
||||||
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();
|
||||||
}
|
}
|
||||||
@ -91,13 +94,10 @@ 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
|
||||||
var captain = new Captain(new FishingBoatAdapter());
|
Captain captain = new Captain(new FishingBoatAdapter());
|
||||||
captain.row();
|
captain.row();
|
||||||
```
|
```
|
||||||
|
|
||||||
## Class diagram
|
|
||||||

|
|
||||||
|
|
||||||
## Applicability
|
## Applicability
|
||||||
Use the Adapter pattern when
|
Use the Adapter pattern when
|
||||||
|
|
||||||
@ -129,7 +129,5 @@ An object adapter
|
|||||||
|
|
||||||
## Credits
|
## Credits
|
||||||
|
|
||||||
* [Design Patterns: Elements of Reusable Object-Oriented Software](https://www.amazon.com/gp/product/0201633612/ref=as_li_tl?ie=UTF8&camp=1789&creative=9325&creativeASIN=0201633612&linkCode=as2&tag=javadesignpat-20&linkId=675d49790ce11db99d90bde47f1aeb59)
|
* [Design Patterns: Elements of Reusable Object-Oriented Software](http://www.amazon.com/Design-Patterns-Elements-Reusable-Object-Oriented/dp/0201633612)
|
||||||
* [J2EE Design Patterns](https://www.amazon.com/gp/product/0596004273/ref=as_li_tl?ie=UTF8&camp=1789&creative=9325&creativeASIN=0596004273&linkCode=as2&tag=javadesignpat-20&linkId=48d37c67fb3d845b802fa9b619ad8f31)
|
* [J2EE Design Patterns](http://www.amazon.com/J2EE-Design-Patterns-William-Crawford/dp/0596004273/ref=sr_1_2)
|
||||||
* [Head First Design Patterns: A Brain-Friendly Guide](https://www.amazon.com/gp/product/0596007124/ref=as_li_tl?ie=UTF8&camp=1789&creative=9325&creativeASIN=0596007124&linkCode=as2&tag=javadesignpat-20&linkId=6b8b6eea86021af6c8e3cd3fc382cb5b)
|
|
||||||
* [Refactoring to Patterns](https://www.amazon.com/gp/product/0321213351/ref=as_li_tl?ie=UTF8&camp=1789&creative=9325&creativeASIN=0321213351&linkCode=as2&tag=javadesignpat-20&linkId=2a76fcb387234bc71b1c61150b3cc3a7)
|
|
||||||
|
Binary file not shown.
Before Width: | Height: | Size: 25 KiB |
@ -1,31 +0,0 @@
|
|||||||
@startuml
|
|
||||||
package com.iluwatar.adapter {
|
|
||||||
class App {
|
|
||||||
- App()
|
|
||||||
+ main(args : String[]) {static}
|
|
||||||
}
|
|
||||||
class Captain {
|
|
||||||
- rowingBoat : RowingBoat
|
|
||||||
+ Captain()
|
|
||||||
+ Captain(boat : RowingBoat)
|
|
||||||
~ row()
|
|
||||||
~ setRowingBoat(boat : RowingBoat)
|
|
||||||
}
|
|
||||||
~class FishingBoat {
|
|
||||||
- LOGGER : Logger {static}
|
|
||||||
~ FishingBoat()
|
|
||||||
~ sail()
|
|
||||||
}
|
|
||||||
class FishingBoatAdapter {
|
|
||||||
- boat : FishingBoat
|
|
||||||
+ FishingBoatAdapter()
|
|
||||||
+ row()
|
|
||||||
}
|
|
||||||
interface RowingBoat {
|
|
||||||
+ row() {abstract}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
FishingBoatAdapter --> "-boat" FishingBoat
|
|
||||||
Captain --> "-rowingBoat" RowingBoat
|
|
||||||
FishingBoatAdapter ..|> RowingBoat
|
|
||||||
@enduml
|
|
109
adapter/pom.xml
109
adapter/pom.xml
@ -1,61 +1,52 @@
|
|||||||
<?xml version="1.0"?>
|
<?xml version="1.0"?>
|
||||||
<!-- The MIT License Copyright © 2014-2019 Ilkka Seppälä Permission is hereby
|
<!--
|
||||||
granted, free of charge, to any person obtaining a copy of this software
|
|
||||||
and associated documentation files (the "Software"), to deal in the Software
|
The MIT License
|
||||||
without restriction, including without limitation the rights to use, copy,
|
Copyright (c) 2014-2016 Ilkka Seppälä
|
||||||
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,
|
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||||
subject to the following conditions: The above copyright notice and this
|
of this software and associated documentation files (the "Software"), to deal
|
||||||
permission notice shall be included in all copies or substantial portions
|
in the Software without restriction, including without limitation the rights
|
||||||
of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY
|
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||||
KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
copies of the Software, and to permit persons to whom the Software is
|
||||||
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
|
furnished to do so, subject to the following conditions:
|
||||||
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,
|
The above copyright notice and this permission notice shall be included in
|
||||||
ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
|
all copies or substantial portions of the Software.
|
||||||
DEALINGS IN THE SOFTWARE. -->
|
|
||||||
<project
|
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"
|
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||||
xmlns="http://maven.apache.org/POM/4.0.0"
|
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
|
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||||
<modelVersion>4.0.0</modelVersion>
|
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||||
<parent>
|
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||||
<groupId>com.iluwatar</groupId>
|
THE SOFTWARE.
|
||||||
<artifactId>java-design-patterns</artifactId>
|
|
||||||
<version>1.23.0-SNAPSHOT</version>
|
-->
|
||||||
</parent>
|
<project xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" xmlns="http://maven.apache.org/POM/4.0.0"
|
||||||
<artifactId>adapter</artifactId>
|
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
|
||||||
<dependencies>
|
<modelVersion>4.0.0</modelVersion>
|
||||||
<dependency>
|
<parent>
|
||||||
<groupId>org.junit.jupiter</groupId>
|
<groupId>com.iluwatar</groupId>
|
||||||
<artifactId>junit-jupiter-engine</artifactId>
|
<artifactId>java-design-patterns</artifactId>
|
||||||
<scope>test</scope>
|
<version>1.21.0-SNAPSHOT</version>
|
||||||
</dependency>
|
</parent>
|
||||||
<dependency>
|
<artifactId>adapter</artifactId>
|
||||||
<groupId>org.mockito</groupId>
|
<dependencies>
|
||||||
<artifactId>mockito-core</artifactId>
|
<dependency>
|
||||||
<scope>test</scope>
|
<groupId>org.junit.jupiter</groupId>
|
||||||
</dependency>
|
<artifactId>junit-jupiter-api</artifactId>
|
||||||
</dependencies>
|
<scope>test</scope>
|
||||||
<build>
|
</dependency>
|
||||||
<plugins>
|
<dependency>
|
||||||
<!-- Maven assembly plugin is invoked with default setting which we have
|
<groupId>org.junit.jupiter</groupId>
|
||||||
in parent pom and specifying the class having main method -->
|
<artifactId>junit-jupiter-engine</artifactId>
|
||||||
<plugin>
|
<scope>test</scope>
|
||||||
<groupId>org.apache.maven.plugins</groupId>
|
</dependency>
|
||||||
<artifactId>maven-assembly-plugin</artifactId>
|
<dependency>
|
||||||
<executions>
|
<groupId>org.mockito</groupId>
|
||||||
<execution>
|
<artifactId>mockito-core</artifactId>
|
||||||
<configuration>
|
<scope>test</scope>
|
||||||
<archive>
|
</dependency>
|
||||||
<manifest>
|
</dependencies>
|
||||||
<mainClass>com.iluwatar.adapter.App</mainClass>
|
|
||||||
</manifest>
|
|
||||||
</archive>
|
|
||||||
</configuration>
|
|
||||||
</execution>
|
|
||||||
</executions>
|
|
||||||
</plugin>
|
|
||||||
</plugins>
|
|
||||||
</build>
|
|
||||||
</project>
|
</project>
|
||||||
|
@ -1,6 +1,6 @@
|
|||||||
/*
|
/**
|
||||||
* The MIT License
|
* The MIT License
|
||||||
* Copyright © 2014-2019 Ilkka Seppälä
|
* Copyright (c) 2014-2016 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,7 +20,6 @@
|
|||||||
* 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;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -29,33 +28,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>There are two variations of the Adapter pattern: The class adapter implements the adaptee's
|
* <p>
|
||||||
|
* 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>The Adapter ({@link FishingBoatAdapter}) converts the interface of the adaptee class ({@link
|
* <p>
|
||||||
* FishingBoat}) into a suitable one expected by the client ({@link RowingBoat}).
|
* The Adapter ({@link FishingBoatAdapter}) converts the interface of the adaptee class (
|
||||||
|
* {@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 final class App {
|
public class App {
|
||||||
|
|
||||||
private App() {
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Program entry point.
|
* Program entry point.
|
||||||
*
|
*
|
||||||
* @param args command line args
|
* @param args command line args
|
||||||
*/
|
*/
|
||||||
public static void main(final String[] args) {
|
public static void main(String[] args) {
|
||||||
// The captain can only operate rowing boats but with adapter he is able to
|
// The captain can only operate rowing boats but with adapter he is able to use fishing boats as well
|
||||||
// use fishing boats as well
|
Captain captain = new Captain(new FishingBoatAdapter());
|
||||||
var captain = new Captain(new FishingBoatAdapter());
|
|
||||||
captain.row();
|
captain.row();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,6 +1,6 @@
|
|||||||
/*
|
/**
|
||||||
* The MIT License
|
* The MIT License
|
||||||
* Copyright © 2014-2019 Ilkka Seppälä
|
* Copyright (c) 2014-2016 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,28 +20,27 @@
|
|||||||
* 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> This is the client in the pattern.
|
* The Captain uses {@link RowingBoat} to sail. <br>
|
||||||
|
* This is the client in the pattern.
|
||||||
*/
|
*/
|
||||||
public final class Captain {
|
public class Captain {
|
||||||
|
|
||||||
private RowingBoat rowingBoat;
|
private RowingBoat rowingBoat;
|
||||||
|
|
||||||
public Captain() {
|
public Captain() {}
|
||||||
|
|
||||||
|
public Captain(RowingBoat rowingBoat) {
|
||||||
|
this.rowingBoat = rowingBoat;
|
||||||
}
|
}
|
||||||
|
|
||||||
public Captain(final RowingBoat boat) {
|
public void setRowingBoat(RowingBoat rowingBoat) {
|
||||||
this.rowingBoat = boat;
|
this.rowingBoat = rowingBoat;
|
||||||
}
|
}
|
||||||
|
|
||||||
void setRowingBoat(final RowingBoat boat) {
|
public void row() {
|
||||||
this.rowingBoat = boat;
|
|
||||||
}
|
|
||||||
|
|
||||||
void row() {
|
|
||||||
rowingBoat.row();
|
rowingBoat.row();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1,6 +1,6 @@
|
|||||||
/*
|
/**
|
||||||
* The MIT License
|
* The MIT License
|
||||||
* Copyright © 2014-2019 Ilkka Seppälä
|
* Copyright (c) 2014-2016 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
|
*
|
||||||
* sailing.
|
* Device class (adaptee in the pattern). We want to reuse this class.
|
||||||
|
* Fishing boat moves by sailing.
|
||||||
|
*
|
||||||
*/
|
*/
|
||||||
final class FishingBoat {
|
public class FishingBoat {
|
||||||
|
|
||||||
private static final Logger LOGGER = getLogger(FishingBoat.class);
|
private static final Logger LOGGER = LoggerFactory.getLogger(FishingBoat.class);
|
||||||
|
|
||||||
void sail() {
|
public 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 © 2014-2019 Ilkka Seppälä
|
* Copyright (c) 2014-2016 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.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 {
|
||||||
|
|
||||||
@ -35,7 +36,8 @@ public class FishingBoatAdapter implements RowingBoat {
|
|||||||
boat = new FishingBoat();
|
boat = new FishingBoat();
|
||||||
}
|
}
|
||||||
|
|
||||||
public final void row() {
|
@Override
|
||||||
|
public void row() {
|
||||||
boat.sail();
|
boat.sail();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,6 +1,6 @@
|
|||||||
/*
|
/**
|
||||||
* The MIT License
|
* The MIT License
|
||||||
* Copyright © 2014-2019 Ilkka Seppälä
|
* Copyright (c) 2014-2016 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,11 +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;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* The interface expected by the client.<br> A rowing boat is rowed to move.
|
* The interface expected by the client.<br>
|
||||||
|
* A rowing boat is rowed to move.
|
||||||
|
*
|
||||||
*/
|
*/
|
||||||
public interface RowingBoat {
|
public interface RowingBoat {
|
||||||
|
|
||||||
|
@ -1,24 +0,0 @@
|
|||||||
/*
|
|
||||||
* The MIT License
|
|
||||||
* Copyright © 2014-2019 Ilkka Seppälä
|
|
||||||
*
|
|
||||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
||||||
* of this software and associated documentation files (the "Software"), to deal
|
|
||||||
* in the Software without restriction, including without limitation the rights
|
|
||||||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
||||||
* copies of the Software, and to permit persons to whom the Software is
|
|
||||||
* furnished to do so, subject to the following conditions:
|
|
||||||
*
|
|
||||||
* The above copyright notice and this permission notice shall be included in
|
|
||||||
* all copies or substantial portions of the Software.
|
|
||||||
*
|
|
||||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
||||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
||||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
||||||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
||||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
||||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
||||||
* THE SOFTWARE.
|
|
||||||
*/
|
|
||||||
|
|
||||||
package com.iluwatar.adapter;
|
|
@ -1,6 +1,6 @@
|
|||||||
/*
|
/**
|
||||||
* The MIT License
|
* The MIT License
|
||||||
* Copyright © 2014-2019 Ilkka Seppälä
|
* Copyright (c) 2014-2016 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.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 {
|
||||||
|
|
||||||
@ -49,29 +50,29 @@ public class AdapterPatternTest {
|
|||||||
public void setup() {
|
public void setup() {
|
||||||
beans = new HashMap<>();
|
beans = new HashMap<>();
|
||||||
|
|
||||||
var fishingBoatAdapter = spy(new FishingBoatAdapter());
|
FishingBoatAdapter fishingBoatAdapter = spy(new FishingBoatAdapter());
|
||||||
beans.put(FISHING_BEAN, fishingBoatAdapter);
|
beans.put(FISHING_BEAN, fishingBoatAdapter);
|
||||||
|
|
||||||
var captain = new Captain();
|
Captain 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 internally
|
* This test asserts that when we use the row() method on a captain bean(client), it is
|
||||||
* calling sail method on the fishing boat object. The Adapter ({@link FishingBoatAdapter} )
|
* internally 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 expected
|
* ) converts the interface of the target class ( {@link FishingBoat}) into a suitable one
|
||||||
* by the client ({@link Captain} ).
|
* expected by the client ({@link Captain} ).
|
||||||
*/
|
*/
|
||||||
@Test
|
@Test
|
||||||
public void testAdapter() {
|
public void testAdapter() {
|
||||||
var captain = (Captain) beans.get(ROWING_BEAN);
|
Captain 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
|
||||||
var adapter = (RowingBoat) beans.get(FISHING_BEAN);
|
RowingBoat adapter = (RowingBoat) beans.get(FISHING_BEAN);
|
||||||
verify(adapter).row();
|
verify(adapter).row();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,6 +1,6 @@
|
|||||||
/*
|
/**
|
||||||
* The MIT License
|
* The MIT License
|
||||||
* Copyright © 2014-2019 Ilkka Seppälä
|
* Copyright (c) 2014-2016 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,17 +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.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() {
|
public void test() throws IOException {
|
||||||
App.main(new String[]{});
|
String[] args = {};
|
||||||
|
App.main(args);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -5,7 +5,8 @@ folder: aggregator-microservices
|
|||||||
permalink: /patterns/aggregator-microservices/
|
permalink: /patterns/aggregator-microservices/
|
||||||
categories: Architectural
|
categories: Architectural
|
||||||
tags:
|
tags:
|
||||||
- Cloud distributed
|
- Java
|
||||||
|
- Spring
|
||||||
---
|
---
|
||||||
|
|
||||||
## Intent
|
## Intent
|
||||||
@ -17,8 +18,6 @@ More variations of the aggregator are:
|
|||||||
- Chained Microservice Design Pattern: In this case each microservice is dependent/ chained to a series
|
- Chained Microservice Design Pattern: In this case each microservice is dependent/ chained to a series
|
||||||
of other microservices.
|
of other microservices.
|
||||||
|
|
||||||
## Class diagram
|
|
||||||
|
|
||||||

|

|
||||||
|
|
||||||
## Applicability
|
## Applicability
|
||||||
@ -27,4 +26,4 @@ Use the Aggregator Microservices pattern when you need a unified API for various
|
|||||||
|
|
||||||
## Credits
|
## Credits
|
||||||
|
|
||||||
* [Microservice Design Patterns](http://web.archive.org/web/20190705163602/http://blog.arungupta.me/microservice-design-patterns/)
|
* [Microservice Design Patterns](http://blog.arungupta.me/microservice-design-patterns/)
|
||||||
|
@ -1,43 +0,0 @@
|
|||||||
@startuml
|
|
||||||
package com.iluwatar.aggregator.microservices {
|
|
||||||
class Aggregator {
|
|
||||||
- informationClient : ProductInformationClient
|
|
||||||
- inventoryClient : ProductInventoryClient
|
|
||||||
+ Aggregator()
|
|
||||||
+ getProduct() : Product
|
|
||||||
}
|
|
||||||
class App {
|
|
||||||
+ App()
|
|
||||||
+ main(args : String[]) {static}
|
|
||||||
}
|
|
||||||
class Product {
|
|
||||||
- productInventories : int
|
|
||||||
- title : String
|
|
||||||
+ Product()
|
|
||||||
+ getProductInventories() : int
|
|
||||||
+ getTitle() : String
|
|
||||||
+ setProductInventories(productInventories : int)
|
|
||||||
+ setTitle(title : String)
|
|
||||||
}
|
|
||||||
interface ProductInformationClient {
|
|
||||||
+ getProductTitle() : String {abstract}
|
|
||||||
}
|
|
||||||
class ProductInformationClientImpl {
|
|
||||||
- LOGGER : Logger {static}
|
|
||||||
+ ProductInformationClientImpl()
|
|
||||||
+ getProductTitle() : String
|
|
||||||
}
|
|
||||||
interface ProductInventoryClient {
|
|
||||||
+ getProductInventories() : Integer {abstract}
|
|
||||||
}
|
|
||||||
class ProductInventoryClientImpl {
|
|
||||||
- LOGGER : Logger {static}
|
|
||||||
+ ProductInventoryClientImpl()
|
|
||||||
+ getProductInventories() : Integer
|
|
||||||
}
|
|
||||||
}
|
|
||||||
Aggregator --> "-informationClient" ProductInformationClient
|
|
||||||
Aggregator --> "-inventoryClient" ProductInventoryClient
|
|
||||||
ProductInformationClientImpl ..|> ProductInformationClient
|
|
||||||
ProductInventoryClientImpl ..|> ProductInventoryClient
|
|
||||||
@enduml
|
|
@ -1,81 +1,93 @@
|
|||||||
<?xml version="1.0" encoding="UTF-8"?>
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
<!-- The MIT License Copyright © 2014-2019 Ilkka Seppälä Permission is hereby
|
<!--
|
||||||
granted, free of charge, to any person obtaining a copy of this software
|
|
||||||
and associated documentation files (the "Software"), to deal in the Software
|
|
||||||
without restriction, including without limitation the rights to use, copy,
|
|
||||||
modify, merge, publish, distribute, sublicense, and/or sell copies of the
|
|
||||||
Software, and to permit persons to whom the Software is furnished to do so,
|
|
||||||
subject to the following conditions: The above copyright notice and this
|
|
||||||
permission notice shall be included in all copies or substantial portions
|
|
||||||
of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY
|
|
||||||
KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
|
||||||
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
|
|
||||||
NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
|
|
||||||
DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
|
|
||||||
ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
|
|
||||||
DEALINGS IN THE SOFTWARE. -->
|
|
||||||
<project xmlns="http://maven.apache.org/POM/4.0.0"
|
|
||||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
|
||||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
|
||||||
<parent>
|
|
||||||
<artifactId>aggregator-microservices</artifactId>
|
|
||||||
<groupId>com.iluwatar</groupId>
|
|
||||||
<version>1.23.0-SNAPSHOT</version>
|
|
||||||
</parent>
|
|
||||||
<modelVersion>4.0.0</modelVersion>
|
|
||||||
<artifactId>aggregator-service</artifactId>
|
|
||||||
<packaging>jar</packaging>
|
|
||||||
<dependencies>
|
|
||||||
<dependency>
|
|
||||||
<groupId>org.springframework</groupId>
|
|
||||||
<artifactId>spring-webmvc</artifactId>
|
|
||||||
</dependency>
|
|
||||||
<dependency>
|
|
||||||
<groupId>org.springframework.boot</groupId>
|
|
||||||
<artifactId>spring-boot-starter-web</artifactId>
|
|
||||||
</dependency>
|
|
||||||
<dependency>
|
|
||||||
<groupId>org.junit.jupiter</groupId>
|
|
||||||
<artifactId>junit-jupiter-engine</artifactId>
|
|
||||||
<scope>test</scope>
|
|
||||||
</dependency>
|
|
||||||
<dependency>
|
|
||||||
<groupId>org.mockito</groupId>
|
|
||||||
<artifactId>mockito-core</artifactId>
|
|
||||||
<scope>test</scope>
|
|
||||||
</dependency>
|
|
||||||
</dependencies>
|
|
||||||
|
|
||||||
<build>
|
The MIT License
|
||||||
<plugins>
|
Copyright (c) 2014-2016 Ilkka Seppälä
|
||||||
<plugin>
|
|
||||||
<groupId>org.springframework.boot</groupId>
|
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||||
<artifactId>spring-boot-maven-plugin</artifactId>
|
of this software and associated documentation files (the "Software"), to deal
|
||||||
<executions>
|
in the Software without restriction, including without limitation the rights
|
||||||
<execution>
|
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||||
<goals>
|
copies of the Software, and to permit persons to whom the Software is
|
||||||
<goal>repackage</goal>
|
furnished to do so, subject to the following conditions:
|
||||||
</goals>
|
|
||||||
</execution>
|
The above copyright notice and this permission notice shall be included in
|
||||||
</executions>
|
all copies or substantial portions of the Software.
|
||||||
</plugin>
|
|
||||||
<!-- Maven assembly plugin is invoked with default setting which we have
|
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||||
in parent pom and specifying the class having main method -->
|
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||||
<plugin>
|
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||||
<groupId>org.apache.maven.plugins</groupId>
|
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||||
<artifactId>maven-assembly-plugin</artifactId>
|
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||||
<executions>
|
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||||
<execution>
|
THE SOFTWARE.
|
||||||
<configuration>
|
|
||||||
<archive>
|
-->
|
||||||
<manifest>
|
<project xmlns="http://maven.apache.org/POM/4.0.0"
|
||||||
<mainClass>com.iluwatar.aggregator.microservices.App</mainClass>
|
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||||
</manifest>
|
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||||
</archive>
|
<parent>
|
||||||
</configuration>
|
<artifactId>aggregator-microservices</artifactId>
|
||||||
</execution>
|
<groupId>com.iluwatar</groupId>
|
||||||
</executions>
|
<version>1.21.0-SNAPSHOT</version>
|
||||||
</plugin>
|
</parent>
|
||||||
</plugins>
|
<modelVersion>4.0.0</modelVersion>
|
||||||
</build>
|
|
||||||
</project>
|
<artifactId>aggregator-service</artifactId>
|
||||||
|
<packaging>jar</packaging>
|
||||||
|
|
||||||
|
<dependencyManagement>
|
||||||
|
<dependencies>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.springframework.boot</groupId>
|
||||||
|
<artifactId>spring-boot-dependencies</artifactId>
|
||||||
|
</dependency>
|
||||||
|
</dependencies>
|
||||||
|
</dependencyManagement>
|
||||||
|
<dependencies>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.springframework</groupId>
|
||||||
|
<artifactId>spring-webmvc</artifactId>
|
||||||
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.springframework.boot</groupId>
|
||||||
|
<artifactId>spring-boot-starter-web</artifactId>
|
||||||
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.junit.jupiter</groupId>
|
||||||
|
<artifactId>junit-jupiter-api</artifactId>
|
||||||
|
<scope>test</scope>
|
||||||
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.junit.jupiter</groupId>
|
||||||
|
<artifactId>junit-jupiter-engine</artifactId>
|
||||||
|
<scope>test</scope>
|
||||||
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.mockito</groupId>
|
||||||
|
<artifactId>mockito-core</artifactId>
|
||||||
|
<scope>test</scope>
|
||||||
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.apache.httpcomponents</groupId>
|
||||||
|
<artifactId>httpclient</artifactId>
|
||||||
|
</dependency>
|
||||||
|
</dependencies>
|
||||||
|
|
||||||
|
<build>
|
||||||
|
<plugins>
|
||||||
|
<plugin>
|
||||||
|
<groupId>org.springframework.boot</groupId>
|
||||||
|
<artifactId>spring-boot-maven-plugin</artifactId>
|
||||||
|
<version>${spring-boot.version}</version>
|
||||||
|
<executions>
|
||||||
|
<execution>
|
||||||
|
<goals>
|
||||||
|
<goal>repackage</goal>
|
||||||
|
</goals>
|
||||||
|
</execution>
|
||||||
|
</executions>
|
||||||
|
</plugin>
|
||||||
|
</plugins>
|
||||||
|
</build>
|
||||||
|
</project>
|
@ -1,17 +1,17 @@
|
|||||||
/*
|
/**
|
||||||
* The MIT License
|
* The MIT License
|
||||||
* Copyright © 2014-2019 Ilkka Seppälä
|
* Copyright (c) 2014-2016 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,16 @@
|
|||||||
* 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 java.util.Objects.requireNonNullElse;
|
|
||||||
|
|
||||||
import javax.annotation.Resource;
|
|
||||||
import org.springframework.web.bind.annotation.RequestMapping;
|
import org.springframework.web.bind.annotation.RequestMapping;
|
||||||
import org.springframework.web.bind.annotation.RequestMethod;
|
|
||||||
import org.springframework.web.bind.annotation.RestController;
|
import org.springframework.web.bind.annotation.RestController;
|
||||||
|
|
||||||
|
import javax.annotation.Resource;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* The aggregator aggregates calls on various micro-services, collects data and further publishes
|
* The aggregator aggregates calls on various micro-services, collects
|
||||||
* them under a REST endpoint.
|
* data and further publishes them under a REST endpoint.
|
||||||
*/
|
*/
|
||||||
@RestController
|
@RestController
|
||||||
public class Aggregator {
|
public class Aggregator {
|
||||||
@ -50,19 +47,11 @@ public class Aggregator {
|
|||||||
*
|
*
|
||||||
* @return a Product.
|
* @return a Product.
|
||||||
*/
|
*/
|
||||||
@RequestMapping(path = "/product", method = RequestMethod.GET)
|
@RequestMapping("/product")
|
||||||
public Product getProduct() {
|
public Product getProduct() {
|
||||||
|
Product product = new Product();
|
||||||
var product = new Product();
|
product.setTitle(informationClient.getProductTitle());
|
||||||
var productTitle = informationClient.getProductTitle();
|
product.setProductInventories(inventoryClient.getProductInventories());
|
||||||
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 © 2014-2019 Ilkka Seppälä
|
* Copyright (c) 2014-2016 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,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.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 © 2014-2019 Ilkka Seppälä
|
* Copyright (c) 2014-2016 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,7 +20,6 @@
|
|||||||
* 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 © 2014-2019 Ilkka Seppälä
|
* Copyright (c) 2014-2016 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,7 +20,6 @@
|
|||||||
* 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 © 2014-2019 Ilkka Seppälä
|
* Copyright (c) 2014-2016 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,18 +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 java.io.IOException;
|
import org.apache.http.client.methods.CloseableHttpResponse;
|
||||||
import java.net.URI;
|
import org.apache.http.client.methods.HttpGet;
|
||||||
import java.net.http.HttpClient;
|
import org.apache.http.impl.client.CloseableHttpClient;
|
||||||
import java.net.http.HttpRequest;
|
import org.apache.http.impl.client.HttpClients;
|
||||||
import java.net.http.HttpResponse;
|
import org.apache.http.util.EntityUtils;
|
||||||
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.
|
||||||
*/
|
*/
|
||||||
@ -42,19 +43,15 @@ public class ProductInformationClientImpl implements ProductInformationClient {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public String getProductTitle() {
|
public String getProductTitle() {
|
||||||
var request = HttpRequest.newBuilder()
|
String response = null;
|
||||||
.GET()
|
try (CloseableHttpClient httpClient = HttpClients.createDefault()) {
|
||||||
.uri(URI.create("http://localhost:51515/information"))
|
HttpGet httpGet = new HttpGet("http://localhost:51515/information");
|
||||||
.build();
|
try (CloseableHttpResponse httpResponse = httpClient.execute(httpGet)) {
|
||||||
var client = HttpClient.newHttpClient();
|
response = EntityUtils.toString(httpResponse.getEntity());
|
||||||
try {
|
}
|
||||||
var httpResponse = client.send(request, HttpResponse.BodyHandlers.ofString());
|
} catch (IOException e) {
|
||||||
return httpResponse.body();
|
LOGGER.error("Exception caught.", e);
|
||||||
} catch (IOException ioe) {
|
|
||||||
LOGGER.error("IOException Occurred", ioe);
|
|
||||||
} catch (InterruptedException ie) {
|
|
||||||
LOGGER.error("InterruptedException Occurred", ie);
|
|
||||||
}
|
}
|
||||||
return null;
|
return response;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,17 +1,17 @@
|
|||||||
/*
|
/**
|
||||||
* The MIT License
|
* The MIT License
|
||||||
* Copyright © 2014-2019 Ilkka Seppälä
|
* Copyright (c) 2014-2016 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,7 +20,6 @@
|
|||||||
* 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;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -28,5 +27,5 @@ package com.iluwatar.aggregator.microservices;
|
|||||||
*/
|
*/
|
||||||
public interface ProductInventoryClient {
|
public interface ProductInventoryClient {
|
||||||
|
|
||||||
Integer getProductInventories();
|
int getProductInventories();
|
||||||
}
|
}
|
||||||
|
@ -1,17 +1,17 @@
|
|||||||
/*
|
/**
|
||||||
* The MIT License
|
* The MIT License
|
||||||
* Copyright © 2014-2019 Ilkka Seppälä
|
* Copyright (c) 2014-2016 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,18 +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 java.io.IOException;
|
import org.apache.http.client.methods.CloseableHttpResponse;
|
||||||
import java.net.URI;
|
import org.apache.http.client.methods.HttpGet;
|
||||||
import java.net.http.HttpClient;
|
import org.apache.http.impl.client.CloseableHttpClient;
|
||||||
import java.net.http.HttpRequest;
|
import org.apache.http.impl.client.HttpClients;
|
||||||
import java.net.http.HttpResponse;
|
import org.apache.http.util.EntityUtils;
|
||||||
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.
|
||||||
*/
|
*/
|
||||||
@ -41,26 +42,16 @@ 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 Integer getProductInventories() {
|
public int getProductInventories() {
|
||||||
var response = "";
|
String response = "0";
|
||||||
|
try (CloseableHttpClient httpClient = HttpClients.createDefault()) {
|
||||||
var request = HttpRequest.newBuilder()
|
HttpGet httpGet = new HttpGet("http://localhost:51516/inventories");
|
||||||
.GET()
|
try (CloseableHttpResponse httpResponse = httpClient.execute(httpGet)) {
|
||||||
.uri(URI.create("http://localhost:51516/inventories"))
|
response = EntityUtils.toString(httpResponse.getEntity());
|
||||||
.build();
|
}
|
||||||
var client = HttpClient.newHttpClient();
|
} catch (IOException e) {
|
||||||
try {
|
LOGGER.error("Exception caught.", e);
|
||||||
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 © 2014-2019 Ilkka Seppälä
|
# Copyright (c) 2014-2016 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,4 +20,5 @@
|
|||||||
# 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 © 2014-2019 Ilkka Seppälä
|
* Copyright (c) 2014-2016 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,18 +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.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
|
||||||
*/
|
*/
|
||||||
@ -56,13 +55,13 @@ public class AggregatorTest {
|
|||||||
*/
|
*/
|
||||||
@Test
|
@Test
|
||||||
public void testGetProduct() {
|
public void testGetProduct() {
|
||||||
var title = "The Product Title.";
|
String title = "The Product Title.";
|
||||||
var inventories = 5;
|
int inventories = 5;
|
||||||
|
|
||||||
when(informationClient.getProductTitle()).thenReturn(title);
|
when(informationClient.getProductTitle()).thenReturn(title);
|
||||||
when(inventoryClient.getProductInventories()).thenReturn(inventories);
|
when(inventoryClient.getProductInventories()).thenReturn(inventories);
|
||||||
|
|
||||||
var testProduct = aggregator.getProduct();
|
Product testProduct = aggregator.getProduct();
|
||||||
|
|
||||||
assertEquals(title, testProduct.getTitle());
|
assertEquals(title, testProduct.getTitle());
|
||||||
assertEquals(inventories, testProduct.getProductInventories());
|
assertEquals(inventories, testProduct.getProductInventories());
|
||||||
|
@ -1,2 +0,0 @@
|
|||||||
@startuml
|
|
||||||
@enduml
|
|
@ -1,12 +0,0 @@
|
|||||||
@startuml
|
|
||||||
package com.iluwatar.information.microservice {
|
|
||||||
class InformationApplication {
|
|
||||||
+ InformationApplication()
|
|
||||||
+ main(args : String[]) {static}
|
|
||||||
}
|
|
||||||
class InformationController {
|
|
||||||
+ InformationController()
|
|
||||||
+ getProductTitle() : String
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@enduml
|
|
@ -1,76 +1,84 @@
|
|||||||
<?xml version="1.0" encoding="UTF-8"?>
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
<!-- 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
|
The MIT License
|
||||||
without restriction, including without limitation the rights to use, copy,
|
Copyright (c) 2014-2016 Ilkka Seppälä
|
||||||
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,
|
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||||
subject to the following conditions: The above copyright notice and this
|
of this software and associated documentation files (the "Software"), to deal
|
||||||
permission notice shall be included in all copies or substantial portions
|
in the Software without restriction, including without limitation the rights
|
||||||
of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY
|
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||||
KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
copies of the Software, and to permit persons to whom the Software is
|
||||||
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
|
furnished to do so, subject to the following conditions:
|
||||||
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,
|
The above copyright notice and this permission notice shall be included in
|
||||||
ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
|
all copies or substantial portions of the Software.
|
||||||
DEALINGS IN THE SOFTWARE. -->
|
|
||||||
|
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||||
|
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||||
|
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||||
|
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||||
|
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||||
|
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||||
|
THE SOFTWARE.
|
||||||
|
|
||||||
|
-->
|
||||||
<project xmlns="http://maven.apache.org/POM/4.0.0"
|
<project xmlns="http://maven.apache.org/POM/4.0.0"
|
||||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||||
<parent>
|
<parent>
|
||||||
<artifactId>aggregator-microservices</artifactId>
|
<artifactId>aggregator-microservices</artifactId>
|
||||||
<groupId>com.iluwatar</groupId>
|
<groupId>com.iluwatar</groupId>
|
||||||
<version>1.23.0-SNAPSHOT</version>
|
<version>1.21.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>
|
||||||
|
|
||||||
<dependencies>
|
<dependencyManagement>
|
||||||
<dependency>
|
<dependencies>
|
||||||
<groupId>org.springframework</groupId>
|
<dependency>
|
||||||
<artifactId>spring-webmvc</artifactId>
|
<groupId>org.springframework.boot</groupId>
|
||||||
</dependency>
|
<artifactId>spring-boot-dependencies</artifactId>
|
||||||
<dependency>
|
</dependency>
|
||||||
<groupId>org.springframework.boot</groupId>
|
</dependencies>
|
||||||
<artifactId>spring-boot-starter-web</artifactId>
|
</dependencyManagement>
|
||||||
</dependency>
|
<dependencies>
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>org.junit.jupiter</groupId>
|
<groupId>org.springframework</groupId>
|
||||||
<artifactId>junit-jupiter-engine</artifactId>
|
<artifactId>spring-webmvc</artifactId>
|
||||||
<scope>test</scope>
|
</dependency>
|
||||||
</dependency>
|
<dependency>
|
||||||
</dependencies>
|
<groupId>org.springframework.boot</groupId>
|
||||||
|
<artifactId>spring-boot-starter-web</artifactId>
|
||||||
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.junit.jupiter</groupId>
|
||||||
|
<artifactId>junit-jupiter-api</artifactId>
|
||||||
|
<scope>test</scope>
|
||||||
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.junit.jupiter</groupId>
|
||||||
|
<artifactId>junit-jupiter-engine</artifactId>
|
||||||
|
<scope>test</scope>
|
||||||
|
</dependency>
|
||||||
|
</dependencies>
|
||||||
|
|
||||||
<build>
|
<build>
|
||||||
<plugins>
|
<plugins>
|
||||||
<plugin>
|
<plugin>
|
||||||
<groupId>org.springframework.boot</groupId>
|
<groupId>org.springframework.boot</groupId>
|
||||||
<artifactId>spring-boot-maven-plugin</artifactId>
|
<artifactId>spring-boot-maven-plugin</artifactId>
|
||||||
<executions>
|
<version>${spring-boot.version}</version>
|
||||||
<execution>
|
<executions>
|
||||||
<goals>
|
<execution>
|
||||||
<goal>repackage</goal>
|
<goals>
|
||||||
</goals>
|
<goal>repackage</goal>
|
||||||
</execution>
|
</goals>
|
||||||
</executions>
|
</execution>
|
||||||
</plugin>
|
</executions>
|
||||||
<plugin>
|
</plugin>
|
||||||
<groupId>org.apache.maven.plugins</groupId>
|
</plugins>
|
||||||
<artifactId>maven-assembly-plugin</artifactId>
|
</build>
|
||||||
<executions>
|
|
||||||
<execution>
|
|
||||||
<configuration>
|
|
||||||
<archive>
|
|
||||||
<manifest>
|
|
||||||
<mainClass>com.iluwatar.information.microservices.InformationApplication</mainClass>
|
|
||||||
</manifest>
|
|
||||||
</archive>
|
|
||||||
</configuration>
|
|
||||||
</execution>
|
|
||||||
</executions>
|
|
||||||
</plugin>
|
|
||||||
</plugins>
|
|
||||||
</build>
|
|
||||||
</project>
|
</project>
|
||||||
|
@ -1,17 +1,17 @@
|
|||||||
/*
|
/**
|
||||||
* The MIT License
|
* The MIT License
|
||||||
* Copyright © 2014-2019 Ilkka Seppälä
|
* Copyright (c) 2014-2016 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,7 +20,6 @@
|
|||||||
* 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 © 2014-2019 Ilkka Seppälä
|
* Copyright (c) 2014-2016 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,7 +20,6 @@
|
|||||||
* 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;
|
||||||
@ -28,7 +27,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 © 2014-2019 Ilkka Seppälä
|
# Copyright (c) 2014-2016 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,4 +20,5 @@
|
|||||||
# 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 © 2014-2019 Ilkka Seppälä
|
* Copyright (c) 2014-2016 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,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.information.microservice;
|
package com.iluwatar.information.microservice;
|
||||||
|
|
||||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
|
||||||
|
|
||||||
import org.junit.jupiter.api.Test;
|
import org.junit.jupiter.api.Test;
|
||||||
|
|
||||||
|
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Test for Information Rest Controller
|
* Test for Information Rest Controller
|
||||||
*/
|
*/
|
||||||
@ -34,8 +33,10 @@ public class InformationControllerTest {
|
|||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void shouldGetProductTitle() {
|
public void shouldGetProductTitle() {
|
||||||
var infoController = new InformationController();
|
InformationController infoController = new InformationController();
|
||||||
var title = infoController.getProductTitle();
|
|
||||||
|
String title = infoController.getProductTitle();
|
||||||
|
|
||||||
assertEquals("The Product Title.", title);
|
assertEquals("The Product Title.", title);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1,12 +0,0 @@
|
|||||||
@startuml
|
|
||||||
package com.iluwatar.inventory.microservice {
|
|
||||||
class InventoryApplication {
|
|
||||||
+ InventoryApplication()
|
|
||||||
+ main(args : String[]) {static}
|
|
||||||
}
|
|
||||||
class InventoryController {
|
|
||||||
+ InventoryController()
|
|
||||||
+ getProductInventories() : int
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@enduml
|
|
@ -1,75 +1,84 @@
|
|||||||
<?xml version="1.0" encoding="UTF-8"?>
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
<!-- 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
|
The MIT License
|
||||||
without restriction, including without limitation the rights to use, copy,
|
Copyright (c) 2014-2016 Ilkka Seppälä
|
||||||
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,
|
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||||
subject to the following conditions: The above copyright notice and this
|
of this software and associated documentation files (the "Software"), to deal
|
||||||
permission notice shall be included in all copies or substantial portions
|
in the Software without restriction, including without limitation the rights
|
||||||
of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY
|
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||||
KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
copies of the Software, and to permit persons to whom the Software is
|
||||||
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
|
furnished to do so, subject to the following conditions:
|
||||||
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,
|
The above copyright notice and this permission notice shall be included in
|
||||||
ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
|
all copies or substantial portions of the Software.
|
||||||
DEALINGS IN THE SOFTWARE. -->
|
|
||||||
|
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||||
|
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||||
|
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||||
|
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||||
|
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||||
|
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||||
|
THE SOFTWARE.
|
||||||
|
|
||||||
|
-->
|
||||||
<project xmlns="http://maven.apache.org/POM/4.0.0"
|
<project xmlns="http://maven.apache.org/POM/4.0.0"
|
||||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||||
<parent>
|
<parent>
|
||||||
<artifactId>aggregator-microservices</artifactId>
|
<artifactId>aggregator-microservices</artifactId>
|
||||||
<groupId>com.iluwatar</groupId>
|
<groupId>com.iluwatar</groupId>
|
||||||
<version>1.23.0-SNAPSHOT</version>
|
<version>1.21.0-SNAPSHOT</version>
|
||||||
</parent>
|
</parent>
|
||||||
<modelVersion>4.0.0</modelVersion>
|
<modelVersion>4.0.0</modelVersion>
|
||||||
<artifactId>inventory-microservice</artifactId>
|
|
||||||
|
|
||||||
<packaging>jar</packaging>
|
<artifactId>inventory-microservice</artifactId>
|
||||||
<dependencies>
|
<packaging>jar</packaging>
|
||||||
<dependency>
|
|
||||||
<groupId>org.springframework</groupId>
|
|
||||||
<artifactId>spring-webmvc</artifactId>
|
|
||||||
</dependency>
|
|
||||||
<dependency>
|
|
||||||
<groupId>org.springframework.boot</groupId>
|
|
||||||
<artifactId>spring-boot-starter-web</artifactId>
|
|
||||||
</dependency>
|
|
||||||
<dependency>
|
|
||||||
<groupId>org.junit.jupiter</groupId>
|
|
||||||
<artifactId>junit-jupiter-engine</artifactId>
|
|
||||||
<scope>test</scope>
|
|
||||||
</dependency>
|
|
||||||
</dependencies>
|
|
||||||
|
|
||||||
<build>
|
<dependencyManagement>
|
||||||
<plugins>
|
<dependencies>
|
||||||
<plugin>
|
<dependency>
|
||||||
<groupId>org.springframework.boot</groupId>
|
<groupId>org.springframework.boot</groupId>
|
||||||
<artifactId>spring-boot-maven-plugin</artifactId>
|
<artifactId>spring-boot-dependencies</artifactId>
|
||||||
<executions>
|
</dependency>
|
||||||
<execution>
|
</dependencies>
|
||||||
<goals>
|
</dependencyManagement>
|
||||||
<goal>repackage</goal>
|
<dependencies>
|
||||||
</goals>
|
<dependency>
|
||||||
</execution>
|
<groupId>org.springframework</groupId>
|
||||||
</executions>
|
<artifactId>spring-webmvc</artifactId>
|
||||||
</plugin>
|
</dependency>
|
||||||
<plugin>
|
<dependency>
|
||||||
<groupId>org.apache.maven.plugins</groupId>
|
<groupId>org.springframework.boot</groupId>
|
||||||
<artifactId>maven-assembly-plugin</artifactId>
|
<artifactId>spring-boot-starter-web</artifactId>
|
||||||
<executions>
|
</dependency>
|
||||||
<execution>
|
<dependency>
|
||||||
<configuration>
|
<groupId>org.junit.jupiter</groupId>
|
||||||
<archive>
|
<artifactId>junit-jupiter-api</artifactId>
|
||||||
<manifest>
|
<scope>test</scope>
|
||||||
<mainClass>com.iluwatar.inventory.microservices.InventoryApplication</mainClass>
|
</dependency>
|
||||||
</manifest>
|
<dependency>
|
||||||
</archive>
|
<groupId>org.junit.jupiter</groupId>
|
||||||
</configuration>
|
<artifactId>junit-jupiter-engine</artifactId>
|
||||||
</execution>
|
<scope>test</scope>
|
||||||
</executions>
|
</dependency>
|
||||||
</plugin>
|
</dependencies>
|
||||||
</plugins>
|
|
||||||
</build>
|
<build>
|
||||||
</project>
|
<plugins>
|
||||||
|
<plugin>
|
||||||
|
<groupId>org.springframework.boot</groupId>
|
||||||
|
<artifactId>spring-boot-maven-plugin</artifactId>
|
||||||
|
<version>${spring-boot.version}</version>
|
||||||
|
<executions>
|
||||||
|
<execution>
|
||||||
|
<goals>
|
||||||
|
<goal>repackage</goal>
|
||||||
|
</goals>
|
||||||
|
</execution>
|
||||||
|
</executions>
|
||||||
|
</plugin>
|
||||||
|
</plugins>
|
||||||
|
</build>
|
||||||
|
</project>
|
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user