Compare commits
216 Commits
Author | SHA1 | Date | |
---|---|---|---|
4f56f7b097 | |||
e25cbad811 | |||
6caea8557b | |||
e05592b522 | |||
f08af14555 | |||
c7f4311706 | |||
37da470178 | |||
7adefc89ba | |||
b27d6dff66 | |||
bf5ba44260 | |||
8603333e5d | |||
7cc93de672 | |||
c2e750aca1 | |||
59c32d3937 | |||
dc4f07eb81 | |||
d24a12a9b2 | |||
62b2f8e9a4 | |||
409ff027b8 | |||
98326a1e5e | |||
3d95696078 | |||
321f5d6c5a | |||
7b61395bc1 | |||
ab2aad3226 | |||
4e40cc3888 | |||
77e14f0069 | |||
4a49f82f23 | |||
ba1d3a0fbf | |||
d627b7af6b | |||
91b2379fd0 | |||
32f9cf3ab1 | |||
a1ede8980f | |||
72733acfc6 | |||
d00bfae5ee | |||
0b834128b9 | |||
89e0e3e73e | |||
d7526fc7c0 | |||
cf10bd1d05 | |||
5ad99be224 | |||
1a55f3a420 | |||
bd82387fc1 | |||
3398badd30 | |||
033dce8387 | |||
e95cfe9292 | |||
150c4e0d5d | |||
c8db19efe2 | |||
2fec4f891b | |||
adf131b784 | |||
77d45c35e0 | |||
2f84369003 | |||
83f1ac7489 | |||
03e187743c | |||
bd83fa28bb | |||
30ca1ea1fb | |||
4c5f9ae44c | |||
3d642cdad7 | |||
b6beffec2e | |||
1987bdb962 | |||
bea1a0d6d9 | |||
d6719729b9 | |||
03f0a2112b | |||
0f75a0e239 | |||
0c062d3f55 | |||
248ea5191a | |||
39b183d569 | |||
02bd15db90 | |||
317a599c3b | |||
7176b86146 | |||
2ea9bfef19 | |||
40042ae392 | |||
e25ef1b0f8 | |||
ca4a4ec07b | |||
e0e51324db | |||
e4458c1b17 | |||
30e09a8dd9 | |||
f987c7253b | |||
ad714294dd | |||
623c2081cd | |||
542a832a66 | |||
444eb07e26 | |||
fd8c05846f | |||
47709e24b9 | |||
09d3a82884 | |||
997bfba3b2 | |||
6326c1742d | |||
2e2707862a | |||
1d9aff4071 | |||
5611f26c77 | |||
d0cdf84936 | |||
a375b2d28b | |||
fcfdbe71f5 | |||
52c483f1d0 | |||
c72faeb67e | |||
299d612b9b | |||
9d4c3154b1 | |||
a3b1265921 | |||
dca68511e8 | |||
42a1dc69ca | |||
47e1cd710c | |||
b3d1c2b2ed | |||
3e20a2afa8 | |||
4c938ab8a5 | |||
8f6f171a3f | |||
b4dcec45ef | |||
dec5ff22fc | |||
7b323e9cb4 | |||
a0af0a1a76 | |||
7c6e1fc3d8 | |||
45cecf68c4 | |||
d7580f5530 | |||
58b8a06561 | |||
befe509582 | |||
17b889c31d | |||
52192de909 | |||
8b020837ea | |||
b369812511 | |||
52d6e20ad9 | |||
749880e3b9 | |||
41593774c6 | |||
0bc722f797 | |||
fcadb223ce | |||
c842f88eb7 | |||
6d516d5124 | |||
9544dd321a | |||
d623e9c8fd | |||
e27de33f75 | |||
7ac7e3b097 | |||
fefb51c46e | |||
58547fae8f | |||
30676f8d4f | |||
483db04cef | |||
fac10dc454 | |||
982f9f5e31 | |||
74d1823078 | |||
f59f18091e | |||
5f033be54f | |||
df911baf36 | |||
32e7181478 | |||
c8a750df49 | |||
191078735f | |||
fa6a6006c3 | |||
a49dbefb56 | |||
fb0617e9c5 | |||
84fd2348ea | |||
f0ff8ad4b3 | |||
c3184255bf | |||
bdacfe30c1 | |||
30363cbb7f | |||
3d8c64d76d | |||
78fcd63271 | |||
30bc25b5bf | |||
6b1356a160 | |||
3731d26f6d | |||
cec9a99410 | |||
9fbb085985 | |||
6e496e7c86 | |||
e3e0e32e92 | |||
531158c836 | |||
6fe01e73b2 | |||
69c9374669 | |||
a57a71b09c | |||
719f80a0d6 | |||
2c82bd9450 | |||
5948a82cf2 | |||
a0151826ad | |||
33fe90d177 | |||
25cacdbbc9 | |||
323e4c8751 | |||
de78490d29 | |||
9059d2b96c | |||
dbca06a9e7 | |||
ca14e8ddad | |||
3dc370e2d1 | |||
4181514c65 | |||
df69a8f986 | |||
885d5bb7dd | |||
1b74e0ff67 | |||
94f80d1868 | |||
c837ffe234 | |||
2edc1898b1 | |||
29fc56002a | |||
0643289c74 | |||
2c0a5e8703 | |||
a3372febd0 | |||
7df65adbfc | |||
d7dd8d42af | |||
4e7a8fdaca | |||
6e59d9554f | |||
e85308fbc4 | |||
10a94cc91c | |||
e2d8079b36 | |||
8367c83aca | |||
875e5b872c | |||
1fa617d08d | |||
6c1f025d0f | |||
601964a2d1 | |||
1884df525b | |||
507b89d5e4 | |||
02d6754804 | |||
e709a196bd | |||
33b41f872e | |||
898f3a428c | |||
0b46a9985d | |||
2aa20b7445 | |||
cfb0fafc7d | |||
119d264779 | |||
9e857d7dd6 | |||
d9a1d1cef9 | |||
3ad36020aa | |||
e1c0731f7e | |||
b8b94b697a | |||
fc70a706c6 | |||
9a85dfe9df | |||
321e9d4191 | |||
b4aeca3aa0 | |||
3ef0921f20 | |||
eb396217d0 |
CONTRIBUTING.MDREADME.md
abstract-factory
adapter
async-method-invocation
bridge
builder
business-delegate
caching
callback
chain
checkstyle-suppressions.xmlcheckstyle.xmlcommand
composite
dao
decorator
delegation
dependency-injection
double-checked-locking
double-dispatch
event-aggregator
event-driven-architecture
exclude-pmd.propertiesexecute-around
facade
factory-method
faq.mdfeature-toggle
etc
index.mdpom.xmlsrc
main
java
com
iluwatar
featuretoggle
test
java
com
iluwatar
featuretoggle
pattern
propertiesversion
tieredversion
user
fluentinterface
index.mdpom.xml
src
main
java
com
iluwatar
fluentinterface
app
fluentiterable
test
java
com
iluwatar
fluentinterface
app
fluentiterable
flux
flyweight
front-controller
half-sync-half-async
intercepting-filter
interpreter
iterator
layers
lazy-loading
mediator
memento
message-channel
model-view-controller
model-view-presenter
monostate
multiton
naked-objects
dom
fixture
index.mdintegtests
pom.xmlwebapp
null-object
object-pool
observer
poison-pill
pom.xmlprivate-class-data
producer-consumer
property
prototype
proxy
publish-subscribe
reactor
reader-writer-lock
repository
resource-acquisition-is-initialization
index.mdpom.xml
src
main
java
com
iluwatar
resource
acquisition
is
initialization
test
java
com
iluwatar
resource
acquisition
is
initialization
servant
service-layer
bin
index.mdpom.xmlsrc
main
java
com
iluwatar
test
java
com
iluwatar
servicelayer
app
common
magic
spell
spellbook
wizard
service-locator
singleton
specification
state
step-builder
strategy
template-method
thread-pool
tolerant-reader
twin
update-ghpages.shvisitor
4
CONTRIBUTING.MD
Normal file
4
CONTRIBUTING.MD
Normal file
@ -0,0 +1,4 @@
|
|||||||
|
This is great you have something to contribute!
|
||||||
|
|
||||||
|
Before going any further please read the [wiki](https://github.com/iluwatar/java-design-patterns/wiki)
|
||||||
|
with conventions and rules we used for this project.
|
17
README.md
17
README.md
@ -2,11 +2,10 @@
|
|||||||
that smart and dearly wants an empty line before a heading to be able to
|
that smart and dearly wants an empty line before a heading to be able to
|
||||||
display it as such, e.g. website) -->
|
display it as such, e.g. website) -->
|
||||||
|
|
||||||
# Design pattern samples in Java
|
# Design patterns implemented in Java
|
||||||
|
|
||||||
[](https://travis-ci.org/iluwatar/java-design-patterns)
|
[](https://travis-ci.org/iluwatar/java-design-patterns)
|
||||||
[](https://coveralls.io/r/iluwatar/java-design-patterns?branch=master)
|
[](https://coveralls.io/r/iluwatar/java-design-patterns?branch=master)
|
||||||
[](https://scan.coverity.com/projects/5634)
|
|
||||||
[](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)
|
||||||
|
|
||||||
# Introduction
|
# Introduction
|
||||||
@ -40,19 +39,7 @@ patterns by any of the following approaches
|
|||||||
|
|
||||||
# How to contribute
|
# How to contribute
|
||||||
|
|
||||||
If you are willing to contribute to the project you will find the relevant information in our [developer wiki](https://github.com/iluwatar/java-design-patterns/wiki).
|
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).
|
||||||
|
|
||||||
# Credits
|
|
||||||
|
|
||||||
* [Effective Java (2nd Edition)](http://www.amazon.com/Effective-Java-Edition-Joshua-Bloch/dp/0321356683)
|
|
||||||
* [Java Generics and Collections](http://www.amazon.com/Java-Generics-Collections-Maurice-Naftalin/dp/0596527756/)
|
|
||||||
* [Let's Modify the Objects-First Approach into Design-Patterns-First](http://edu.pecinovsky.cz/papers/2006_ITiCSE_Design_Patterns_First.pdf)
|
|
||||||
* [Pattern Languages of Program Design](http://www.amazon.com/Pattern-Languages-Program-Design-Coplien/dp/0201607344/ref=sr_1_1)
|
|
||||||
* [Presentation Tier Patterns](http://www.javagyan.com/tutorials/corej2eepatterns/presentation-tier-patterns)
|
|
||||||
* [Functional Programming in Java: Harnessing the Power of Java 8 Lambda Expressions](http://www.amazon.com/Functional-Programming-Java-Harnessing-Expressions/dp/1937785467/ref=sr_1_1)
|
|
||||||
* [Patterns of Enterprise Application Architecture](http://www.amazon.com/Patterns-Enterprise-Application-Architecture-Martin/dp/0321127420)
|
|
||||||
* [Spring Data](http://www.amazon.com/Spring-Data-Mark-Pollack/dp/1449323952/ref=sr_1_1)
|
|
||||||
* [J2EE Design Patterns](http://www.amazon.com/J2EE-Design-Patterns-William-Crawford/dp/0596004273/ref=sr_1_2)
|
|
||||||
|
|
||||||
# License
|
# License
|
||||||
|
|
||||||
|
@ -7,26 +7,30 @@ categories: Creational
|
|||||||
tags:
|
tags:
|
||||||
- Java
|
- Java
|
||||||
- Gang Of Four
|
- Gang Of Four
|
||||||
|
- Difficulty-Intermediate
|
||||||
---
|
---
|
||||||
|
|
||||||
**Also known as:** Kit
|
## Also known as
|
||||||
|
Kit
|
||||||
|
|
||||||
**Intent:** Provide an interface for creating families of related or dependent
|
## Intent
|
||||||
|
Provide an interface for creating families of related or dependent
|
||||||
objects without specifying their concrete classes.
|
objects without specifying their concrete classes.
|
||||||
|
|
||||||

|

|
||||||
|
|
||||||
**Applicability:** Use the Abstract Factory pattern when
|
## 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 independent of how its products are created, composed and represented
|
||||||
* a system should be configured with one of multiple families of products
|
* 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
|
* 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
|
* you want to provide a class library of products, and you want to reveal just their interfaces, not their implementations
|
||||||
|
|
||||||
**Real world examples:**
|
## Real world examples
|
||||||
|
|
||||||
* [javax.xml.parsers.DocumentBuilderFactory](http://docs.oracle.com/javase/8/docs/api/javax/xml/parsers/DocumentBuilderFactory.html)
|
* [javax.xml.parsers.DocumentBuilderFactory](http://docs.oracle.com/javase/8/docs/api/javax/xml/parsers/DocumentBuilderFactory.html)
|
||||||
|
|
||||||
**Credits**
|
## Credits
|
||||||
|
|
||||||
* [Design Patterns: Elements of Reusable Object-Oriented Software](http://www.amazon.com/Design-Patterns-Elements-Reusable-Object-Oriented/dp/0201633612)
|
* [Design Patterns: Elements of Reusable Object-Oriented Software](http://www.amazon.com/Design-Patterns-Elements-Reusable-Object-Oriented/dp/0201633612)
|
||||||
|
@ -1,11 +1,35 @@
|
|||||||
<?xml version="1.0"?>
|
<?xml version="1.0"?>
|
||||||
|
<!--
|
||||||
|
|
||||||
|
The MIT License
|
||||||
|
Copyright (c) 2014 Ilkka Seppälä
|
||||||
|
|
||||||
|
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||||
|
of this software and associated documentation files (the "Software"), to deal
|
||||||
|
in the Software without restriction, including without limitation the rights
|
||||||
|
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||||
|
copies of the Software, and to permit persons to whom the Software is
|
||||||
|
furnished to do so, subject to the following conditions:
|
||||||
|
|
||||||
|
The above copyright notice and this permission notice shall be included in
|
||||||
|
all copies or substantial portions of the Software.
|
||||||
|
|
||||||
|
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||||
|
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||||
|
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||||
|
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||||
|
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||||
|
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||||
|
THE SOFTWARE.
|
||||||
|
|
||||||
|
-->
|
||||||
<project xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" xmlns="http://maven.apache.org/POM/4.0.0"
|
<project xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" xmlns="http://maven.apache.org/POM/4.0.0"
|
||||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
|
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
|
||||||
<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.8.0</version>
|
<version>1.10.0</version>
|
||||||
</parent>
|
</parent>
|
||||||
<artifactId>abstract-factory</artifactId>
|
<artifactId>abstract-factory</artifactId>
|
||||||
<dependencies>
|
<dependencies>
|
||||||
|
@ -1,20 +1,40 @@
|
|||||||
|
/**
|
||||||
|
* The MIT License
|
||||||
|
* Copyright (c) 2014 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.abstractfactory;
|
package com.iluwatar.abstractfactory;
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
*
|
*
|
||||||
* The Abstract Factory pattern provides a way to encapsulate a group of individual factories that
|
* The Abstract Factory pattern provides a way to encapsulate a group of individual factories that have a common theme
|
||||||
* have a common theme without specifying their concrete classes. In normal usage, the client
|
* without specifying their concrete classes. In normal usage, the client software creates a concrete implementation of
|
||||||
* software creates a concrete implementation of the abstract factory and then uses the generic
|
* the abstract factory and then uses the generic interface of the factory to create the concrete objects that are part
|
||||||
* interface of the factory to create the concrete objects that are part of the theme. The client
|
* of the theme. The client does not know (or care) which concrete objects it gets from each of these internal
|
||||||
* does not know (or care) which concrete objects it gets from each of these internal factories,
|
* factories, since it uses only the generic interfaces of their products. This pattern separates the details of
|
||||||
* since it uses only the generic interfaces of their products. This pattern separates the details
|
* implementation of a set of objects from their general usage and relies on object composition, as object creation is
|
||||||
* of implementation of a set of objects from their general usage and relies on object composition,
|
* implemented in methods exposed in the factory interface.
|
||||||
* as object creation is implemented in methods exposed in the factory interface.
|
|
||||||
* <p>
|
* <p>
|
||||||
* The essence of the Abstract Factory pattern is a factory interface ({@link KingdomFactory}) and
|
* The essence of the Abstract Factory pattern is a factory interface ({@link KingdomFactory}) and its implementations (
|
||||||
* its implementations ({@link ElfKingdomFactory}, {@link OrcKingdomFactory}). The example uses both
|
* {@link ElfKingdomFactory}, {@link OrcKingdomFactory}). The example uses both concrete implementations to create a
|
||||||
* concrete implementations to create a king, a castle and an army.
|
* king, a castle and an army.
|
||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
public class App {
|
public class App {
|
||||||
@ -23,11 +43,8 @@ public class App {
|
|||||||
private Castle castle;
|
private Castle castle;
|
||||||
private Army army;
|
private Army army;
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Creates kingdom
|
* Creates kingdom
|
||||||
*
|
|
||||||
* @param factory
|
|
||||||
*/
|
*/
|
||||||
public void createKingdom(final KingdomFactory factory) {
|
public void createKingdom(final KingdomFactory factory) {
|
||||||
setKing(factory.createKing());
|
setKing(factory.createKing());
|
||||||
@ -47,14 +64,6 @@ public class App {
|
|||||||
return factory.createKing();
|
return factory.createKing();
|
||||||
}
|
}
|
||||||
|
|
||||||
Castle getCastle(final KingdomFactory factory) {
|
|
||||||
return factory.createCastle();
|
|
||||||
}
|
|
||||||
|
|
||||||
Army getArmy(final KingdomFactory factory) {
|
|
||||||
return factory.createArmy();
|
|
||||||
}
|
|
||||||
|
|
||||||
public King getKing() {
|
public King getKing() {
|
||||||
return king;
|
return king;
|
||||||
}
|
}
|
||||||
@ -63,6 +72,10 @@ public class App {
|
|||||||
this.king = king;
|
this.king = king;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Castle getCastle(final KingdomFactory factory) {
|
||||||
|
return factory.createCastle();
|
||||||
|
}
|
||||||
|
|
||||||
public Castle getCastle() {
|
public Castle getCastle() {
|
||||||
return castle;
|
return castle;
|
||||||
}
|
}
|
||||||
@ -71,6 +84,10 @@ public class App {
|
|||||||
this.castle = castle;
|
this.castle = castle;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Army getArmy(final KingdomFactory factory) {
|
||||||
|
return factory.createArmy();
|
||||||
|
}
|
||||||
|
|
||||||
public Army getArmy() {
|
public Army getArmy() {
|
||||||
return army;
|
return army;
|
||||||
}
|
}
|
||||||
@ -79,11 +96,11 @@ public class App {
|
|||||||
this.army = army;
|
this.army = army;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 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) {
|
||||||
|
|
||||||
|
@ -1,3 +1,25 @@
|
|||||||
|
/**
|
||||||
|
* The MIT License
|
||||||
|
* Copyright (c) 2014 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.abstractfactory;
|
package com.iluwatar.abstractfactory;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -1,3 +1,25 @@
|
|||||||
|
/**
|
||||||
|
* The MIT License
|
||||||
|
* Copyright (c) 2014 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.abstractfactory;
|
package com.iluwatar.abstractfactory;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -1,3 +1,25 @@
|
|||||||
|
/**
|
||||||
|
* The MIT License
|
||||||
|
* Copyright (c) 2014 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.abstractfactory;
|
package com.iluwatar.abstractfactory;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -1,3 +1,25 @@
|
|||||||
|
/**
|
||||||
|
* The MIT License
|
||||||
|
* Copyright (c) 2014 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.abstractfactory;
|
package com.iluwatar.abstractfactory;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -1,3 +1,25 @@
|
|||||||
|
/**
|
||||||
|
* The MIT License
|
||||||
|
* Copyright (c) 2014 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.abstractfactory;
|
package com.iluwatar.abstractfactory;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -1,3 +1,25 @@
|
|||||||
|
/**
|
||||||
|
* The MIT License
|
||||||
|
* Copyright (c) 2014 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.abstractfactory;
|
package com.iluwatar.abstractfactory;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -1,3 +1,25 @@
|
|||||||
|
/**
|
||||||
|
* The MIT License
|
||||||
|
* Copyright (c) 2014 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.abstractfactory;
|
package com.iluwatar.abstractfactory;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -1,3 +1,25 @@
|
|||||||
|
/**
|
||||||
|
* The MIT License
|
||||||
|
* Copyright (c) 2014 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.abstractfactory;
|
package com.iluwatar.abstractfactory;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -1,3 +1,25 @@
|
|||||||
|
/**
|
||||||
|
* The MIT License
|
||||||
|
* Copyright (c) 2014 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.abstractfactory;
|
package com.iluwatar.abstractfactory;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -1,3 +1,25 @@
|
|||||||
|
/**
|
||||||
|
* The MIT License
|
||||||
|
* Copyright (c) 2014 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.abstractfactory;
|
package com.iluwatar.abstractfactory;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -1,3 +1,25 @@
|
|||||||
|
/**
|
||||||
|
* The MIT License
|
||||||
|
* Copyright (c) 2014 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.abstractfactory;
|
package com.iluwatar.abstractfactory;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -1,3 +1,25 @@
|
|||||||
|
/**
|
||||||
|
* The MIT License
|
||||||
|
* Copyright (c) 2014 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.abstractfactory;
|
package com.iluwatar.abstractfactory;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
100
abstract-factory/src/test/java/com/iluwatar/abstractfactory/AbstractFactoryTest.java
Normal file
100
abstract-factory/src/test/java/com/iluwatar/abstractfactory/AbstractFactoryTest.java
Normal file
@ -0,0 +1,100 @@
|
|||||||
|
/**
|
||||||
|
* The MIT License
|
||||||
|
* Copyright (c) 2014 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.abstractfactory;
|
||||||
|
|
||||||
|
import static org.junit.Assert.assertEquals;
|
||||||
|
import static org.junit.Assert.assertTrue;
|
||||||
|
|
||||||
|
import org.junit.Before;
|
||||||
|
import org.junit.Test;
|
||||||
|
|
||||||
|
public class AbstractFactoryTest {
|
||||||
|
|
||||||
|
private App app = new App();
|
||||||
|
private KingdomFactory elfFactory;
|
||||||
|
private KingdomFactory orcFactory;
|
||||||
|
|
||||||
|
@Before
|
||||||
|
public void setUp() {
|
||||||
|
elfFactory = app.getElfKingdomFactory();
|
||||||
|
orcFactory = app.getOrcKingdomFactory();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void king() {
|
||||||
|
final King elfKing = app.getKing(elfFactory);
|
||||||
|
assertTrue(elfKing instanceof ElfKing);
|
||||||
|
assertEquals(ElfKing.DESCRIPTION, elfKing.getDescription());
|
||||||
|
final King orcKing = app.getKing(orcFactory);
|
||||||
|
assertTrue(orcKing instanceof OrcKing);
|
||||||
|
assertEquals(OrcKing.DESCRIPTION, orcKing.getDescription());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void castle() {
|
||||||
|
final Castle elfCastle = app.getCastle(elfFactory);
|
||||||
|
assertTrue(elfCastle instanceof ElfCastle);
|
||||||
|
assertEquals(ElfCastle.DESCRIPTION, elfCastle.getDescription());
|
||||||
|
final Castle orcCastle = app.getCastle(orcFactory);
|
||||||
|
assertTrue(orcCastle instanceof OrcCastle);
|
||||||
|
assertEquals(OrcCastle.DESCRIPTION, orcCastle.getDescription());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void army() {
|
||||||
|
final Army elfArmy = app.getArmy(elfFactory);
|
||||||
|
assertTrue(elfArmy instanceof ElfArmy);
|
||||||
|
assertEquals(ElfArmy.DESCRIPTION, elfArmy.getDescription());
|
||||||
|
final Army orcArmy = app.getArmy(orcFactory);
|
||||||
|
assertTrue(orcArmy instanceof OrcArmy);
|
||||||
|
assertEquals(OrcArmy.DESCRIPTION, orcArmy.getDescription());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void createElfKingdom() {
|
||||||
|
app.createKingdom(elfFactory);
|
||||||
|
final King king = app.getKing();
|
||||||
|
final Castle castle = app.getCastle();
|
||||||
|
final Army army = app.getArmy();
|
||||||
|
assertTrue(king instanceof ElfKing);
|
||||||
|
assertEquals(ElfKing.DESCRIPTION, king.getDescription());
|
||||||
|
assertTrue(castle instanceof ElfCastle);
|
||||||
|
assertEquals(ElfCastle.DESCRIPTION, castle.getDescription());
|
||||||
|
assertTrue(army instanceof ElfArmy);
|
||||||
|
assertEquals(ElfArmy.DESCRIPTION, army.getDescription());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void createOrcKingdom() {
|
||||||
|
app.createKingdom(orcFactory);
|
||||||
|
final King king = app.getKing();
|
||||||
|
final Castle castle = app.getCastle();
|
||||||
|
final Army army = app.getArmy();
|
||||||
|
assertTrue(king instanceof OrcKing);
|
||||||
|
assertEquals(OrcKing.DESCRIPTION, king.getDescription());
|
||||||
|
assertTrue(castle instanceof OrcCastle);
|
||||||
|
assertEquals(OrcCastle.DESCRIPTION, castle.getDescription());
|
||||||
|
assertTrue(army instanceof OrcArmy);
|
||||||
|
assertEquals(OrcArmy.DESCRIPTION, army.getDescription());
|
||||||
|
}
|
||||||
|
}
|
@ -1,78 +1,38 @@
|
|||||||
|
/**
|
||||||
|
* The MIT License
|
||||||
|
* Copyright (c) 2014 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.abstractfactory;
|
package com.iluwatar.abstractfactory;
|
||||||
|
|
||||||
import static org.junit.Assert.assertEquals;
|
|
||||||
import static org.junit.Assert.assertTrue;
|
|
||||||
|
|
||||||
import org.junit.Before;
|
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
|
|
||||||
|
import java.io.IOException;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Tests that Abstract Factory example runs without errors.
|
||||||
|
*/
|
||||||
public class AppTest {
|
public class AppTest {
|
||||||
|
|
||||||
private App app = new App();
|
|
||||||
private KingdomFactory elfFactory;
|
|
||||||
private KingdomFactory orcFactory;
|
|
||||||
|
|
||||||
@Before
|
|
||||||
public void setUp() {
|
|
||||||
elfFactory = app.getElfKingdomFactory();
|
|
||||||
orcFactory = app.getOrcKingdomFactory();
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void king() {
|
public void test() throws IOException {
|
||||||
final King elfKing = app.getKing(elfFactory);
|
String[] args = {};
|
||||||
assertTrue(elfKing instanceof ElfKing);
|
App.main(args);
|
||||||
assertEquals(ElfKing.DESCRIPTION, elfKing.getDescription());
|
|
||||||
final King orcKing = app.getKing(orcFactory);
|
|
||||||
assertTrue(orcKing instanceof OrcKing);
|
|
||||||
assertEquals(OrcKing.DESCRIPTION, orcKing.getDescription());
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
public void castle() {
|
|
||||||
final Castle elfCastle = app.getCastle(elfFactory);
|
|
||||||
assertTrue(elfCastle instanceof ElfCastle);
|
|
||||||
assertEquals(ElfCastle.DESCRIPTION, elfCastle.getDescription());
|
|
||||||
final Castle orcCastle = app.getCastle(orcFactory);
|
|
||||||
assertTrue(orcCastle instanceof OrcCastle);
|
|
||||||
assertEquals(OrcCastle.DESCRIPTION, orcCastle.getDescription());
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
public void army() {
|
|
||||||
final Army elfArmy = app.getArmy(elfFactory);
|
|
||||||
assertTrue(elfArmy instanceof ElfArmy);
|
|
||||||
assertEquals(ElfArmy.DESCRIPTION, elfArmy.getDescription());
|
|
||||||
final Army orcArmy = app.getArmy(orcFactory);
|
|
||||||
assertTrue(orcArmy instanceof OrcArmy);
|
|
||||||
assertEquals(OrcArmy.DESCRIPTION, orcArmy.getDescription());
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
public void createElfKingdom() {
|
|
||||||
app.createKingdom(elfFactory);
|
|
||||||
final King king = app.getKing();
|
|
||||||
final Castle castle = app.getCastle();
|
|
||||||
final Army army = app.getArmy();
|
|
||||||
assertTrue(king instanceof ElfKing);
|
|
||||||
assertEquals(ElfKing.DESCRIPTION, king.getDescription());
|
|
||||||
assertTrue(castle instanceof ElfCastle);
|
|
||||||
assertEquals(ElfCastle.DESCRIPTION, castle.getDescription());
|
|
||||||
assertTrue(army instanceof ElfArmy);
|
|
||||||
assertEquals(ElfArmy.DESCRIPTION, army.getDescription());
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
public void createOrcKingdom() {
|
|
||||||
app.createKingdom(orcFactory);
|
|
||||||
final King king = app.getKing();
|
|
||||||
final Castle castle = app.getCastle();
|
|
||||||
final Army army = app.getArmy();
|
|
||||||
assertTrue(king instanceof OrcKing);
|
|
||||||
assertEquals(OrcKing.DESCRIPTION, king.getDescription());
|
|
||||||
assertTrue(castle instanceof OrcCastle);
|
|
||||||
assertEquals(OrcCastle.DESCRIPTION, castle.getDescription());
|
|
||||||
assertTrue(army instanceof OrcArmy);
|
|
||||||
assertEquals(OrcArmy.DESCRIPTION, army.getDescription());
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Binary file not shown.
Before ![]() (image error) Size: 12 KiB After ![]() (image error) Size: 12 KiB ![]() ![]() |
@ -1,61 +1,61 @@
|
|||||||
<?xml version="1.0" encoding="UTF-8"?>
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
<class-diagram version="1.1.8" icons="true" automaticImage="PNG" always-add-relationships="false" generalizations="true"
|
<class-diagram version="1.1.9" icons="true" always-add-relationships="false" generalizations="true" realizations="true"
|
||||||
realizations="true" associations="true" dependencies="false" nesting-relationships="true">
|
associations="true" dependencies="false" nesting-relationships="true" router="FAN">
|
||||||
<class id="1" language="java" name="com.iluwatar.adapter.GnomeEngineeringManager" project="adapter"
|
<class id="1" language="java" name="com.iluwatar.adapter.FishingBoat" project="adapter"
|
||||||
file="/adapter/src/main/java/com/iluwatar/adapter/GnomeEngineeringManager.java" binary="false" corner="BOTTOM_RIGHT">
|
file="/adapter/src/main/java/com/iluwatar/adapter/FishingBoat.java" binary="false" corner="BOTTOM_RIGHT">
|
||||||
<position height="106" width="224" x="110" y="210"/>
|
<position height="-1" width="-1" x="656" y="355"/>
|
||||||
<display autosize="true" stereotype="true" package="true" initial-value="false" signature="true"
|
<display autosize="true" stereotype="true" package="true" initial-value="false" signature="true"
|
||||||
sort-features="false" accessors="true" visibility="true">
|
sort-features="false" accessors="true" visibility="true">
|
||||||
<attributes public="true" package="true" protected="true" private="true" static="true"/>
|
<attributes public="true" package="true" protected="true" private="true" static="true"/>
|
||||||
<operations public="true" package="true" protected="true" private="true" static="true"/>
|
<operations public="true" package="true" protected="true" private="true" static="true"/>
|
||||||
</display>
|
</display>
|
||||||
</class>
|
</class>
|
||||||
<interface id="2" language="java" name="com.iluwatar.adapter.Engineer" project="adapter"
|
<class id="2" language="java" name="com.iluwatar.adapter.Captain" project="adapter"
|
||||||
file="/adapter/src/main/java/com/iluwatar/adapter/Engineer.java" binary="false" corner="BOTTOM_RIGHT">
|
file="/adapter/src/main/java/com/iluwatar/adapter/Captain.java" binary="false" corner="BOTTOM_RIGHT">
|
||||||
<position height="88" width="141" x="110" y="356"/>
|
<position height="-1" width="-1" x="228" y="185"/>
|
||||||
|
<display autosize="true" stereotype="true" package="true" initial-value="false" signature="true"
|
||||||
|
sort-features="false" accessors="true" visibility="true">
|
||||||
|
<attributes public="true" package="true" protected="true" private="true" static="true"/>
|
||||||
|
<operations public="true" package="true" protected="true" private="true" static="true"/>
|
||||||
|
</display>
|
||||||
|
</class>
|
||||||
|
<class id="3" language="java" name="com.iluwatar.adapter.BattleFishingBoat" project="adapter"
|
||||||
|
file="/adapter/src/main/java/com/iluwatar/adapter/BattleFishingBoat.java" binary="false" corner="BOTTOM_RIGHT">
|
||||||
|
<position height="-1" width="-1" x="463" y="357"/>
|
||||||
|
<display autosize="true" stereotype="true" package="true" initial-value="false" signature="true"
|
||||||
|
sort-features="false" accessors="true" visibility="true">
|
||||||
|
<attributes public="true" package="true" protected="true" private="true" static="true"/>
|
||||||
|
<operations public="true" package="true" protected="true" private="true" static="true"/>
|
||||||
|
</display>
|
||||||
|
</class>
|
||||||
|
<interface id="4" language="java" name="com.iluwatar.adapter.BattleShip" project="adapter"
|
||||||
|
file="/adapter/src/main/java/com/iluwatar/adapter/BattleShip.java" binary="false" corner="BOTTOM_RIGHT">
|
||||||
|
<position height="-1" width="-1" x="466" y="170"/>
|
||||||
<display autosize="true" stereotype="true" package="true" initial-value="false" signature="true"
|
<display autosize="true" stereotype="true" package="true" initial-value="false" signature="true"
|
||||||
sort-features="false" accessors="true" visibility="true">
|
sort-features="false" accessors="true" visibility="true">
|
||||||
<attributes public="true" package="true" protected="true" private="true" static="true"/>
|
<attributes public="true" package="true" protected="true" private="true" static="true"/>
|
||||||
<operations public="true" package="true" protected="true" private="true" static="true"/>
|
<operations public="true" package="true" protected="true" private="true" static="true"/>
|
||||||
</display>
|
</display>
|
||||||
</interface>
|
</interface>
|
||||||
<class id="3" language="java" name="com.iluwatar.adapter.GnomeEngineer" project="adapter"
|
|
||||||
file="/adapter/src/main/java/com/iluwatar/adapter/GnomeEngineer.java" binary="false" corner="BOTTOM_RIGHT">
|
|
||||||
<position height="106" width="141" x="374" y="210"/>
|
|
||||||
<display autosize="true" stereotype="true" package="true" initial-value="false" signature="true"
|
|
||||||
sort-features="false" accessors="true" visibility="true">
|
|
||||||
<attributes public="true" package="true" protected="true" private="true" static="true"/>
|
|
||||||
<operations public="true" package="true" protected="true" private="true" static="true"/>
|
|
||||||
</display>
|
|
||||||
</class>
|
|
||||||
<class id="4" language="java" name="com.iluwatar.adapter.GoblinGlider" project="adapter"
|
|
||||||
file="/adapter/src/main/java/com/iluwatar/adapter/GoblinGlider.java" binary="false" corner="BOTTOM_RIGHT">
|
|
||||||
<position height="142" width="130" x="374" y="356"/>
|
|
||||||
<display autosize="true" stereotype="true" package="true" initial-value="false" signature="true"
|
|
||||||
sort-features="false" accessors="true" visibility="true">
|
|
||||||
<attributes public="true" package="true" protected="true" private="true" static="true"/>
|
|
||||||
<operations public="true" package="true" protected="true" private="true" static="true"/>
|
|
||||||
</display>
|
|
||||||
</class>
|
|
||||||
<realization id="5">
|
<realization id="5">
|
||||||
<end type="SOURCE" refId="1"/>
|
<end type="SOURCE" refId="2"/>
|
||||||
<end type="TARGET" refId="2"/>
|
<end type="TARGET" refId="4"/>
|
||||||
</realization>
|
</realization>
|
||||||
<realization id="6">
|
<association id="6">
|
||||||
<end type="SOURCE" refId="3"/>
|
<end type="SOURCE" refId="3" navigable="false">
|
||||||
<end type="TARGET" refId="2"/>
|
<attribute id="7" name="boat"/>
|
||||||
</realization>
|
<multiplicity id="8" minimum="0" maximum="1"/>
|
||||||
<association id="7">
|
|
||||||
<end type="SOURCE" refId="1" navigable="false">
|
|
||||||
<attribute id="8" name="engineer"/>
|
|
||||||
<multiplicity id="9" minimum="0" maximum="1"/>
|
|
||||||
</end>
|
</end>
|
||||||
<end type="TARGET" refId="2" navigable="true"/>
|
<end type="TARGET" refId="1" navigable="true"/>
|
||||||
<display labels="true" multiplicity="true"/>
|
<display labels="true" multiplicity="true"/>
|
||||||
</association>
|
</association>
|
||||||
|
<realization id="9">
|
||||||
|
<end type="SOURCE" refId="3"/>
|
||||||
|
<end type="TARGET" refId="4"/>
|
||||||
|
</realization>
|
||||||
<association id="10">
|
<association id="10">
|
||||||
<end type="SOURCE" refId="3" navigable="false">
|
<end type="SOURCE" refId="2" navigable="false">
|
||||||
<attribute id="11" name="glider"/>
|
<attribute id="11" name="battleship"/>
|
||||||
<multiplicity id="12" minimum="0" maximum="1"/>
|
<multiplicity id="12" minimum="0" maximum="1"/>
|
||||||
</end>
|
</end>
|
||||||
<end type="TARGET" refId="4" navigable="true"/>
|
<end type="TARGET" refId="4" navigable="true"/>
|
||||||
|
Binary file not shown.
Before ![]() (image error) Size: 29 KiB |
@ -7,26 +7,31 @@ categories: Structural
|
|||||||
tags:
|
tags:
|
||||||
- Java
|
- Java
|
||||||
- Gang Of Four
|
- Gang Of Four
|
||||||
|
- Difficulty-Beginner
|
||||||
---
|
---
|
||||||
|
|
||||||
**Also known as:** Wrapper
|
## Also known as
|
||||||
|
Wrapper
|
||||||
|
|
||||||
**Intent:** Convert the interface of a class into another interface the clients
|
## Intent
|
||||||
|
Convert the interface of a class into another interface the clients
|
||||||
expect. Adapter lets classes work together that couldn't otherwise because of
|
expect. Adapter lets classes work together that couldn't otherwise because of
|
||||||
incompatible interfaces.
|
incompatible interfaces.
|
||||||
|
|
||||||

|

|
||||||
|
|
||||||
**Applicability:** Use the Adapter pattern when
|
## Applicability
|
||||||
|
Use the Adapter pattern when
|
||||||
|
|
||||||
* you want to use an existing class, and its interface does not match the one you need
|
* you want to use an existing class, and its interface does not match the one you need
|
||||||
* you want to create a reusable class that cooperates with unrelated or unforeseen classes, that is, classes that don't necessarily have compatible interfaces
|
* you want to create a reusable class that cooperates with unrelated or unforeseen classes, that is, classes that don't necessarily have compatible interfaces
|
||||||
* you need to use several existing subclasses, but it's impractical to adapt their interface by subclassing every one. An object adapter can adapt the interface of its parent class.
|
* you need to use several existing subclasses, but it's impractical to adapt their interface by subclassing every one. An object adapter can adapt the interface of its parent class.
|
||||||
|
|
||||||
**Real world examples:**
|
## Real world examples
|
||||||
|
|
||||||
* [java.util.Arrays#asList()](http://docs.oracle.com/javase/8/docs/api/java/util/Arrays.html#asList%28T...%29)
|
* [java.util.Arrays#asList()](http://docs.oracle.com/javase/8/docs/api/java/util/Arrays.html#asList%28T...%29)
|
||||||
|
|
||||||
**Credits**
|
## Credits
|
||||||
|
|
||||||
* [Design Patterns: Elements of Reusable Object-Oriented Software](http://www.amazon.com/Design-Patterns-Elements-Reusable-Object-Oriented/dp/0201633612)
|
* [Design Patterns: Elements of Reusable Object-Oriented Software](http://www.amazon.com/Design-Patterns-Elements-Reusable-Object-Oriented/dp/0201633612)
|
||||||
|
* [J2EE Design Patterns](http://www.amazon.com/J2EE-Design-Patterns-William-Crawford/dp/0596004273/ref=sr_1_2)
|
||||||
|
@ -1,11 +1,35 @@
|
|||||||
<?xml version="1.0"?>
|
<?xml version="1.0"?>
|
||||||
|
<!--
|
||||||
|
|
||||||
|
The MIT License
|
||||||
|
Copyright (c) 2014 Ilkka Seppälä
|
||||||
|
|
||||||
|
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||||
|
of this software and associated documentation files (the "Software"), to deal
|
||||||
|
in the Software without restriction, including without limitation the rights
|
||||||
|
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||||
|
copies of the Software, and to permit persons to whom the Software is
|
||||||
|
furnished to do so, subject to the following conditions:
|
||||||
|
|
||||||
|
The above copyright notice and this permission notice shall be included in
|
||||||
|
all copies or substantial portions of the Software.
|
||||||
|
|
||||||
|
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||||
|
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||||
|
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||||
|
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||||
|
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||||
|
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||||
|
THE SOFTWARE.
|
||||||
|
|
||||||
|
-->
|
||||||
<project xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" xmlns="http://maven.apache.org/POM/4.0.0"
|
<project xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" xmlns="http://maven.apache.org/POM/4.0.0"
|
||||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
|
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
|
||||||
<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.8.0</version>
|
<version>1.10.0</version>
|
||||||
</parent>
|
</parent>
|
||||||
<artifactId>adapter</artifactId>
|
<artifactId>adapter</artifactId>
|
||||||
<dependencies>
|
<dependencies>
|
||||||
|
@ -1,3 +1,25 @@
|
|||||||
|
/**
|
||||||
|
* The MIT License
|
||||||
|
* Copyright (c) 2014 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;
|
package com.iluwatar.adapter;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -6,13 +28,22 @@ 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 GnomeEngineer}) converts the interface of the target class (
|
* <p>
|
||||||
* {@link GoblinGlider}) into a suitable one expected by the client ({@link GnomeEngineeringManager}
|
* The Adapter ({@link BattleFishingBoat}) converts the interface of the adaptee class (
|
||||||
* ).
|
* {@link FishingBoat}) into a suitable one expected by the client ( {@link BattleShip} ).
|
||||||
|
*
|
||||||
|
* <p>
|
||||||
|
* The story of this implementation is this. <br>
|
||||||
|
* Pirates are coming! we need a {@link BattleShip} to fight! 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 battleship which can fire and move. The spec is in {@link BattleShip}. We will
|
||||||
|
* use the Adapter pattern to reuse {@link FishingBoat}.
|
||||||
|
*
|
||||||
*/
|
*/
|
||||||
public class App {
|
public class App {
|
||||||
|
|
||||||
@ -22,7 +53,8 @@ public class App {
|
|||||||
* @param args command line args
|
* @param args command line args
|
||||||
*/
|
*/
|
||||||
public static void main(String[] args) {
|
public static void main(String[] args) {
|
||||||
Engineer manager = new GnomeEngineeringManager(new GnomeEngineer());
|
Captain captain = new Captain(new BattleFishingBoat());
|
||||||
manager.operateDevice();
|
captain.move();
|
||||||
|
captain.fire();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -0,0 +1,51 @@
|
|||||||
|
/**
|
||||||
|
* The MIT License
|
||||||
|
* Copyright (c) 2014 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;
|
||||||
|
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
* Adapter class. Adapts the interface of the device ({@link FishingBoat}) into {@link BattleShip}
|
||||||
|
* interface expected by the client ({@link Captain}). <br>
|
||||||
|
* In this case we added a new function fire to suit the interface. We are reusing the
|
||||||
|
* {@link FishingBoat} without changing itself. The Adapter class can just map the functions of the
|
||||||
|
* Adaptee or add, delete features of the Adaptee.
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
public class BattleFishingBoat implements BattleShip {
|
||||||
|
|
||||||
|
private FishingBoat boat;
|
||||||
|
|
||||||
|
public BattleFishingBoat() {
|
||||||
|
boat = new FishingBoat();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void fire() {
|
||||||
|
System.out.println("fire!");
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void move() {
|
||||||
|
boat.sail();
|
||||||
|
}
|
||||||
|
}
|
36
adapter/src/main/java/com/iluwatar/adapter/BattleShip.java
Normal file
36
adapter/src/main/java/com/iluwatar/adapter/BattleShip.java
Normal file
@ -0,0 +1,36 @@
|
|||||||
|
/**
|
||||||
|
* The MIT License
|
||||||
|
* Copyright (c) 2014 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;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The interface expected by the client.<br>
|
||||||
|
* A Battleship can fire and move.
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
public interface BattleShip {
|
||||||
|
|
||||||
|
void fire();
|
||||||
|
|
||||||
|
void move();
|
||||||
|
|
||||||
|
}
|
55
adapter/src/main/java/com/iluwatar/adapter/Captain.java
Normal file
55
adapter/src/main/java/com/iluwatar/adapter/Captain.java
Normal file
@ -0,0 +1,55 @@
|
|||||||
|
/**
|
||||||
|
* The MIT License
|
||||||
|
* Copyright (c) 2014 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;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The Captain uses {@link BattleShip} to fight. <br>
|
||||||
|
* This is the client in the pattern.
|
||||||
|
*/
|
||||||
|
public class Captain implements BattleShip {
|
||||||
|
|
||||||
|
private BattleShip battleship;
|
||||||
|
|
||||||
|
public Captain() {
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
public Captain(BattleShip battleship) {
|
||||||
|
this.battleship = battleship;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setBattleship(BattleShip battleship) {
|
||||||
|
this.battleship = battleship;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void fire() {
|
||||||
|
battleship.fire();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void move() {
|
||||||
|
battleship.move();
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -1,11 +0,0 @@
|
|||||||
package com.iluwatar.adapter;
|
|
||||||
|
|
||||||
/**
|
|
||||||
*
|
|
||||||
* Engineers can operate devices.
|
|
||||||
*
|
|
||||||
*/
|
|
||||||
public interface Engineer {
|
|
||||||
|
|
||||||
void operateDevice();
|
|
||||||
}
|
|
40
adapter/src/main/java/com/iluwatar/adapter/FishingBoat.java
Normal file
40
adapter/src/main/java/com/iluwatar/adapter/FishingBoat.java
Normal file
@ -0,0 +1,40 @@
|
|||||||
|
/**
|
||||||
|
* The MIT License
|
||||||
|
* Copyright (c) 2014 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;
|
||||||
|
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
* Device class (adaptee in the pattern). We want to reuse this class
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
public class FishingBoat {
|
||||||
|
|
||||||
|
public void sail() {
|
||||||
|
System.out.println("The Boat is moving to that place");
|
||||||
|
}
|
||||||
|
|
||||||
|
public void fish() {
|
||||||
|
System.out.println("fishing ...");
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -1,23 +0,0 @@
|
|||||||
package com.iluwatar.adapter;
|
|
||||||
|
|
||||||
/**
|
|
||||||
*
|
|
||||||
* Adapter class. Adapts the interface of the device ({@link GoblinGlider}) into {@link Engineer}
|
|
||||||
* interface expected by the client ({@link GnomeEngineeringManager}).
|
|
||||||
*
|
|
||||||
*/
|
|
||||||
public class GnomeEngineer implements Engineer {
|
|
||||||
|
|
||||||
private GoblinGlider glider;
|
|
||||||
|
|
||||||
public GnomeEngineer() {
|
|
||||||
glider = new GoblinGlider();
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void operateDevice() {
|
|
||||||
glider.attachGlider();
|
|
||||||
glider.gainSpeed();
|
|
||||||
glider.takeOff();
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,26 +0,0 @@
|
|||||||
package com.iluwatar.adapter;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* GnomeEngineering manager uses {@link Engineer} to operate devices.
|
|
||||||
*/
|
|
||||||
public class GnomeEngineeringManager implements Engineer {
|
|
||||||
|
|
||||||
private Engineer engineer;
|
|
||||||
|
|
||||||
public GnomeEngineeringManager() {
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
public GnomeEngineeringManager(Engineer engineer) {
|
|
||||||
this.engineer = engineer;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void operateDevice() {
|
|
||||||
engineer.operateDevice();
|
|
||||||
}
|
|
||||||
|
|
||||||
public void setEngineer(Engineer engineer) {
|
|
||||||
this.engineer = engineer;
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,21 +0,0 @@
|
|||||||
package com.iluwatar.adapter;
|
|
||||||
|
|
||||||
/**
|
|
||||||
*
|
|
||||||
* Device class (adaptee in the pattern).
|
|
||||||
*
|
|
||||||
*/
|
|
||||||
public class GoblinGlider {
|
|
||||||
|
|
||||||
public void attachGlider() {
|
|
||||||
System.out.println("Glider attached.");
|
|
||||||
}
|
|
||||||
|
|
||||||
public void gainSpeed() {
|
|
||||||
System.out.println("Gaining speed.");
|
|
||||||
}
|
|
||||||
|
|
||||||
public void takeOff() {
|
|
||||||
System.out.println("Lift-off!");
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,37 +1,47 @@
|
|||||||
|
/**
|
||||||
|
* The MIT License
|
||||||
|
* Copyright (c) 2014 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;
|
package com.iluwatar.adapter;
|
||||||
|
|
||||||
import static org.mockito.Mockito.spy;
|
|
||||||
import static org.mockito.Mockito.verify;
|
|
||||||
|
|
||||||
import java.util.HashMap;
|
|
||||||
import java.util.Map;
|
|
||||||
|
|
||||||
import org.junit.Before;
|
import org.junit.Before;
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
|
|
||||||
|
import java.util.HashMap;
|
||||||
|
import java.util.Map;
|
||||||
|
|
||||||
|
import static org.mockito.Mockito.spy;
|
||||||
|
import static org.mockito.Mockito.verify;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* An adapter helps two incompatible interfaces to work together. This is the real world definition
|
* Test class
|
||||||
* for an adapter. Interfaces may be incompatible but the inner functionality should suit the need.
|
|
||||||
* 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.
|
|
||||||
*
|
*
|
||||||
* <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
|
|
||||||
* object. This example uses the object adapter approach.
|
|
||||||
*
|
|
||||||
* <p>The Adapter ({@link GnomeEngineer}) converts the interface
|
|
||||||
* of the target class ({@link GoblinGlider}) into a suitable one expected by
|
|
||||||
* the client ({@link GnomeEngineeringManager}
|
|
||||||
* ).
|
|
||||||
*/
|
*/
|
||||||
public class AdapterPatternTest {
|
public class AdapterPatternTest {
|
||||||
|
|
||||||
private Map<String, Object> beans;
|
private Map<String, Object> beans;
|
||||||
|
|
||||||
private static final String ENGINEER_BEAN = "engineer";
|
private static final String BATTLESHIP_BEAN = "engineer";
|
||||||
|
|
||||||
private static final String MANAGER_BEAN = "manager";
|
private static final String CAPTAIN_BEAN = "captain";
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* This method runs before the test execution and sets the bean objects in the beans Map.
|
* This method runs before the test execution and sets the bean objects in the beans Map.
|
||||||
@ -40,29 +50,34 @@ public class AdapterPatternTest {
|
|||||||
public void setup() {
|
public void setup() {
|
||||||
beans = new HashMap<>();
|
beans = new HashMap<>();
|
||||||
|
|
||||||
GnomeEngineer gnomeEngineer = spy(new GnomeEngineer());
|
BattleFishingBoat battleFishingBoat = spy(new BattleFishingBoat());
|
||||||
beans.put(ENGINEER_BEAN, gnomeEngineer);
|
beans.put(BATTLESHIP_BEAN, battleFishingBoat);
|
||||||
|
|
||||||
GnomeEngineeringManager manager = new GnomeEngineeringManager();
|
Captain captain = new Captain();
|
||||||
manager.setEngineer((GnomeEngineer) beans.get(ENGINEER_BEAN));
|
captain.setBattleship((BattleFishingBoat) beans.get(BATTLESHIP_BEAN));
|
||||||
beans.put(MANAGER_BEAN, manager);
|
beans.put(CAPTAIN_BEAN, captain);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* This test asserts that when we call operateDevice() method on a manager bean, it is internally
|
* This test asserts that when we use the move() method on a captain bean(client), it is
|
||||||
* calling operateDevice method on the engineer object. The Adapter ({@link GnomeEngineer})
|
* internally calling move method on the battleship object. The Adapter ({@link BattleFishingBoat}
|
||||||
* converts the interface of the target class ( {@link GoblinGlider}) into a suitable one expected
|
* ) converts the interface of the target class ( {@link FishingBoat}) into a suitable one
|
||||||
* by the client ({@link GnomeEngineeringManager} ).
|
* expected by the client ({@link Captain} ).
|
||||||
*/
|
*/
|
||||||
@Test
|
@Test
|
||||||
public void testAdapter() {
|
public void testAdapter() {
|
||||||
Engineer manager = (Engineer) beans.get(MANAGER_BEAN);
|
BattleShip captain = (BattleShip) beans.get(CAPTAIN_BEAN);
|
||||||
|
|
||||||
// when manager is asked to operate device
|
// when captain moves
|
||||||
manager.operateDevice();
|
captain.move();
|
||||||
|
|
||||||
|
// the captain internally calls the battleship object to move
|
||||||
|
BattleShip battleship = (BattleShip) beans.get(BATTLESHIP_BEAN);
|
||||||
|
verify(battleship).move();
|
||||||
|
|
||||||
|
// same with above with firing
|
||||||
|
captain.fire();
|
||||||
|
verify(battleship).fire();
|
||||||
|
|
||||||
// Manager internally calls the engineer object to operateDevice
|
|
||||||
Engineer engineer = (Engineer) beans.get(ENGINEER_BEAN);
|
|
||||||
verify(engineer).operateDevice();
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
38
adapter/src/test/java/com/iluwatar/adapter/AppTest.java
Normal file
38
adapter/src/test/java/com/iluwatar/adapter/AppTest.java
Normal file
@ -0,0 +1,38 @@
|
|||||||
|
/**
|
||||||
|
* The MIT License
|
||||||
|
* Copyright (c) 2014 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;
|
||||||
|
|
||||||
|
import org.junit.Test;
|
||||||
|
|
||||||
|
import java.io.IOException;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Tests that Adapter example runs without errors.
|
||||||
|
*/
|
||||||
|
public class AppTest {
|
||||||
|
@Test
|
||||||
|
public void test() throws IOException {
|
||||||
|
String[] args = {};
|
||||||
|
App.main(args);
|
||||||
|
}
|
||||||
|
}
|
@ -4,24 +4,29 @@ title: Async Method Invocation
|
|||||||
folder: async-method-invocation
|
folder: async-method-invocation
|
||||||
permalink: /patterns/async-method-invocation/
|
permalink: /patterns/async-method-invocation/
|
||||||
categories: Concurrency
|
categories: Concurrency
|
||||||
tags: Java
|
tags:
|
||||||
|
- Java
|
||||||
|
- Difficulty-Intermediate
|
||||||
|
- Functional
|
||||||
---
|
---
|
||||||
|
|
||||||
**Intent:** Asynchronous method invocation is pattern where the calling thread
|
## Intent
|
||||||
|
Asynchronous method invocation is pattern where the calling thread
|
||||||
is not blocked while waiting results of tasks. The pattern provides parallel
|
is not blocked while waiting results of tasks. The pattern provides parallel
|
||||||
processing of multiple independent tasks and retrieving the results via
|
processing of multiple independent tasks and retrieving the results via
|
||||||
callbacks or waiting until everything is done.
|
callbacks or waiting until everything is done.
|
||||||
|
|
||||||

|

|
||||||
|
|
||||||
**Applicability:** Use async method invocation pattern when
|
## Applicability
|
||||||
|
Use async method invocation pattern when
|
||||||
|
|
||||||
* you have multiple independent tasks that can run in parallel
|
* you have multiple independent tasks that can run in parallel
|
||||||
* you need to improve the performance of a group of sequential tasks
|
* you need to improve the performance of a group of sequential tasks
|
||||||
* you have limited amount of processing capacity or long running tasks and the
|
* you have limited amount of processing capacity or long running tasks and the
|
||||||
caller should not wait the tasks to be ready
|
caller should not wait the tasks to be ready
|
||||||
|
|
||||||
**Real world examples:**
|
## Real world examples
|
||||||
|
|
||||||
* [FutureTask](http://docs.oracle.com/javase/8/docs/api/java/util/concurrent/FutureTask.html), [CompletableFuture](https://docs.oracle.com/javase/8/docs/api/java/util/concurrent/CompletableFuture.html) and [ExecutorService](http://docs.oracle.com/javase/8/docs/api/java/util/concurrent/ExecutorService.html) (Java)
|
* [FutureTask](http://docs.oracle.com/javase/8/docs/api/java/util/concurrent/FutureTask.html), [CompletableFuture](https://docs.oracle.com/javase/8/docs/api/java/util/concurrent/CompletableFuture.html) and [ExecutorService](http://docs.oracle.com/javase/8/docs/api/java/util/concurrent/ExecutorService.html) (Java)
|
||||||
* [Task-based Asynchronous Pattern](https://msdn.microsoft.com/en-us/library/hh873175.aspx) (.NET)
|
* [Task-based Asynchronous Pattern](https://msdn.microsoft.com/en-us/library/hh873175.aspx) (.NET)
|
||||||
|
@ -1,11 +1,35 @@
|
|||||||
<?xml version="1.0"?>
|
<?xml version="1.0"?>
|
||||||
|
<!--
|
||||||
|
|
||||||
|
The MIT License
|
||||||
|
Copyright (c) 2014 Ilkka Seppälä
|
||||||
|
|
||||||
|
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||||
|
of this software and associated documentation files (the "Software"), to deal
|
||||||
|
in the Software without restriction, including without limitation the rights
|
||||||
|
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||||
|
copies of the Software, and to permit persons to whom the Software is
|
||||||
|
furnished to do so, subject to the following conditions:
|
||||||
|
|
||||||
|
The above copyright notice and this permission notice shall be included in
|
||||||
|
all copies or substantial portions of the Software.
|
||||||
|
|
||||||
|
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||||
|
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||||
|
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||||
|
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||||
|
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||||
|
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||||
|
THE SOFTWARE.
|
||||||
|
|
||||||
|
-->
|
||||||
<project xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" xmlns="http://maven.apache.org/POM/4.0.0"
|
<project xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" xmlns="http://maven.apache.org/POM/4.0.0"
|
||||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
|
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
|
||||||
<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.8.0</version>
|
<version>1.10.0</version>
|
||||||
</parent>
|
</parent>
|
||||||
<artifactId>async-method-invocation</artifactId>
|
<artifactId>async-method-invocation</artifactId>
|
||||||
<dependencies>
|
<dependencies>
|
||||||
@ -14,5 +38,10 @@
|
|||||||
<artifactId>junit</artifactId>
|
<artifactId>junit</artifactId>
|
||||||
<scope>test</scope>
|
<scope>test</scope>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.mockito</groupId>
|
||||||
|
<artifactId>mockito-core</artifactId>
|
||||||
|
<scope>test</scope>
|
||||||
|
</dependency>
|
||||||
</dependencies>
|
</dependencies>
|
||||||
</project>
|
</project>
|
||||||
|
@ -1,27 +1,48 @@
|
|||||||
|
/**
|
||||||
|
* The MIT License
|
||||||
|
* Copyright (c) 2014 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.async.method.invocation;
|
package com.iluwatar.async.method.invocation;
|
||||||
|
|
||||||
import java.util.concurrent.Callable;
|
import java.util.concurrent.Callable;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* This application demonstrates the async method invocation pattern. Key parts of the pattern are
|
* This application demonstrates the async method invocation pattern. Key parts of the pattern are
|
||||||
* <code>AsyncResult</code> which is an intermediate container for an asynchronously evaluated
|
* <code>AsyncResult</code> which is an intermediate container for an asynchronously evaluated value,
|
||||||
* value, <code>AsyncCallback</code> which can be provided to be executed on task completion and
|
* <code>AsyncCallback</code> which can be provided to be executed on task completion and <code>AsyncExecutor</code>
|
||||||
* <code>AsyncExecutor</code> that manages the execution of the async tasks.
|
* that manages the execution of the async tasks.
|
||||||
* <p>
|
* <p>
|
||||||
* The main method shows example flow of async invocations. The main thread starts multiple tasks
|
* The main method shows example flow of async invocations. The main thread starts multiple tasks with variable
|
||||||
* with variable durations and then continues its own work. When the main thread has done it's job
|
* durations and then continues its own work. When the main thread has done it's job it collects the results of the
|
||||||
* it collects the results of the async tasks. Two of the tasks are handled with callbacks, meaning
|
* async tasks. Two of the tasks are handled with callbacks, meaning the callbacks are executed immediately when the
|
||||||
* the callbacks are executed immediately when the tasks complete.
|
* tasks complete.
|
||||||
* <p>
|
* <p>
|
||||||
* Noteworthy difference of thread usage between the async results and callbacks is that the async
|
* Noteworthy difference of thread usage between the async results and callbacks is that the async results are collected
|
||||||
* results are collected in the main thread but the callbacks are executed within the worker
|
* in the main thread but the callbacks are executed within the worker threads. This should be noted when working with
|
||||||
* threads. This should be noted when working with thread pools.
|
* thread pools.
|
||||||
* <p>
|
* <p>
|
||||||
* Java provides its own implementations of async method invocation pattern. FutureTask,
|
* Java provides its own implementations of async method invocation pattern. FutureTask, CompletableFuture and
|
||||||
* CompletableFuture and ExecutorService are the real world implementations of this pattern. But due
|
* ExecutorService are the real world implementations of this pattern. But due to the nature of parallel programming,
|
||||||
* to the nature of parallel programming, the implementations are not trivial. This example does not
|
* the implementations are not trivial. This example does not take all possible scenarios into account but rather
|
||||||
* take all possible scenarios into account but rather provides a simple version that helps to
|
* provides a simple version that helps to understand the pattern.
|
||||||
* understand the pattern.
|
|
||||||
*
|
*
|
||||||
* @see AsyncResult
|
* @see AsyncResult
|
||||||
* @see AsyncCallback
|
* @see AsyncCallback
|
||||||
@ -33,6 +54,9 @@ import java.util.concurrent.Callable;
|
|||||||
*/
|
*/
|
||||||
public class App {
|
public class App {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Program entry point
|
||||||
|
*/
|
||||||
public static void main(String[] args) throws Exception {
|
public static void main(String[] args) throws Exception {
|
||||||
// construct a new executor that will run async tasks
|
// construct a new executor that will run async tasks
|
||||||
AsyncExecutor executor = new ThreadAsyncExecutor();
|
AsyncExecutor executor = new ThreadAsyncExecutor();
|
||||||
@ -41,10 +65,8 @@ public class App {
|
|||||||
AsyncResult<Integer> asyncResult1 = executor.startProcess(lazyval(10, 500));
|
AsyncResult<Integer> asyncResult1 = executor.startProcess(lazyval(10, 500));
|
||||||
AsyncResult<String> asyncResult2 = executor.startProcess(lazyval("test", 300));
|
AsyncResult<String> asyncResult2 = executor.startProcess(lazyval("test", 300));
|
||||||
AsyncResult<Long> asyncResult3 = executor.startProcess(lazyval(50L, 700));
|
AsyncResult<Long> asyncResult3 = executor.startProcess(lazyval(50L, 700));
|
||||||
AsyncResult<Integer> asyncResult4 =
|
AsyncResult<Integer> asyncResult4 = executor.startProcess(lazyval(20, 400), callback("Callback result 4"));
|
||||||
executor.startProcess(lazyval(20, 400), callback("Callback result 4"));
|
AsyncResult<String> asyncResult5 = executor.startProcess(lazyval("callback", 600), callback("Callback result 5"));
|
||||||
AsyncResult<String> asyncResult5 =
|
|
||||||
executor.startProcess(lazyval("callback", 600), callback("Callback result 5"));
|
|
||||||
|
|
||||||
// emulate processing in the current thread while async tasks are running in their own threads
|
// emulate processing in the current thread while async tasks are running in their own threads
|
||||||
Thread.sleep(350); // Oh boy I'm working hard here
|
Thread.sleep(350); // Oh boy I'm working hard here
|
||||||
@ -66,8 +88,10 @@ public class App {
|
|||||||
/**
|
/**
|
||||||
* Creates a callable that lazily evaluates to given value with artificial delay.
|
* Creates a callable that lazily evaluates to given value with artificial delay.
|
||||||
*
|
*
|
||||||
* @param value value to evaluate
|
* @param value
|
||||||
* @param delayMillis artificial delay in milliseconds
|
* value to evaluate
|
||||||
|
* @param delayMillis
|
||||||
|
* artificial delay in milliseconds
|
||||||
* @return new callable for lazy evaluation
|
* @return new callable for lazy evaluation
|
||||||
*/
|
*/
|
||||||
private static <T> Callable<T> lazyval(T value, long delayMillis) {
|
private static <T> Callable<T> lazyval(T value, long delayMillis) {
|
||||||
@ -81,7 +105,8 @@ public class App {
|
|||||||
/**
|
/**
|
||||||
* Creates a simple callback that logs the complete status of the async result.
|
* Creates a simple callback that logs the complete status of the async result.
|
||||||
*
|
*
|
||||||
* @param name callback name
|
* @param name
|
||||||
|
* callback name
|
||||||
* @return new async callback
|
* @return new async callback
|
||||||
*/
|
*/
|
||||||
private static <T> AsyncCallback<T> callback(String name) {
|
private static <T> AsyncCallback<T> callback(String name) {
|
||||||
|
@ -1,3 +1,25 @@
|
|||||||
|
/**
|
||||||
|
* The MIT License
|
||||||
|
* Copyright (c) 2014 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.async.method.invocation;
|
package com.iluwatar.async.method.invocation;
|
||||||
|
|
||||||
import java.util.Optional;
|
import java.util.Optional;
|
||||||
|
@ -1,3 +1,25 @@
|
|||||||
|
/**
|
||||||
|
* The MIT License
|
||||||
|
* Copyright (c) 2014 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.async.method.invocation;
|
package com.iluwatar.async.method.invocation;
|
||||||
|
|
||||||
import java.util.concurrent.Callable;
|
import java.util.concurrent.Callable;
|
||||||
|
@ -1,3 +1,25 @@
|
|||||||
|
/**
|
||||||
|
* The MIT License
|
||||||
|
* Copyright (c) 2014 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.async.method.invocation;
|
package com.iluwatar.async.method.invocation;
|
||||||
|
|
||||||
import java.util.concurrent.ExecutionException;
|
import java.util.concurrent.ExecutionException;
|
||||||
@ -5,8 +27,6 @@ import java.util.concurrent.ExecutionException;
|
|||||||
/**
|
/**
|
||||||
*
|
*
|
||||||
* AsyncResult interface
|
* AsyncResult interface
|
||||||
*
|
|
||||||
* @param <T>
|
|
||||||
*/
|
*/
|
||||||
public interface AsyncResult<T> {
|
public interface AsyncResult<T> {
|
||||||
|
|
||||||
|
48
async-method-invocation/src/main/java/com/iluwatar/async/method/invocation/ThreadAsyncExecutor.java
48
async-method-invocation/src/main/java/com/iluwatar/async/method/invocation/ThreadAsyncExecutor.java
@ -1,3 +1,25 @@
|
|||||||
|
/**
|
||||||
|
* The MIT License
|
||||||
|
* Copyright (c) 2014 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.async.method.invocation;
|
package com.iluwatar.async.method.invocation;
|
||||||
|
|
||||||
import java.util.Optional;
|
import java.util.Optional;
|
||||||
@ -29,13 +51,12 @@ public class ThreadAsyncExecutor implements AsyncExecutor {
|
|||||||
} catch (Exception ex) {
|
} catch (Exception ex) {
|
||||||
result.setException(ex);
|
result.setException(ex);
|
||||||
}
|
}
|
||||||
}, "executor-" + idx.incrementAndGet()).start();
|
} , "executor-" + idx.incrementAndGet()).start();
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public <T> T endProcess(AsyncResult<T> asyncResult) throws ExecutionException,
|
public <T> T endProcess(AsyncResult<T> asyncResult) throws ExecutionException, InterruptedException {
|
||||||
InterruptedException {
|
|
||||||
if (asyncResult.isCompleted()) {
|
if (asyncResult.isCompleted()) {
|
||||||
return asyncResult.getValue();
|
return asyncResult.getValue();
|
||||||
} else {
|
} else {
|
||||||
@ -45,9 +66,8 @@ public class ThreadAsyncExecutor implements AsyncExecutor {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Simple implementation of async result that allows completing it successfully with a value or
|
* Simple implementation of async result that allows completing it successfully with a value or exceptionally with an
|
||||||
* exceptionally with an exception. A really simplified version from its real life cousins
|
* exception. A really simplified version from its real life cousins FutureTask and CompletableFuture.
|
||||||
* FutureTask and CompletableFuture.
|
|
||||||
*
|
*
|
||||||
* @see java.util.concurrent.FutureTask
|
* @see java.util.concurrent.FutureTask
|
||||||
* @see java.util.concurrent.CompletableFuture
|
* @see java.util.concurrent.CompletableFuture
|
||||||
@ -71,10 +91,11 @@ public class ThreadAsyncExecutor implements AsyncExecutor {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Sets the value from successful execution and executes callback if available. Notifies any
|
* Sets the value from successful execution and executes callback if available. Notifies any thread waiting for
|
||||||
* thread waiting for completion.
|
* completion.
|
||||||
*
|
*
|
||||||
* @param value value of the evaluated task
|
* @param value
|
||||||
|
* value of the evaluated task
|
||||||
*/
|
*/
|
||||||
void setValue(T value) {
|
void setValue(T value) {
|
||||||
this.value = value;
|
this.value = value;
|
||||||
@ -86,10 +107,11 @@ public class ThreadAsyncExecutor implements AsyncExecutor {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Sets the exception from failed execution and executes callback if available. Notifies any
|
* Sets the exception from failed execution and executes callback if available. Notifies any thread waiting for
|
||||||
* thread waiting for completion.
|
* completion.
|
||||||
*
|
*
|
||||||
* @param exception exception of the failed task
|
* @param exception
|
||||||
|
* exception of the failed task
|
||||||
*/
|
*/
|
||||||
void setException(Exception exception) {
|
void setException(Exception exception) {
|
||||||
this.exception = exception;
|
this.exception = exception;
|
||||||
@ -102,7 +124,7 @@ public class ThreadAsyncExecutor implements AsyncExecutor {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public boolean isCompleted() {
|
public boolean isCompleted() {
|
||||||
return (state > RUNNING);
|
return state > RUNNING;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
@ -1,3 +1,25 @@
|
|||||||
|
/**
|
||||||
|
* The MIT License
|
||||||
|
* Copyright (c) 2014 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.async.method.invocation;
|
package com.iluwatar.async.method.invocation;
|
||||||
|
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
|
312
async-method-invocation/src/test/java/com/iluwatar/async/method/invocation/ThreadAsyncExecutorTest.java
Normal file
312
async-method-invocation/src/test/java/com/iluwatar/async/method/invocation/ThreadAsyncExecutorTest.java
Normal file
@ -0,0 +1,312 @@
|
|||||||
|
/**
|
||||||
|
* The MIT License
|
||||||
|
* Copyright (c) 2014 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.async.method.invocation;
|
||||||
|
|
||||||
|
import org.junit.Test;
|
||||||
|
import org.mockito.ArgumentCaptor;
|
||||||
|
import org.mockito.Matchers;
|
||||||
|
|
||||||
|
import java.util.Optional;
|
||||||
|
import java.util.concurrent.Callable;
|
||||||
|
import java.util.concurrent.ExecutionException;
|
||||||
|
|
||||||
|
import static org.junit.Assert.*;
|
||||||
|
import static org.mockito.Matchers.eq;
|
||||||
|
import static org.mockito.Mockito.*;
|
||||||
|
import static org.mockito.internal.verification.VerificationModeFactory.times;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Date: 12/6/15 - 10:49 AM
|
||||||
|
*
|
||||||
|
* @author Jeroen Meulemeester
|
||||||
|
*/
|
||||||
|
public class ThreadAsyncExecutorTest {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Test used to verify the happy path of {@link ThreadAsyncExecutor#startProcess(Callable)}
|
||||||
|
*/
|
||||||
|
@Test(timeout = 3000)
|
||||||
|
public void testSuccessfulTaskWithoutCallback() throws Exception {
|
||||||
|
// Instantiate a new executor and start a new 'null' task ...
|
||||||
|
final ThreadAsyncExecutor executor = new ThreadAsyncExecutor();
|
||||||
|
|
||||||
|
final Object result = new Object();
|
||||||
|
final Callable<Object> task = mock(Callable.class);
|
||||||
|
when(task.call()).thenReturn(result);
|
||||||
|
|
||||||
|
final AsyncResult<Object> asyncResult = executor.startProcess(task);
|
||||||
|
assertNotNull(asyncResult);
|
||||||
|
asyncResult.await(); // Prevent timing issues, and wait until the result is available
|
||||||
|
assertTrue(asyncResult.isCompleted());
|
||||||
|
|
||||||
|
// Our task should only execute once ...
|
||||||
|
verify(task, times(1)).call();
|
||||||
|
|
||||||
|
// ... and the result should be exactly the same object
|
||||||
|
assertSame(result, asyncResult.getValue());
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Test used to verify the happy path of {@link ThreadAsyncExecutor#startProcess(Callable, AsyncCallback)}
|
||||||
|
*/
|
||||||
|
@Test(timeout = 3000)
|
||||||
|
public void testSuccessfulTaskWithCallback() throws Exception {
|
||||||
|
// Instantiate a new executor and start a new 'null' task ...
|
||||||
|
final ThreadAsyncExecutor executor = new ThreadAsyncExecutor();
|
||||||
|
|
||||||
|
final Object result = new Object();
|
||||||
|
final Callable<Object> task = mock(Callable.class);
|
||||||
|
when(task.call()).thenReturn(result);
|
||||||
|
|
||||||
|
final AsyncCallback callback = mock(AsyncCallback.class);
|
||||||
|
final AsyncResult<Object> asyncResult = executor.startProcess(task, callback);
|
||||||
|
assertNotNull(asyncResult);
|
||||||
|
asyncResult.await(); // Prevent timing issues, and wait until the result is available
|
||||||
|
assertTrue(asyncResult.isCompleted());
|
||||||
|
|
||||||
|
// Our task should only execute once ...
|
||||||
|
verify(task, times(1)).call();
|
||||||
|
|
||||||
|
// ... same for the callback, we expect our object
|
||||||
|
final ArgumentCaptor<Optional<Exception>> optionalCaptor = ArgumentCaptor.forClass((Class) Optional.class);
|
||||||
|
verify(callback, times(1)).onComplete(eq(result), optionalCaptor.capture());
|
||||||
|
|
||||||
|
final Optional<Exception> optionalException = optionalCaptor.getValue();
|
||||||
|
assertNotNull(optionalException);
|
||||||
|
assertFalse(optionalException.isPresent());
|
||||||
|
|
||||||
|
// ... and the result should be exactly the same object
|
||||||
|
assertSame(result, asyncResult.getValue());
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Test used to verify the happy path of {@link ThreadAsyncExecutor#startProcess(Callable)} when a task takes a while
|
||||||
|
* to execute
|
||||||
|
*/
|
||||||
|
@Test(timeout = 5000)
|
||||||
|
public void testLongRunningTaskWithoutCallback() throws Exception {
|
||||||
|
// Instantiate a new executor and start a new 'null' task ...
|
||||||
|
final ThreadAsyncExecutor executor = new ThreadAsyncExecutor();
|
||||||
|
|
||||||
|
final Object result = new Object();
|
||||||
|
final Callable<Object> task = mock(Callable.class);
|
||||||
|
when(task.call()).thenAnswer(i -> {
|
||||||
|
Thread.sleep(1500);
|
||||||
|
return result;
|
||||||
|
});
|
||||||
|
|
||||||
|
final AsyncResult<Object> asyncResult = executor.startProcess(task);
|
||||||
|
assertNotNull(asyncResult);
|
||||||
|
assertFalse(asyncResult.isCompleted());
|
||||||
|
|
||||||
|
try {
|
||||||
|
asyncResult.getValue();
|
||||||
|
fail("Expected IllegalStateException when calling AsyncResult#getValue on a non-completed task");
|
||||||
|
} catch (IllegalStateException e) {
|
||||||
|
assertNotNull(e.getMessage());
|
||||||
|
}
|
||||||
|
|
||||||
|
// Our task should only execute once, but it can take a while ...
|
||||||
|
verify(task, timeout(3000).times(1)).call();
|
||||||
|
|
||||||
|
// Prevent timing issues, and wait until the result is available
|
||||||
|
asyncResult.await();
|
||||||
|
assertTrue(asyncResult.isCompleted());
|
||||||
|
verifyNoMoreInteractions(task);
|
||||||
|
|
||||||
|
// ... and the result should be exactly the same object
|
||||||
|
assertSame(result, asyncResult.getValue());
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Test used to verify the happy path of {@link ThreadAsyncExecutor#startProcess(Callable, AsyncCallback)} when a task
|
||||||
|
* takes a while to execute
|
||||||
|
*/
|
||||||
|
@Test(timeout = 5000)
|
||||||
|
public void testLongRunningTaskWithCallback() throws Exception {
|
||||||
|
// Instantiate a new executor and start a new 'null' task ...
|
||||||
|
final ThreadAsyncExecutor executor = new ThreadAsyncExecutor();
|
||||||
|
|
||||||
|
final Object result = new Object();
|
||||||
|
final Callable<Object> task = mock(Callable.class);
|
||||||
|
when(task.call()).thenAnswer(i -> {
|
||||||
|
Thread.sleep(1500);
|
||||||
|
return result;
|
||||||
|
});
|
||||||
|
|
||||||
|
final AsyncCallback<Object> callback = mock(AsyncCallback.class);
|
||||||
|
final AsyncResult<Object> asyncResult = executor.startProcess(task, callback);
|
||||||
|
assertNotNull(asyncResult);
|
||||||
|
assertFalse(asyncResult.isCompleted());
|
||||||
|
|
||||||
|
verifyZeroInteractions(callback);
|
||||||
|
|
||||||
|
try {
|
||||||
|
asyncResult.getValue();
|
||||||
|
fail("Expected IllegalStateException when calling AsyncResult#getValue on a non-completed task");
|
||||||
|
} catch (IllegalStateException e) {
|
||||||
|
assertNotNull(e.getMessage());
|
||||||
|
}
|
||||||
|
|
||||||
|
// Our task should only execute once, but it can take a while ...
|
||||||
|
verify(task, timeout(3000).times(1)).call();
|
||||||
|
|
||||||
|
final ArgumentCaptor<Optional<Exception>> optionalCaptor = ArgumentCaptor.forClass((Class) Optional.class);
|
||||||
|
verify(callback, timeout(3000).times(1)).onComplete(eq(result), optionalCaptor.capture());
|
||||||
|
|
||||||
|
final Optional<Exception> optionalException = optionalCaptor.getValue();
|
||||||
|
assertNotNull(optionalException);
|
||||||
|
assertFalse(optionalException.isPresent());
|
||||||
|
|
||||||
|
// Prevent timing issues, and wait until the result is available
|
||||||
|
asyncResult.await();
|
||||||
|
assertTrue(asyncResult.isCompleted());
|
||||||
|
verifyNoMoreInteractions(task, callback);
|
||||||
|
|
||||||
|
// ... and the result should be exactly the same object
|
||||||
|
assertSame(result, asyncResult.getValue());
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Test used to verify the happy path of {@link ThreadAsyncExecutor#startProcess(Callable)} when a task takes a while
|
||||||
|
* to execute, while waiting on the result using {@link ThreadAsyncExecutor#endProcess(AsyncResult)}
|
||||||
|
*/
|
||||||
|
@Test(timeout = 5000)
|
||||||
|
public void testEndProcess() throws Exception {
|
||||||
|
// Instantiate a new executor and start a new 'null' task ...
|
||||||
|
final ThreadAsyncExecutor executor = new ThreadAsyncExecutor();
|
||||||
|
|
||||||
|
final Object result = new Object();
|
||||||
|
final Callable<Object> task = mock(Callable.class);
|
||||||
|
when(task.call()).thenAnswer(i -> {
|
||||||
|
Thread.sleep(1500);
|
||||||
|
return result;
|
||||||
|
});
|
||||||
|
|
||||||
|
final AsyncResult<Object> asyncResult = executor.startProcess(task);
|
||||||
|
assertNotNull(asyncResult);
|
||||||
|
assertFalse(asyncResult.isCompleted());
|
||||||
|
|
||||||
|
try {
|
||||||
|
asyncResult.getValue();
|
||||||
|
fail("Expected IllegalStateException when calling AsyncResult#getValue on a non-completed task");
|
||||||
|
} catch (IllegalStateException e) {
|
||||||
|
assertNotNull(e.getMessage());
|
||||||
|
}
|
||||||
|
|
||||||
|
assertSame(result, executor.endProcess(asyncResult));
|
||||||
|
verify(task, times(1)).call();
|
||||||
|
assertTrue(asyncResult.isCompleted());
|
||||||
|
|
||||||
|
// Calling end process a second time while already finished should give the same result
|
||||||
|
assertSame(result, executor.endProcess(asyncResult));
|
||||||
|
verifyNoMoreInteractions(task);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Test used to verify the behaviour of {@link ThreadAsyncExecutor#startProcess(Callable)} when the callable is 'null'
|
||||||
|
*/
|
||||||
|
@Test(timeout = 3000)
|
||||||
|
public void testNullTask() throws Exception {
|
||||||
|
// Instantiate a new executor and start a new 'null' task ...
|
||||||
|
final ThreadAsyncExecutor executor = new ThreadAsyncExecutor();
|
||||||
|
final AsyncResult<Object> asyncResult = executor.startProcess(null);
|
||||||
|
|
||||||
|
assertNotNull("The AsyncResult should not be 'null', even though the task was 'null'.", asyncResult);
|
||||||
|
asyncResult.await(); // Prevent timing issues, and wait until the result is available
|
||||||
|
assertTrue(asyncResult.isCompleted());
|
||||||
|
|
||||||
|
try {
|
||||||
|
asyncResult.getValue();
|
||||||
|
fail("Expected ExecutionException with NPE as cause");
|
||||||
|
} catch (final ExecutionException e) {
|
||||||
|
assertNotNull(e.getMessage());
|
||||||
|
assertNotNull(e.getCause());
|
||||||
|
assertEquals(NullPointerException.class, e.getCause().getClass());
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Test used to verify the behaviour of {@link ThreadAsyncExecutor#startProcess(Callable, AsyncCallback)} when the
|
||||||
|
* callable is 'null', but the asynchronous callback is provided
|
||||||
|
*/
|
||||||
|
@Test(timeout = 3000)
|
||||||
|
public void testNullTaskWithCallback() throws Exception {
|
||||||
|
// Instantiate a new executor and start a new 'null' task ...
|
||||||
|
final ThreadAsyncExecutor executor = new ThreadAsyncExecutor();
|
||||||
|
final AsyncCallback<Object> callback = mock(AsyncCallback.class);
|
||||||
|
final AsyncResult<Object> asyncResult = executor.startProcess(null, callback);
|
||||||
|
|
||||||
|
assertNotNull("The AsyncResult should not be 'null', even though the task was 'null'.", asyncResult);
|
||||||
|
asyncResult.await(); // Prevent timing issues, and wait until the result is available
|
||||||
|
assertTrue(asyncResult.isCompleted());
|
||||||
|
|
||||||
|
final ArgumentCaptor<Optional<Exception>> optionalCaptor = ArgumentCaptor.forClass((Class) Optional.class);
|
||||||
|
verify(callback, times(1)).onComplete(Matchers.isNull(), optionalCaptor.capture());
|
||||||
|
|
||||||
|
final Optional<Exception> optionalException = optionalCaptor.getValue();
|
||||||
|
assertNotNull(optionalException);
|
||||||
|
assertTrue(optionalException.isPresent());
|
||||||
|
|
||||||
|
final Exception exception = optionalException.get();
|
||||||
|
assertNotNull(exception);
|
||||||
|
assertEquals(NullPointerException.class, exception.getClass());
|
||||||
|
|
||||||
|
try {
|
||||||
|
asyncResult.getValue();
|
||||||
|
fail("Expected ExecutionException with NPE as cause");
|
||||||
|
} catch (final ExecutionException e) {
|
||||||
|
assertNotNull(e.getMessage());
|
||||||
|
assertNotNull(e.getCause());
|
||||||
|
assertEquals(NullPointerException.class, e.getCause().getClass());
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Test used to verify the behaviour of {@link ThreadAsyncExecutor#startProcess(Callable, AsyncCallback)} when both
|
||||||
|
* the callable and the asynchronous callback are 'null'
|
||||||
|
*/
|
||||||
|
@Test(timeout = 3000)
|
||||||
|
public void testNullTaskWithNullCallback() throws Exception {
|
||||||
|
// Instantiate a new executor and start a new 'null' task ...
|
||||||
|
final ThreadAsyncExecutor executor = new ThreadAsyncExecutor();
|
||||||
|
final AsyncResult<Object> asyncResult = executor.startProcess(null, null);
|
||||||
|
|
||||||
|
assertNotNull("The AsyncResult should not be 'null', even though the task and callback were 'null'.", asyncResult);
|
||||||
|
asyncResult.await(); // Prevent timing issues, and wait until the result is available
|
||||||
|
assertTrue(asyncResult.isCompleted());
|
||||||
|
|
||||||
|
try {
|
||||||
|
asyncResult.getValue();
|
||||||
|
fail("Expected ExecutionException with NPE as cause");
|
||||||
|
} catch (final ExecutionException e) {
|
||||||
|
assertNotNull(e.getMessage());
|
||||||
|
assertNotNull(e.getCause());
|
||||||
|
assertEquals(NullPointerException.class, e.getCause().getClass());
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -7,17 +7,20 @@ categories: Structural
|
|||||||
tags:
|
tags:
|
||||||
- Java
|
- Java
|
||||||
- Gang Of Four
|
- Gang Of Four
|
||||||
|
- Difficulty-Intermediate
|
||||||
---
|
---
|
||||||
|
|
||||||
**Also known as:** Handle/Body
|
## Also known as
|
||||||
|
Handle/Body
|
||||||
|
|
||||||
**Intent:** Decouple an abstraction from its implementation so that the two can
|
## Intent
|
||||||
|
Decouple an abstraction from its implementation so that the two can
|
||||||
vary independently.
|
vary independently.
|
||||||
|
|
||||||
|
|
||||||

|

|
||||||
|
|
||||||
**Applicability:** Use the Bridge pattern when
|
## Applicability
|
||||||
|
Use the Bridge pattern when
|
||||||
|
|
||||||
* you want to avoid a permanent binding between an abstraction and its implementation. This might be the case, for example, when the implementation must be selected or switched at run-time.
|
* you want to avoid a permanent binding between an abstraction and its implementation. This might be the case, for example, when the implementation must be selected or switched at run-time.
|
||||||
* both the abstractions and their implementations should be extensible by subclassing. In this case, the Bridge pattern lets you combine the different abstractions and implementations and extend them independently
|
* both the abstractions and their implementations should be extensible by subclassing. In this case, the Bridge pattern lets you combine the different abstractions and implementations and extend them independently
|
||||||
@ -25,6 +28,6 @@ vary independently.
|
|||||||
* you have a proliferation of classes. Such a class hierarchy indicates the need for splitting an object into two parts. Rumbaugh uses the term "nested generalizations" to refer to such class hierarchies
|
* you have a proliferation of classes. Such a class hierarchy indicates the need for splitting an object into two parts. Rumbaugh uses the term "nested generalizations" to refer to such class hierarchies
|
||||||
* you want to share an implementation among multiple objects (perhaps using reference counting), and this fact should be hidden from the client. A simple example is Coplien's String class, in which multiple objects can share the same string representation.
|
* you want to share an implementation among multiple objects (perhaps using reference counting), and this fact should be hidden from the client. A simple example is Coplien's String class, in which multiple objects can share the same string representation.
|
||||||
|
|
||||||
**Credits**
|
## Credits
|
||||||
|
|
||||||
* [Design Patterns: Elements of Reusable Object-Oriented Software](http://www.amazon.com/Design-Patterns-Elements-Reusable-Object-Oriented/dp/0201633612)
|
* [Design Patterns: Elements of Reusable Object-Oriented Software](http://www.amazon.com/Design-Patterns-Elements-Reusable-Object-Oriented/dp/0201633612)
|
||||||
|
@ -1,11 +1,35 @@
|
|||||||
<?xml version="1.0"?>
|
<?xml version="1.0"?>
|
||||||
|
<!--
|
||||||
|
|
||||||
|
The MIT License
|
||||||
|
Copyright (c) 2014 Ilkka Seppälä
|
||||||
|
|
||||||
|
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||||
|
of this software and associated documentation files (the "Software"), to deal
|
||||||
|
in the Software without restriction, including without limitation the rights
|
||||||
|
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||||
|
copies of the Software, and to permit persons to whom the Software is
|
||||||
|
furnished to do so, subject to the following conditions:
|
||||||
|
|
||||||
|
The above copyright notice and this permission notice shall be included in
|
||||||
|
all copies or substantial portions of the Software.
|
||||||
|
|
||||||
|
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||||
|
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||||
|
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||||
|
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||||
|
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||||
|
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||||
|
THE SOFTWARE.
|
||||||
|
|
||||||
|
-->
|
||||||
<project xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" xmlns="http://maven.apache.org/POM/4.0.0"
|
<project xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" xmlns="http://maven.apache.org/POM/4.0.0"
|
||||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
|
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
|
||||||
<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.8.0</version>
|
<version>1.10.0</version>
|
||||||
</parent>
|
</parent>
|
||||||
<artifactId>bridge</artifactId>
|
<artifactId>bridge</artifactId>
|
||||||
<dependencies>
|
<dependencies>
|
||||||
@ -14,5 +38,10 @@
|
|||||||
<artifactId>junit</artifactId>
|
<artifactId>junit</artifactId>
|
||||||
<scope>test</scope>
|
<scope>test</scope>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.mockito</groupId>
|
||||||
|
<artifactId>mockito-core</artifactId>
|
||||||
|
<scope>test</scope>
|
||||||
|
</dependency>
|
||||||
</dependencies>
|
</dependencies>
|
||||||
</project>
|
</project>
|
||||||
|
@ -1,3 +1,25 @@
|
|||||||
|
/**
|
||||||
|
* The MIT License
|
||||||
|
* Copyright (c) 2014 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.bridge;
|
package com.iluwatar.bridge;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -1,3 +1,25 @@
|
|||||||
|
/**
|
||||||
|
* The MIT License
|
||||||
|
* Copyright (c) 2014 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.bridge;
|
package com.iluwatar.bridge;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -1,3 +1,25 @@
|
|||||||
|
/**
|
||||||
|
* The MIT License
|
||||||
|
* Copyright (c) 2014 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.bridge;
|
package com.iluwatar.bridge;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -1,3 +1,25 @@
|
|||||||
|
/**
|
||||||
|
* The MIT License
|
||||||
|
* Copyright (c) 2014 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.bridge;
|
package com.iluwatar.bridge;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -1,3 +1,25 @@
|
|||||||
|
/**
|
||||||
|
* The MIT License
|
||||||
|
* Copyright (c) 2014 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.bridge;
|
package com.iluwatar.bridge;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -1,3 +1,25 @@
|
|||||||
|
/**
|
||||||
|
* The MIT License
|
||||||
|
* Copyright (c) 2014 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.bridge;
|
package com.iluwatar.bridge;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -1,3 +1,25 @@
|
|||||||
|
/**
|
||||||
|
* The MIT License
|
||||||
|
* Copyright (c) 2014 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.bridge;
|
package com.iluwatar.bridge;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -1,3 +1,25 @@
|
|||||||
|
/**
|
||||||
|
* The MIT License
|
||||||
|
* Copyright (c) 2014 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.bridge;
|
package com.iluwatar.bridge;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -1,3 +1,25 @@
|
|||||||
|
/**
|
||||||
|
* The MIT License
|
||||||
|
* Copyright (c) 2014 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.bridge;
|
package com.iluwatar.bridge;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -1,3 +1,25 @@
|
|||||||
|
/**
|
||||||
|
* The MIT License
|
||||||
|
* Copyright (c) 2014 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.bridge;
|
package com.iluwatar.bridge;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -1,3 +1,25 @@
|
|||||||
|
/**
|
||||||
|
* The MIT License
|
||||||
|
* Copyright (c) 2014 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.bridge;
|
package com.iluwatar.bridge;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -1,3 +1,25 @@
|
|||||||
|
/**
|
||||||
|
* The MIT License
|
||||||
|
* Copyright (c) 2014 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.bridge;
|
package com.iluwatar.bridge;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -1,9 +1,29 @@
|
|||||||
|
/**
|
||||||
|
* The MIT License
|
||||||
|
* Copyright (c) 2014 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.bridge;
|
package com.iluwatar.bridge;
|
||||||
|
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
|
|
||||||
import com.iluwatar.bridge.App;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
*
|
*
|
||||||
* Application test
|
* Application test
|
||||||
|
@ -0,0 +1,55 @@
|
|||||||
|
/**
|
||||||
|
* The MIT License
|
||||||
|
* Copyright (c) 2014 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.bridge;
|
||||||
|
|
||||||
|
import org.junit.Test;
|
||||||
|
|
||||||
|
import static org.mockito.Mockito.spy;
|
||||||
|
import static org.mockito.Mockito.verify;
|
||||||
|
import static org.mockito.Mockito.verifyNoMoreInteractions;
|
||||||
|
import static org.mockito.internal.verification.VerificationModeFactory.times;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Date: 12/6/15 - 11:15 PM
|
||||||
|
*
|
||||||
|
* @author Jeroen Meulemeester
|
||||||
|
*/
|
||||||
|
public class BlindingMagicWeaponTest extends MagicWeaponTest {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Invoke all possible actions on the weapon and check if the actions are executed on the actual
|
||||||
|
* underlying weapon implementation.
|
||||||
|
*/
|
||||||
|
@Test
|
||||||
|
public void testExcalibur() throws Exception {
|
||||||
|
final Excalibur excalibur = spy(new Excalibur());
|
||||||
|
final BlindingMagicWeapon blindingMagicWeapon = new BlindingMagicWeapon(excalibur);
|
||||||
|
|
||||||
|
testBasicWeaponActions(blindingMagicWeapon, excalibur);
|
||||||
|
|
||||||
|
blindingMagicWeapon.blind();
|
||||||
|
verify(excalibur, times(1)).blindImp();
|
||||||
|
verifyNoMoreInteractions(excalibur);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -0,0 +1,55 @@
|
|||||||
|
/**
|
||||||
|
* The MIT License
|
||||||
|
* Copyright (c) 2014 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.bridge;
|
||||||
|
|
||||||
|
import org.junit.Test;
|
||||||
|
|
||||||
|
import static org.mockito.Mockito.spy;
|
||||||
|
import static org.mockito.Mockito.verify;
|
||||||
|
import static org.mockito.Mockito.verifyNoMoreInteractions;
|
||||||
|
import static org.mockito.internal.verification.VerificationModeFactory.times;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Date: 12/6/15 - 11:26 PM
|
||||||
|
*
|
||||||
|
* @author Jeroen Meulemeester
|
||||||
|
*/
|
||||||
|
public class FlyingMagicWeaponTest extends MagicWeaponTest {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Invoke all possible actions on the weapon and check if the actions are executed on the actual
|
||||||
|
* underlying weapon implementation.
|
||||||
|
*/
|
||||||
|
@Test
|
||||||
|
public void testMjollnir() throws Exception {
|
||||||
|
final Mjollnir mjollnir = spy(new Mjollnir());
|
||||||
|
final FlyingMagicWeapon flyingMagicWeapon = new FlyingMagicWeapon(mjollnir);
|
||||||
|
|
||||||
|
testBasicWeaponActions(flyingMagicWeapon, mjollnir);
|
||||||
|
|
||||||
|
flyingMagicWeapon.fly();
|
||||||
|
verify(mjollnir, times(1)).flyImp();
|
||||||
|
verifyNoMoreInteractions(mjollnir);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -0,0 +1,64 @@
|
|||||||
|
/**
|
||||||
|
* The MIT License
|
||||||
|
* Copyright (c) 2014 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.bridge;
|
||||||
|
|
||||||
|
import static org.junit.Assert.assertNotNull;
|
||||||
|
import static org.mockito.Mockito.verify;
|
||||||
|
import static org.mockito.Mockito.verifyNoMoreInteractions;
|
||||||
|
import static org.mockito.internal.verification.VerificationModeFactory.times;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Date: 12/6/15 - 11:28 PM
|
||||||
|
*
|
||||||
|
* @author Jeroen Meulemeester
|
||||||
|
*/
|
||||||
|
public abstract class MagicWeaponTest {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Invoke the basic actions of the given weapon, and test if the underlying weapon implementation
|
||||||
|
* is invoked
|
||||||
|
*
|
||||||
|
* @param weaponImpl The spied weapon implementation where actions are bridged to
|
||||||
|
* @param weapon The weapon, handled by the app
|
||||||
|
*/
|
||||||
|
protected final void testBasicWeaponActions(final MagicWeapon weapon,
|
||||||
|
final MagicWeaponImpl weaponImpl) {
|
||||||
|
assertNotNull(weapon);
|
||||||
|
assertNotNull(weaponImpl);
|
||||||
|
assertNotNull(weapon.getImp());
|
||||||
|
|
||||||
|
weapon.swing();
|
||||||
|
verify(weaponImpl, times(1)).swingImp();
|
||||||
|
verifyNoMoreInteractions(weaponImpl);
|
||||||
|
|
||||||
|
weapon.wield();
|
||||||
|
verify(weaponImpl, times(1)).wieldImp();
|
||||||
|
verifyNoMoreInteractions(weaponImpl);
|
||||||
|
|
||||||
|
weapon.unwield();
|
||||||
|
verify(weaponImpl, times(1)).unwieldImp();
|
||||||
|
verifyNoMoreInteractions(weaponImpl);
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -0,0 +1,55 @@
|
|||||||
|
/**
|
||||||
|
* The MIT License
|
||||||
|
* Copyright (c) 2014 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.bridge;
|
||||||
|
|
||||||
|
import org.junit.Test;
|
||||||
|
|
||||||
|
import static org.mockito.Mockito.spy;
|
||||||
|
import static org.mockito.Mockito.verify;
|
||||||
|
import static org.mockito.Mockito.verifyNoMoreInteractions;
|
||||||
|
import static org.mockito.internal.verification.VerificationModeFactory.times;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Date: 12/6/15 - 11:43 PM
|
||||||
|
*
|
||||||
|
* @author Jeroen Meulemeester
|
||||||
|
*/
|
||||||
|
public class SoulEatingMagicWeaponTest extends MagicWeaponTest {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Invoke all possible actions on the weapon and check if the actions are executed on the actual
|
||||||
|
* underlying weapon implementation.
|
||||||
|
*/
|
||||||
|
@Test
|
||||||
|
public void testStormBringer() throws Exception {
|
||||||
|
final Stormbringer stormbringer = spy(new Stormbringer());
|
||||||
|
final SoulEatingMagicWeapon soulEatingMagicWeapon = new SoulEatingMagicWeapon(stormbringer);
|
||||||
|
|
||||||
|
testBasicWeaponActions(soulEatingMagicWeapon, stormbringer);
|
||||||
|
|
||||||
|
soulEatingMagicWeapon.eatSoul();
|
||||||
|
verify(stormbringer, times(1)).eatSoulImp();
|
||||||
|
verifyNoMoreInteractions(stormbringer);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -7,24 +7,28 @@ categories: Creational
|
|||||||
tags:
|
tags:
|
||||||
- Java
|
- Java
|
||||||
- Gang Of Four
|
- Gang Of Four
|
||||||
|
- Difficulty-Intermediate
|
||||||
---
|
---
|
||||||
|
|
||||||
**Intent:** Separate the construction of a complex object from its
|
## Intent
|
||||||
|
Separate the construction of a complex object from its
|
||||||
representation so that the same construction process can create different
|
representation so that the same construction process can create different
|
||||||
representations.
|
representations.
|
||||||
|
|
||||||

|

|
||||||
|
|
||||||
**Applicability:** Use the Builder pattern when
|
## Applicability
|
||||||
|
Use the Builder pattern when
|
||||||
|
|
||||||
* the algorithm for creating a complex object should be independent of the parts that make up the object and how they're assembled
|
* the algorithm for creating a complex object should be independent of the parts that make up the object and how they're assembled
|
||||||
* the construction process must allow different representations for the object that's constructed
|
* the construction process must allow different representations for the object that's constructed
|
||||||
|
|
||||||
**Real world examples:**
|
## Real world examples
|
||||||
|
|
||||||
* [java.lang.StringBuilder](http://docs.oracle.com/javase/8/docs/api/java/lang/StringBuilder.html)
|
* [java.lang.StringBuilder](http://docs.oracle.com/javase/8/docs/api/java/lang/StringBuilder.html)
|
||||||
* [Apache Camel builders](https://github.com/apache/camel/tree/0e195428ee04531be27a0b659005e3aa8d159d23/camel-core/src/main/java/org/apache/camel/builder)
|
* [Apache Camel builders](https://github.com/apache/camel/tree/0e195428ee04531be27a0b659005e3aa8d159d23/camel-core/src/main/java/org/apache/camel/builder)
|
||||||
|
|
||||||
**Credits**
|
## Credits
|
||||||
|
|
||||||
* [Design Patterns: Elements of Reusable Object-Oriented Software](http://www.amazon.com/Design-Patterns-Elements-Reusable-Object-Oriented/dp/0201633612)
|
* [Design Patterns: Elements of Reusable Object-Oriented Software](http://www.amazon.com/Design-Patterns-Elements-Reusable-Object-Oriented/dp/0201633612)
|
||||||
|
* [Effective Java (2nd Edition)](http://www.amazon.com/Effective-Java-Edition-Joshua-Bloch/dp/0321356683)
|
||||||
|
@ -1,11 +1,35 @@
|
|||||||
<?xml version="1.0"?>
|
<?xml version="1.0"?>
|
||||||
|
<!--
|
||||||
|
|
||||||
|
The MIT License
|
||||||
|
Copyright (c) 2014 Ilkka Seppälä
|
||||||
|
|
||||||
|
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||||
|
of this software and associated documentation files (the "Software"), to deal
|
||||||
|
in the Software without restriction, including without limitation the rights
|
||||||
|
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||||
|
copies of the Software, and to permit persons to whom the Software is
|
||||||
|
furnished to do so, subject to the following conditions:
|
||||||
|
|
||||||
|
The above copyright notice and this permission notice shall be included in
|
||||||
|
all copies or substantial portions of the Software.
|
||||||
|
|
||||||
|
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||||
|
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||||
|
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||||
|
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||||
|
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||||
|
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||||
|
THE SOFTWARE.
|
||||||
|
|
||||||
|
-->
|
||||||
<project xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" xmlns="http://maven.apache.org/POM/4.0.0"
|
<project xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" xmlns="http://maven.apache.org/POM/4.0.0"
|
||||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
|
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
|
||||||
<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.8.0</version>
|
<version>1.10.0</version>
|
||||||
</parent>
|
</parent>
|
||||||
<artifactId>builder</artifactId>
|
<artifactId>builder</artifactId>
|
||||||
<dependencies>
|
<dependencies>
|
||||||
|
@ -1,3 +1,25 @@
|
|||||||
|
/**
|
||||||
|
* The MIT License
|
||||||
|
* Copyright (c) 2014 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.builder;
|
package com.iluwatar.builder;
|
||||||
|
|
||||||
import com.iluwatar.builder.Hero.HeroBuilder;
|
import com.iluwatar.builder.Hero.HeroBuilder;
|
||||||
|
@ -1,3 +1,25 @@
|
|||||||
|
/**
|
||||||
|
* The MIT License
|
||||||
|
* Copyright (c) 2014 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.builder;
|
package com.iluwatar.builder;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -1,3 +1,25 @@
|
|||||||
|
/**
|
||||||
|
* The MIT License
|
||||||
|
* Copyright (c) 2014 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.builder;
|
package com.iluwatar.builder;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -1,3 +1,25 @@
|
|||||||
|
/**
|
||||||
|
* The MIT License
|
||||||
|
* Copyright (c) 2014 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.builder;
|
package com.iluwatar.builder;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -1,3 +1,25 @@
|
|||||||
|
/**
|
||||||
|
* The MIT License
|
||||||
|
* Copyright (c) 2014 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.builder;
|
package com.iluwatar.builder;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -42,31 +64,27 @@ public class Hero {
|
|||||||
public String toString() {
|
public String toString() {
|
||||||
|
|
||||||
StringBuilder sb = new StringBuilder();
|
StringBuilder sb = new StringBuilder();
|
||||||
sb.append("This is a ");
|
sb.append("This is a ")
|
||||||
sb.append(profession);
|
.append(profession)
|
||||||
sb.append(" named ");
|
.append(" named ")
|
||||||
sb.append(name);
|
.append(name);
|
||||||
if (hairColor != null || hairType != null) {
|
if (hairColor != null || hairType != null) {
|
||||||
sb.append(" with ");
|
sb.append(" with ");
|
||||||
if (hairColor != null) {
|
if (hairColor != null) {
|
||||||
sb.append(hairColor);
|
sb.append(hairColor).append(' ');
|
||||||
sb.append(" ");
|
|
||||||
}
|
}
|
||||||
if (hairType != null) {
|
if (hairType != null) {
|
||||||
sb.append(hairType);
|
sb.append(hairType).append(' ');
|
||||||
sb.append(" ");
|
|
||||||
}
|
}
|
||||||
sb.append(hairType != HairType.BALD ? "hair" : "head");
|
sb.append(hairType != HairType.BALD ? "hair" : "head");
|
||||||
}
|
}
|
||||||
if (armor != null) {
|
if (armor != null) {
|
||||||
sb.append(" wearing ");
|
sb.append(" wearing ").append(armor);
|
||||||
sb.append(armor);
|
|
||||||
}
|
}
|
||||||
if (weapon != null) {
|
if (weapon != null) {
|
||||||
sb.append(" and wielding a ");
|
sb.append(" and wielding a ").append(weapon);
|
||||||
sb.append(weapon);
|
|
||||||
}
|
}
|
||||||
sb.append(".");
|
sb.append('.');
|
||||||
return sb.toString();
|
return sb.toString();
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -93,6 +111,9 @@ public class Hero {
|
|||||||
private Armor armor;
|
private Armor armor;
|
||||||
private Weapon weapon;
|
private Weapon weapon;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Constructor
|
||||||
|
*/
|
||||||
public HeroBuilder(Profession profession, String name) {
|
public HeroBuilder(Profession profession, String name) {
|
||||||
if (profession == null || name == null) {
|
if (profession == null || name == null) {
|
||||||
throw new IllegalArgumentException("profession and name can not be null");
|
throw new IllegalArgumentException("profession and name can not be null");
|
||||||
|
@ -1,3 +1,25 @@
|
|||||||
|
/**
|
||||||
|
* The MIT License
|
||||||
|
* Copyright (c) 2014 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.builder;
|
package com.iluwatar.builder;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -1,3 +1,25 @@
|
|||||||
|
/**
|
||||||
|
* The MIT License
|
||||||
|
* Copyright (c) 2014 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.builder;
|
package com.iluwatar.builder;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -1,9 +1,29 @@
|
|||||||
|
/**
|
||||||
|
* The MIT License
|
||||||
|
* Copyright (c) 2014 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.builder;
|
package com.iluwatar.builder;
|
||||||
|
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
|
|
||||||
import com.iluwatar.builder.App;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
*
|
*
|
||||||
* Application test
|
* Application test
|
||||||
|
78
builder/src/test/java/com/iluwatar/builder/HeroTest.java
Normal file
78
builder/src/test/java/com/iluwatar/builder/HeroTest.java
Normal file
@ -0,0 +1,78 @@
|
|||||||
|
/**
|
||||||
|
* The MIT License
|
||||||
|
* Copyright (c) 2014 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.builder;
|
||||||
|
|
||||||
|
import org.junit.Test;
|
||||||
|
|
||||||
|
import static org.junit.Assert.assertEquals;
|
||||||
|
import static org.junit.Assert.assertNotNull;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Date: 12/6/15 - 11:01 PM
|
||||||
|
*
|
||||||
|
* @author Jeroen Meulemeester
|
||||||
|
*/
|
||||||
|
public class HeroTest {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Test if we get the expected exception when trying to create a hero without a profession
|
||||||
|
*/
|
||||||
|
@Test(expected = IllegalArgumentException.class)
|
||||||
|
public void testMissingProfession() throws Exception {
|
||||||
|
new Hero.HeroBuilder(null, "Sir without a job");
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Test if we get the expected exception when trying to create a hero without a name
|
||||||
|
*/
|
||||||
|
@Test(expected = IllegalArgumentException.class)
|
||||||
|
public void testMissingName() throws Exception {
|
||||||
|
new Hero.HeroBuilder(Profession.THIEF, null);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Test if the hero build by the builder has the correct attributes, as requested
|
||||||
|
*/
|
||||||
|
@Test
|
||||||
|
public void testBuildHero() throws Exception {
|
||||||
|
final String heroName = "Sir Lancelot";
|
||||||
|
|
||||||
|
final Hero hero = new Hero.HeroBuilder(Profession.WARRIOR, heroName)
|
||||||
|
.withArmor(Armor.CHAIN_MAIL)
|
||||||
|
.withWeapon(Weapon.SWORD)
|
||||||
|
.withHairType(HairType.LONG_CURLY)
|
||||||
|
.withHairColor(HairColor.BLOND)
|
||||||
|
.build();
|
||||||
|
|
||||||
|
assertNotNull(hero);
|
||||||
|
assertNotNull(hero.toString());
|
||||||
|
assertEquals(Profession.WARRIOR, hero.getProfession());
|
||||||
|
assertEquals(heroName, hero.getName());
|
||||||
|
assertEquals(Armor.CHAIN_MAIL, hero.getArmor());
|
||||||
|
assertEquals(Weapon.SWORD, hero.getWeapon());
|
||||||
|
assertEquals(HairType.LONG_CURLY, hero.getHairType());
|
||||||
|
assertEquals(HairColor.BLOND, hero.getHairColor());
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -4,18 +4,25 @@ title: Business Delegate
|
|||||||
folder: business-delegate
|
folder: business-delegate
|
||||||
permalink: /patterns/business-delegate/
|
permalink: /patterns/business-delegate/
|
||||||
categories: Business Tier
|
categories: Business Tier
|
||||||
tags: Java
|
tags:
|
||||||
|
- Java
|
||||||
|
- Difficulty-Intermediate
|
||||||
---
|
---
|
||||||
|
|
||||||
**Intent:** The Business Delegate pattern adds an abstraction layer between
|
## Intent
|
||||||
|
The Business Delegate pattern adds an abstraction layer between
|
||||||
presentation and business tiers. By using the pattern we gain loose coupling
|
presentation and business tiers. By using the pattern we gain loose coupling
|
||||||
between the tiers and encapsulate knowledge about how to locate, connect to,
|
between the tiers and encapsulate knowledge about how to locate, connect to,
|
||||||
and interact with the business objects that make up the application.
|
and interact with the business objects that make up the application.
|
||||||
|
|
||||||

|

|
||||||
|
|
||||||
**Applicability:** Use the Business Delegate pattern when
|
## Applicability
|
||||||
|
Use the Business Delegate pattern when
|
||||||
|
|
||||||
* you want loose coupling between presentation and business tiers
|
* you want loose coupling between presentation and business tiers
|
||||||
* you want to orchestrate calls to multiple business services
|
* you want to orchestrate calls to multiple business services
|
||||||
* you want to encapsulate service lookups and service calls
|
* you want to encapsulate service lookups and service calls
|
||||||
|
|
||||||
|
##Credits
|
||||||
|
* [J2EE Design Patterns](http://www.amazon.com/J2EE-Design-Patterns-William-Crawford/dp/0596004273/ref=sr_1_2)
|
||||||
|
@ -1,4 +1,28 @@
|
|||||||
<?xml version="1.0"?>
|
<?xml version="1.0"?>
|
||||||
|
<!--
|
||||||
|
|
||||||
|
The MIT License
|
||||||
|
Copyright (c) 2014 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
|
<project
|
||||||
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"
|
||||||
xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
|
xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
|
||||||
@ -6,7 +30,7 @@
|
|||||||
<parent>
|
<parent>
|
||||||
<groupId>com.iluwatar</groupId>
|
<groupId>com.iluwatar</groupId>
|
||||||
<artifactId>java-design-patterns</artifactId>
|
<artifactId>java-design-patterns</artifactId>
|
||||||
<version>1.8.0</version>
|
<version>1.10.0</version>
|
||||||
</parent>
|
</parent>
|
||||||
<artifactId>business-delegate</artifactId>
|
<artifactId>business-delegate</artifactId>
|
||||||
<dependencies>
|
<dependencies>
|
||||||
|
@ -1,3 +1,25 @@
|
|||||||
|
/**
|
||||||
|
* The MIT License
|
||||||
|
* Copyright (c) 2014 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.business.delegate;
|
package com.iluwatar.business.delegate;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -1,3 +1,25 @@
|
|||||||
|
/**
|
||||||
|
* The MIT License
|
||||||
|
* Copyright (c) 2014 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.business.delegate;
|
package com.iluwatar.business.delegate;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -1,3 +1,25 @@
|
|||||||
|
/**
|
||||||
|
* The MIT License
|
||||||
|
* Copyright (c) 2014 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.business.delegate;
|
package com.iluwatar.business.delegate;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -1,3 +1,25 @@
|
|||||||
|
/**
|
||||||
|
* The MIT License
|
||||||
|
* Copyright (c) 2014 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.business.delegate;
|
package com.iluwatar.business.delegate;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -1,3 +1,25 @@
|
|||||||
|
/**
|
||||||
|
* The MIT License
|
||||||
|
* Copyright (c) 2014 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.business.delegate;
|
package com.iluwatar.business.delegate;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -1,3 +1,25 @@
|
|||||||
|
/**
|
||||||
|
* The MIT License
|
||||||
|
* Copyright (c) 2014 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.business.delegate;
|
package com.iluwatar.business.delegate;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -1,3 +1,25 @@
|
|||||||
|
/**
|
||||||
|
* The MIT License
|
||||||
|
* Copyright (c) 2014 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.business.delegate;
|
package com.iluwatar.business.delegate;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -1,3 +1,25 @@
|
|||||||
|
/**
|
||||||
|
* The MIT License
|
||||||
|
* Copyright (c) 2014 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.business.delegate;
|
package com.iluwatar.business.delegate;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -0,0 +1,38 @@
|
|||||||
|
/**
|
||||||
|
* The MIT License
|
||||||
|
* Copyright (c) 2014 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.business.delegate;
|
||||||
|
|
||||||
|
import org.junit.Test;
|
||||||
|
|
||||||
|
import java.io.IOException;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Tests that Business Delegate example runs without errors.
|
||||||
|
*/
|
||||||
|
public class AppTest {
|
||||||
|
@Test
|
||||||
|
public void test() throws IOException {
|
||||||
|
String[] args = {};
|
||||||
|
App.main(args);
|
||||||
|
}
|
||||||
|
}
|
@ -1,3 +1,25 @@
|
|||||||
|
/**
|
||||||
|
* The MIT License
|
||||||
|
* Copyright (c) 2014 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.business.delegate;
|
package com.iluwatar.business.delegate;
|
||||||
|
|
||||||
import static org.mockito.Mockito.spy;
|
import static org.mockito.Mockito.spy;
|
||||||
|
@ -6,19 +6,23 @@ permalink: /patterns/caching/
|
|||||||
categories: Other
|
categories: Other
|
||||||
tags:
|
tags:
|
||||||
- Java
|
- Java
|
||||||
|
- Difficulty-Intermediate
|
||||||
|
- Performance
|
||||||
---
|
---
|
||||||
|
|
||||||
**Intent:** To avoid expensive re-acquisition of resources by not releasing
|
## Intent
|
||||||
|
To avoid expensive re-acquisition of resources by not releasing
|
||||||
the resources immediately after their use. The resources retain their identity, are kept in some
|
the resources immediately after their use. The resources retain their identity, are kept in some
|
||||||
fast-access storage, and are re-used to avoid having to acquire them again.
|
fast-access storage, and are re-used to avoid having to acquire them again.
|
||||||
|
|
||||||

|

|
||||||
|
|
||||||
**Applicability:** Use the Caching pattern(s) when
|
## Applicability
|
||||||
|
Use the Caching pattern(s) when
|
||||||
|
|
||||||
* Repetitious acquisition, initialization, and release of the same resource causes unnecessary performance overhead.
|
* Repetitious acquisition, initialization, and release of the same resource causes unnecessary performance overhead.
|
||||||
|
|
||||||
**Credits**
|
## Credits
|
||||||
|
|
||||||
* [Write-through, write-around, write-back: Cache explained](http://www.computerweekly.com/feature/Write-through-write-around-write-back-Cache-explained)
|
* [Write-through, write-around, write-back: Cache explained](http://www.computerweekly.com/feature/Write-through-write-around-write-back-Cache-explained)
|
||||||
* [Read-Through, Write-Through, Write-Behind, and Refresh-Ahead Caching](https://docs.oracle.com/cd/E15357_01/coh.360/e15723/cache_rtwtwbra.htm#COHDG5177)
|
* [Read-Through, Write-Through, Write-Behind, and Refresh-Ahead Caching](https://docs.oracle.com/cd/E15357_01/coh.360/e15723/cache_rtwtwbra.htm#COHDG5177)
|
||||||
|
@ -1,11 +1,35 @@
|
|||||||
<?xml version="1.0"?>
|
<?xml version="1.0"?>
|
||||||
|
<!--
|
||||||
|
|
||||||
|
The MIT License
|
||||||
|
Copyright (c) 2014 Ilkka Seppälä
|
||||||
|
|
||||||
|
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||||
|
of this software and associated documentation files (the "Software"), to deal
|
||||||
|
in the Software without restriction, including without limitation the rights
|
||||||
|
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||||
|
copies of the Software, and to permit persons to whom the Software is
|
||||||
|
furnished to do so, subject to the following conditions:
|
||||||
|
|
||||||
|
The above copyright notice and this permission notice shall be included in
|
||||||
|
all copies or substantial portions of the Software.
|
||||||
|
|
||||||
|
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||||
|
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||||
|
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||||
|
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||||
|
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||||
|
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||||
|
THE SOFTWARE.
|
||||||
|
|
||||||
|
-->
|
||||||
<project xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" xmlns="http://maven.apache.org/POM/4.0.0"
|
<project xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" xmlns="http://maven.apache.org/POM/4.0.0"
|
||||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
|
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
|
||||||
<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.8.0</version>
|
<version>1.10.0</version>
|
||||||
</parent>
|
</parent>
|
||||||
<artifactId>caching</artifactId>
|
<artifactId>caching</artifactId>
|
||||||
<dependencies>
|
<dependencies>
|
||||||
|
@ -1,3 +1,25 @@
|
|||||||
|
/**
|
||||||
|
* The MIT License
|
||||||
|
* Copyright (c) 2014 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.caching;
|
package com.iluwatar.caching;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -21,7 +43,7 @@ package com.iluwatar.caching;
|
|||||||
* application data. The cache itself is implemented as an internal (Java) data structure. It adopts
|
* application data. The cache itself is implemented as an internal (Java) data structure. It adopts
|
||||||
* a Least-Recently-Used (LRU) strategy for evicting data from itself when its full. The three
|
* a Least-Recently-Used (LRU) strategy for evicting data from itself when its full. The three
|
||||||
* strategies are individually tested. The testing of the cache is restricted towards saving and
|
* strategies are individually tested. The testing of the cache is restricted towards saving and
|
||||||
* querying of user accounts from the underlying data store ( {@link DBManager}). The main class (
|
* querying of user accounts from the underlying data store ( {@link DbManager}). The main class (
|
||||||
* {@link App} is not aware of the underlying mechanics of the application (i.e. save and query) and
|
* {@link App} is not aware of the underlying mechanics of the application (i.e. save and query) and
|
||||||
* whether the data is coming from the cache or the DB (i.e. separation of concern). The AppManager
|
* whether the data is coming from the cache or the DB (i.e. separation of concern). The AppManager
|
||||||
* ({@link AppManager}) handles the transaction of data to-and-from the underlying data store
|
* ({@link AppManager}) handles the transaction of data to-and-from the underlying data store
|
||||||
@ -43,7 +65,7 @@ public class App {
|
|||||||
* @param args command line args
|
* @param args command line args
|
||||||
*/
|
*/
|
||||||
public static void main(String[] args) {
|
public static void main(String[] args) {
|
||||||
AppManager.initDB(false); // VirtualDB (instead of MongoDB) was used in running the JUnit tests
|
AppManager.initDb(false); // VirtualDB (instead of MongoDB) was used in running the JUnit tests
|
||||||
// and the App class to avoid Maven compilation errors. Set flag to
|
// and the App class to avoid Maven compilation errors. Set flag to
|
||||||
// true to run the tests with MongoDB (provided that MongoDB is
|
// true to run the tests with MongoDB (provided that MongoDB is
|
||||||
// installed and socket connection is open).
|
// installed and socket connection is open).
|
||||||
@ -65,8 +87,8 @@ public class App {
|
|||||||
|
|
||||||
AppManager.save(userAccount1);
|
AppManager.save(userAccount1);
|
||||||
System.out.println(AppManager.printCacheContent());
|
System.out.println(AppManager.printCacheContent());
|
||||||
userAccount1 = AppManager.find("001");
|
AppManager.find("001");
|
||||||
userAccount1 = AppManager.find("001");
|
AppManager.find("001");
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -80,15 +102,15 @@ public class App {
|
|||||||
|
|
||||||
AppManager.save(userAccount2);
|
AppManager.save(userAccount2);
|
||||||
System.out.println(AppManager.printCacheContent());
|
System.out.println(AppManager.printCacheContent());
|
||||||
userAccount2 = AppManager.find("002");
|
AppManager.find("002");
|
||||||
System.out.println(AppManager.printCacheContent());
|
System.out.println(AppManager.printCacheContent());
|
||||||
userAccount2 = AppManager.find("002");
|
userAccount2 = AppManager.find("002");
|
||||||
userAccount2.setUserName("Jane G.");
|
userAccount2.setUserName("Jane G.");
|
||||||
AppManager.save(userAccount2);
|
AppManager.save(userAccount2);
|
||||||
System.out.println(AppManager.printCacheContent());
|
System.out.println(AppManager.printCacheContent());
|
||||||
userAccount2 = AppManager.find("002");
|
AppManager.find("002");
|
||||||
System.out.println(AppManager.printCacheContent());
|
System.out.println(AppManager.printCacheContent());
|
||||||
userAccount2 = AppManager.find("002");
|
AppManager.find("002");
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -106,12 +128,12 @@ public class App {
|
|||||||
AppManager.save(userAccount4);
|
AppManager.save(userAccount4);
|
||||||
AppManager.save(userAccount5);
|
AppManager.save(userAccount5);
|
||||||
System.out.println(AppManager.printCacheContent());
|
System.out.println(AppManager.printCacheContent());
|
||||||
userAccount3 = AppManager.find("003");
|
AppManager.find("003");
|
||||||
System.out.println(AppManager.printCacheContent());
|
System.out.println(AppManager.printCacheContent());
|
||||||
UserAccount userAccount6 = new UserAccount("006", "Yasha", "She is an only child.");
|
UserAccount userAccount6 = new UserAccount("006", "Yasha", "She is an only child.");
|
||||||
AppManager.save(userAccount6);
|
AppManager.save(userAccount6);
|
||||||
System.out.println(AppManager.printCacheContent());
|
System.out.println(AppManager.printCacheContent());
|
||||||
userAccount4 = AppManager.find("004");
|
AppManager.find("004");
|
||||||
System.out.println(AppManager.printCacheContent());
|
System.out.println(AppManager.printCacheContent());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,3 +1,25 @@
|
|||||||
|
/**
|
||||||
|
* The MIT License
|
||||||
|
* Copyright (c) 2014 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.caching;
|
package com.iluwatar.caching;
|
||||||
|
|
||||||
import java.text.ParseException;
|
import java.text.ParseException;
|
||||||
@ -15,24 +37,30 @@ public class AppManager {
|
|||||||
|
|
||||||
private static CachingPolicy cachingPolicy;
|
private static CachingPolicy cachingPolicy;
|
||||||
|
|
||||||
|
private AppManager() {
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
*
|
*
|
||||||
* Developer/Tester is able to choose whether the application should use MongoDB as its underlying
|
* Developer/Tester is able to choose whether the application should use MongoDB as its underlying
|
||||||
* data storage or a simple Java data structure to (temporarily) store the data/objects during
|
* data storage or a simple Java data structure to (temporarily) store the data/objects during
|
||||||
* runtime.
|
* runtime.
|
||||||
*/
|
*/
|
||||||
public static void initDB(boolean useMongoDB) {
|
public static void initDb(boolean useMongoDb) {
|
||||||
if (useMongoDB) {
|
if (useMongoDb) {
|
||||||
try {
|
try {
|
||||||
DBManager.connect();
|
DbManager.connect();
|
||||||
} catch (ParseException e) {
|
} catch (ParseException e) {
|
||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
DBManager.createVirtualDB();
|
DbManager.createVirtualDb();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Initialize caching policy
|
||||||
|
*/
|
||||||
public static void initCachingPolicy(CachingPolicy policy) {
|
public static void initCachingPolicy(CachingPolicy policy) {
|
||||||
cachingPolicy = policy;
|
cachingPolicy = policy;
|
||||||
if (cachingPolicy == CachingPolicy.BEHIND) {
|
if (cachingPolicy == CachingPolicy.BEHIND) {
|
||||||
@ -50,15 +78,21 @@ public class AppManager {
|
|||||||
CacheStore.initCapacity(capacity);
|
CacheStore.initCapacity(capacity);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static UserAccount find(String userID) {
|
/**
|
||||||
|
* Find user account
|
||||||
|
*/
|
||||||
|
public static UserAccount find(String userId) {
|
||||||
if (cachingPolicy == CachingPolicy.THROUGH || cachingPolicy == CachingPolicy.AROUND) {
|
if (cachingPolicy == CachingPolicy.THROUGH || cachingPolicy == CachingPolicy.AROUND) {
|
||||||
return CacheStore.readThrough(userID);
|
return CacheStore.readThrough(userId);
|
||||||
} else if (cachingPolicy == CachingPolicy.BEHIND) {
|
} else if (cachingPolicy == CachingPolicy.BEHIND) {
|
||||||
return CacheStore.readThroughWithWriteBackPolicy(userID);
|
return CacheStore.readThroughWithWriteBackPolicy(userId);
|
||||||
}
|
}
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Save user account
|
||||||
|
*/
|
||||||
public static void save(UserAccount userAccount) {
|
public static void save(UserAccount userAccount) {
|
||||||
if (cachingPolicy == CachingPolicy.THROUGH) {
|
if (cachingPolicy == CachingPolicy.THROUGH) {
|
||||||
CacheStore.writeThrough(userAccount);
|
CacheStore.writeThrough(userAccount);
|
||||||
|
@ -1,3 +1,25 @@
|
|||||||
|
/**
|
||||||
|
* The MIT License
|
||||||
|
* Copyright (c) 2014 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.caching;
|
package com.iluwatar.caching;
|
||||||
|
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
@ -9,88 +31,118 @@ import java.util.ArrayList;
|
|||||||
*/
|
*/
|
||||||
public class CacheStore {
|
public class CacheStore {
|
||||||
|
|
||||||
static LRUCache cache = null;
|
static LruCache cache = null;
|
||||||
|
|
||||||
|
private CacheStore() {
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Init cache capacity
|
||||||
|
*/
|
||||||
public static void initCapacity(int capacity) {
|
public static void initCapacity(int capacity) {
|
||||||
if (null == cache)
|
if (null == cache) {
|
||||||
cache = new LRUCache(capacity);
|
cache = new LruCache(capacity);
|
||||||
else
|
} else {
|
||||||
cache.setCapacity(capacity);
|
cache.setCapacity(capacity);
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
public static UserAccount readThrough(String userID) {
|
/**
|
||||||
if (cache.contains(userID)) {
|
* Get user account using read-through cache
|
||||||
|
*/
|
||||||
|
public static UserAccount readThrough(String userId) {
|
||||||
|
if (cache.contains(userId)) {
|
||||||
System.out.println("# Cache Hit!");
|
System.out.println("# Cache Hit!");
|
||||||
return cache.get(userID);
|
return cache.get(userId);
|
||||||
}
|
}
|
||||||
System.out.println("# Cache Miss!");
|
System.out.println("# Cache Miss!");
|
||||||
UserAccount userAccount = DBManager.readFromDB(userID);
|
UserAccount userAccount = DbManager.readFromDb(userId);
|
||||||
cache.set(userID, userAccount);
|
cache.set(userId, userAccount);
|
||||||
return userAccount;
|
return userAccount;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get user account using write-through cache
|
||||||
|
*/
|
||||||
public static void writeThrough(UserAccount userAccount) {
|
public static void writeThrough(UserAccount userAccount) {
|
||||||
if (cache.contains(userAccount.getUserID())) {
|
if (cache.contains(userAccount.getUserId())) {
|
||||||
DBManager.updateDB(userAccount);
|
DbManager.updateDb(userAccount);
|
||||||
} else {
|
} else {
|
||||||
DBManager.writeToDB(userAccount);
|
DbManager.writeToDb(userAccount);
|
||||||
}
|
}
|
||||||
cache.set(userAccount.getUserID(), userAccount);
|
cache.set(userAccount.getUserId(), userAccount);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get user account using write-around cache
|
||||||
|
*/
|
||||||
public static void writeAround(UserAccount userAccount) {
|
public static void writeAround(UserAccount userAccount) {
|
||||||
if (cache.contains(userAccount.getUserID())) {
|
if (cache.contains(userAccount.getUserId())) {
|
||||||
DBManager.updateDB(userAccount);
|
DbManager.updateDb(userAccount);
|
||||||
cache.invalidate(userAccount.getUserID()); // Cache data has been updated -- remove older
|
cache.invalidate(userAccount.getUserId()); // Cache data has been updated -- remove older
|
||||||
// version from cache.
|
// version from cache.
|
||||||
} else {
|
} else {
|
||||||
DBManager.writeToDB(userAccount);
|
DbManager.writeToDb(userAccount);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public static UserAccount readThroughWithWriteBackPolicy(String userID) {
|
/**
|
||||||
if (cache.contains(userID)) {
|
* Get user account using read-through cache with write-back policy
|
||||||
|
*/
|
||||||
|
public static UserAccount readThroughWithWriteBackPolicy(String userId) {
|
||||||
|
if (cache.contains(userId)) {
|
||||||
System.out.println("# Cache Hit!");
|
System.out.println("# Cache Hit!");
|
||||||
return cache.get(userID);
|
return cache.get(userId);
|
||||||
}
|
}
|
||||||
System.out.println("# Cache Miss!");
|
System.out.println("# Cache Miss!");
|
||||||
UserAccount userAccount = DBManager.readFromDB(userID);
|
UserAccount userAccount = DbManager.readFromDb(userId);
|
||||||
if (cache.isFull()) {
|
if (cache.isFull()) {
|
||||||
System.out.println("# Cache is FULL! Writing LRU data to DB...");
|
System.out.println("# Cache is FULL! Writing LRU data to DB...");
|
||||||
UserAccount toBeWrittenToDB = cache.getLRUData();
|
UserAccount toBeWrittenToDb = cache.getLruData();
|
||||||
DBManager.upsertDB(toBeWrittenToDB);
|
DbManager.upsertDb(toBeWrittenToDb);
|
||||||
}
|
}
|
||||||
cache.set(userID, userAccount);
|
cache.set(userId, userAccount);
|
||||||
return userAccount;
|
return userAccount;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Set user account
|
||||||
|
*/
|
||||||
public static void writeBehind(UserAccount userAccount) {
|
public static void writeBehind(UserAccount userAccount) {
|
||||||
if (cache.isFull() && !cache.contains(userAccount.getUserID())) {
|
if (cache.isFull() && !cache.contains(userAccount.getUserId())) {
|
||||||
System.out.println("# Cache is FULL! Writing LRU data to DB...");
|
System.out.println("# Cache is FULL! Writing LRU data to DB...");
|
||||||
UserAccount toBeWrittenToDB = cache.getLRUData();
|
UserAccount toBeWrittenToDb = cache.getLruData();
|
||||||
DBManager.upsertDB(toBeWrittenToDB);
|
DbManager.upsertDb(toBeWrittenToDb);
|
||||||
}
|
}
|
||||||
cache.set(userAccount.getUserID(), userAccount);
|
cache.set(userAccount.getUserId(), userAccount);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Clears cache
|
||||||
|
*/
|
||||||
public static void clearCache() {
|
public static void clearCache() {
|
||||||
if (null != cache)
|
if (null != cache) {
|
||||||
cache.clear();
|
cache.clear();
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Writes remaining content in the cache into the DB.
|
* Writes remaining content in the cache into the DB.
|
||||||
*/
|
*/
|
||||||
public static void flushCache() {
|
public static void flushCache() {
|
||||||
System.out.println("# flushCache...");
|
System.out.println("# flushCache...");
|
||||||
if (null == cache)
|
if (null == cache) {
|
||||||
return;
|
return;
|
||||||
|
}
|
||||||
ArrayList<UserAccount> listOfUserAccounts = cache.getCacheDataInListForm();
|
ArrayList<UserAccount> listOfUserAccounts = cache.getCacheDataInListForm();
|
||||||
for (UserAccount userAccount : listOfUserAccounts) {
|
for (UserAccount userAccount : listOfUserAccounts) {
|
||||||
DBManager.upsertDB(userAccount);
|
DbManager.upsertDb(userAccount);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Print user accounts
|
||||||
|
*/
|
||||||
public static String print() {
|
public static String print() {
|
||||||
ArrayList<UserAccount> listOfUserAccounts = cache.getCacheDataInListForm();
|
ArrayList<UserAccount> listOfUserAccounts = cache.getCacheDataInListForm();
|
||||||
StringBuilder sb = new StringBuilder();
|
StringBuilder sb = new StringBuilder();
|
||||||
|
@ -1,3 +1,25 @@
|
|||||||
|
/**
|
||||||
|
* The MIT License
|
||||||
|
* Copyright (c) 2014 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.caching;
|
package com.iluwatar.caching;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -1,3 +1,25 @@
|
|||||||
|
/**
|
||||||
|
* The MIT License
|
||||||
|
* Copyright (c) 2014 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.caching;
|
package com.iluwatar.caching;
|
||||||
|
|
||||||
import java.text.ParseException;
|
import java.text.ParseException;
|
||||||
@ -21,7 +43,7 @@ import com.mongodb.client.model.UpdateOptions;
|
|||||||
* during runtime (createVirtualDB()).</p>
|
* during runtime (createVirtualDB()).</p>
|
||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
public class DBManager {
|
public class DbManager {
|
||||||
|
|
||||||
private static MongoClient mongoClient;
|
private static MongoClient mongoClient;
|
||||||
private static MongoDatabase db;
|
private static MongoDatabase db;
|
||||||
@ -29,21 +51,34 @@ public class DBManager {
|
|||||||
|
|
||||||
private static HashMap<String, UserAccount> virtualDB;
|
private static HashMap<String, UserAccount> virtualDB;
|
||||||
|
|
||||||
public static void createVirtualDB() {
|
private DbManager() {
|
||||||
useMongoDB = false;
|
|
||||||
virtualDB = new HashMap<String, UserAccount>();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Create DB
|
||||||
|
*/
|
||||||
|
public static void createVirtualDb() {
|
||||||
|
useMongoDB = false;
|
||||||
|
virtualDB = new HashMap<>();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Connect to DB
|
||||||
|
*/
|
||||||
public static void connect() throws ParseException {
|
public static void connect() throws ParseException {
|
||||||
useMongoDB = true;
|
useMongoDB = true;
|
||||||
mongoClient = new MongoClient();
|
mongoClient = new MongoClient();
|
||||||
db = mongoClient.getDatabase("test");
|
db = mongoClient.getDatabase("test");
|
||||||
}
|
}
|
||||||
|
|
||||||
public static UserAccount readFromDB(String userID) {
|
/**
|
||||||
|
* Read user account from DB
|
||||||
|
*/
|
||||||
|
public static UserAccount readFromDb(String userId) {
|
||||||
if (!useMongoDB) {
|
if (!useMongoDB) {
|
||||||
if (virtualDB.containsKey(userID))
|
if (virtualDB.containsKey(userId)) {
|
||||||
return virtualDB.get(userID);
|
return virtualDB.get(userId);
|
||||||
|
}
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
if (null == db) {
|
if (null == db) {
|
||||||
@ -54,18 +89,20 @@ public class DBManager {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
FindIterable<Document> iterable =
|
FindIterable<Document> iterable =
|
||||||
db.getCollection("user_accounts").find(new Document("userID", userID));
|
db.getCollection("user_accounts").find(new Document("userID", userId));
|
||||||
if (iterable == null)
|
if (iterable == null) {
|
||||||
return null;
|
return null;
|
||||||
|
}
|
||||||
Document doc = iterable.first();
|
Document doc = iterable.first();
|
||||||
UserAccount userAccount =
|
return new UserAccount(userId, doc.getString("userName"), doc.getString("additionalInfo"));
|
||||||
new UserAccount(userID, doc.getString("userName"), doc.getString("additionalInfo"));
|
|
||||||
return userAccount;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public static void writeToDB(UserAccount userAccount) {
|
/**
|
||||||
|
* Write user account to DB
|
||||||
|
*/
|
||||||
|
public static void writeToDb(UserAccount userAccount) {
|
||||||
if (!useMongoDB) {
|
if (!useMongoDB) {
|
||||||
virtualDB.put(userAccount.getUserID(), userAccount);
|
virtualDB.put(userAccount.getUserId(), userAccount);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
if (null == db) {
|
if (null == db) {
|
||||||
@ -76,13 +113,16 @@ public class DBManager {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
db.getCollection("user_accounts").insertOne(
|
db.getCollection("user_accounts").insertOne(
|
||||||
new Document("userID", userAccount.getUserID()).append("userName",
|
new Document("userID", userAccount.getUserId()).append("userName",
|
||||||
userAccount.getUserName()).append("additionalInfo", userAccount.getAdditionalInfo()));
|
userAccount.getUserName()).append("additionalInfo", userAccount.getAdditionalInfo()));
|
||||||
}
|
}
|
||||||
|
|
||||||
public static void updateDB(UserAccount userAccount) {
|
/**
|
||||||
|
* Update DB
|
||||||
|
*/
|
||||||
|
public static void updateDb(UserAccount userAccount) {
|
||||||
if (!useMongoDB) {
|
if (!useMongoDB) {
|
||||||
virtualDB.put(userAccount.getUserID(), userAccount);
|
virtualDB.put(userAccount.getUserId(), userAccount);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
if (null == db) {
|
if (null == db) {
|
||||||
@ -93,7 +133,7 @@ public class DBManager {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
db.getCollection("user_accounts").updateOne(
|
db.getCollection("user_accounts").updateOne(
|
||||||
new Document("userID", userAccount.getUserID()),
|
new Document("userID", userAccount.getUserId()),
|
||||||
new Document("$set", new Document("userName", userAccount.getUserName()).append(
|
new Document("$set", new Document("userName", userAccount.getUserName()).append(
|
||||||
"additionalInfo", userAccount.getAdditionalInfo())));
|
"additionalInfo", userAccount.getAdditionalInfo())));
|
||||||
}
|
}
|
||||||
@ -102,9 +142,9 @@ public class DBManager {
|
|||||||
*
|
*
|
||||||
* Insert data into DB if it does not exist. Else, update it.
|
* Insert data into DB if it does not exist. Else, update it.
|
||||||
*/
|
*/
|
||||||
public static void upsertDB(UserAccount userAccount) {
|
public static void upsertDb(UserAccount userAccount) {
|
||||||
if (!useMongoDB) {
|
if (!useMongoDB) {
|
||||||
virtualDB.put(userAccount.getUserID(), userAccount);
|
virtualDB.put(userAccount.getUserId(), userAccount);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
if (null == db) {
|
if (null == db) {
|
||||||
@ -115,8 +155,8 @@ public class DBManager {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
db.getCollection("user_accounts").updateOne(
|
db.getCollection("user_accounts").updateOne(
|
||||||
new Document("userID", userAccount.getUserID()),
|
new Document("userID", userAccount.getUserId()),
|
||||||
new Document("$set", new Document("userID", userAccount.getUserID()).append("userName",
|
new Document("$set", new Document("userID", userAccount.getUserId()).append("userName",
|
||||||
userAccount.getUserName()).append("additionalInfo", userAccount.getAdditionalInfo())),
|
userAccount.getUserName()).append("additionalInfo", userAccount.getAdditionalInfo())),
|
||||||
new UpdateOptions().upsert(true));
|
new UpdateOptions().upsert(true));
|
||||||
}
|
}
|
@ -1,3 +1,25 @@
|
|||||||
|
/**
|
||||||
|
* The MIT License
|
||||||
|
* Copyright (c) 2014 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.caching;
|
package com.iluwatar.caching;
|
||||||
|
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
@ -12,32 +34,35 @@ import java.util.HashMap;
|
|||||||
* LRU data is always at the end of the list.
|
* LRU data is always at the end of the list.
|
||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
public class LRUCache {
|
public class LruCache {
|
||||||
|
|
||||||
class Node {
|
class Node {
|
||||||
String userID;
|
String userId;
|
||||||
UserAccount userAccount;
|
UserAccount userAccount;
|
||||||
Node previous;
|
Node previous;
|
||||||
Node next;
|
Node next;
|
||||||
|
|
||||||
public Node(String userID, UserAccount userAccount) {
|
public Node(String userId, UserAccount userAccount) {
|
||||||
this.userID = userID;
|
this.userId = userId;
|
||||||
this.userAccount = userAccount;
|
this.userAccount = userAccount;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
int capacity;
|
int capacity;
|
||||||
HashMap<String, Node> cache = new HashMap<String, Node>();
|
HashMap<String, Node> cache = new HashMap<>();
|
||||||
Node head = null;
|
Node head = null;
|
||||||
Node end = null;
|
Node end = null;
|
||||||
|
|
||||||
public LRUCache(int capacity) {
|
public LruCache(int capacity) {
|
||||||
this.capacity = capacity;
|
this.capacity = capacity;
|
||||||
}
|
}
|
||||||
|
|
||||||
public UserAccount get(String userID) {
|
/**
|
||||||
if (cache.containsKey(userID)) {
|
* Get user account
|
||||||
Node node = cache.get(userID);
|
*/
|
||||||
|
public UserAccount get(String userId) {
|
||||||
|
if (cache.containsKey(userId)) {
|
||||||
|
Node node = cache.get(userId);
|
||||||
remove(node);
|
remove(node);
|
||||||
setHead(node);
|
setHead(node);
|
||||||
return node.userAccount;
|
return node.userAccount;
|
||||||
@ -69,52 +94,63 @@ public class LRUCache {
|
|||||||
public void setHead(Node node) {
|
public void setHead(Node node) {
|
||||||
node.next = head;
|
node.next = head;
|
||||||
node.previous = null;
|
node.previous = null;
|
||||||
if (head != null)
|
if (head != null) {
|
||||||
head.previous = node;
|
head.previous = node;
|
||||||
|
}
|
||||||
head = node;
|
head = node;
|
||||||
if (end == null)
|
if (end == null) {
|
||||||
end = head;
|
end = head;
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
public void set(String userID, UserAccount userAccount) {
|
/**
|
||||||
if (cache.containsKey(userID)) {
|
* Set user account
|
||||||
Node old = cache.get(userID);
|
*/
|
||||||
|
public void set(String userId, UserAccount userAccount) {
|
||||||
|
if (cache.containsKey(userId)) {
|
||||||
|
Node old = cache.get(userId);
|
||||||
old.userAccount = userAccount;
|
old.userAccount = userAccount;
|
||||||
remove(old);
|
remove(old);
|
||||||
setHead(old);
|
setHead(old);
|
||||||
} else {
|
} else {
|
||||||
Node newNode = new Node(userID, userAccount);
|
Node newNode = new Node(userId, userAccount);
|
||||||
if (cache.size() >= capacity) {
|
if (cache.size() >= capacity) {
|
||||||
System.out.println("# Cache is FULL! Removing " + end.userID + " from cache...");
|
System.out.println("# Cache is FULL! Removing " + end.userId + " from cache...");
|
||||||
cache.remove(end.userID); // remove LRU data from cache.
|
cache.remove(end.userId); // remove LRU data from cache.
|
||||||
remove(end);
|
remove(end);
|
||||||
setHead(newNode);
|
setHead(newNode);
|
||||||
} else {
|
} else {
|
||||||
setHead(newNode);
|
setHead(newNode);
|
||||||
}
|
}
|
||||||
cache.put(userID, newNode);
|
cache.put(userId, newNode);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public boolean contains(String userID) {
|
public boolean contains(String userId) {
|
||||||
return cache.containsKey(userID);
|
return cache.containsKey(userId);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void invalidate(String userID) {
|
/**
|
||||||
System.out.println("# " + userID + " has been updated! Removing older version from cache...");
|
* Invalidate cache for user
|
||||||
Node toBeRemoved = cache.get(userID);
|
*/
|
||||||
|
public void invalidate(String userId) {
|
||||||
|
System.out.println("# " + userId + " has been updated! Removing older version from cache...");
|
||||||
|
Node toBeRemoved = cache.get(userId);
|
||||||
remove(toBeRemoved);
|
remove(toBeRemoved);
|
||||||
cache.remove(userID);
|
cache.remove(userId);
|
||||||
}
|
}
|
||||||
|
|
||||||
public boolean isFull() {
|
public boolean isFull() {
|
||||||
return cache.size() >= capacity;
|
return cache.size() >= capacity;
|
||||||
}
|
}
|
||||||
|
|
||||||
public UserAccount getLRUData() {
|
public UserAccount getLruData() {
|
||||||
return end.userAccount;
|
return end.userAccount;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Clear cache
|
||||||
|
*/
|
||||||
public void clear() {
|
public void clear() {
|
||||||
head = null;
|
head = null;
|
||||||
end = null;
|
end = null;
|
||||||
@ -126,7 +162,7 @@ public class LRUCache {
|
|||||||
* Returns cache data in list form.
|
* Returns cache data in list form.
|
||||||
*/
|
*/
|
||||||
public ArrayList<UserAccount> getCacheDataInListForm() {
|
public ArrayList<UserAccount> getCacheDataInListForm() {
|
||||||
ArrayList<UserAccount> listOfCacheData = new ArrayList<UserAccount>();
|
ArrayList<UserAccount> listOfCacheData = new ArrayList<>();
|
||||||
Node temp = head;
|
Node temp = head;
|
||||||
while (temp != null) {
|
while (temp != null) {
|
||||||
listOfCacheData.add(temp.userAccount);
|
listOfCacheData.add(temp.userAccount);
|
||||||
@ -135,6 +171,9 @@ public class LRUCache {
|
|||||||
return listOfCacheData;
|
return listOfCacheData;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Set cache capacity
|
||||||
|
*/
|
||||||
public void setCapacity(int newCapacity) {
|
public void setCapacity(int newCapacity) {
|
||||||
if (capacity > newCapacity) {
|
if (capacity > newCapacity) {
|
||||||
clear(); // Behavior can be modified to accommodate for decrease in cache size. For now, we'll
|
clear(); // Behavior can be modified to accommodate for decrease in cache size. For now, we'll
|
@ -1,3 +1,25 @@
|
|||||||
|
/**
|
||||||
|
* The MIT License
|
||||||
|
* Copyright (c) 2014 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.caching;
|
package com.iluwatar.caching;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -6,22 +28,25 @@ package com.iluwatar.caching;
|
|||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
public class UserAccount {
|
public class UserAccount {
|
||||||
private String userID;
|
private String userId;
|
||||||
private String userName;
|
private String userName;
|
||||||
private String additionalInfo;
|
private String additionalInfo;
|
||||||
|
|
||||||
public UserAccount(String userID, String userName, String additionalInfo) {
|
/**
|
||||||
this.userID = userID;
|
* Constructor
|
||||||
|
*/
|
||||||
|
public UserAccount(String userId, String userName, String additionalInfo) {
|
||||||
|
this.userId = userId;
|
||||||
this.userName = userName;
|
this.userName = userName;
|
||||||
this.additionalInfo = additionalInfo;
|
this.additionalInfo = additionalInfo;
|
||||||
}
|
}
|
||||||
|
|
||||||
public String getUserID() {
|
public String getUserId() {
|
||||||
return userID;
|
return userId;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setUserID(String userID) {
|
public void setUserId(String userId) {
|
||||||
this.userID = userID;
|
this.userId = userId;
|
||||||
}
|
}
|
||||||
|
|
||||||
public String getUserName() {
|
public String getUserName() {
|
||||||
@ -42,6 +67,6 @@ public class UserAccount {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public String toString() {
|
public String toString() {
|
||||||
return userID + ", " + userName + ", " + additionalInfo;
|
return userId + ", " + userName + ", " + additionalInfo;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,41 +1,38 @@
|
|||||||
|
/**
|
||||||
|
* The MIT License
|
||||||
|
* Copyright (c) 2014 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.caching;
|
package com.iluwatar.caching;
|
||||||
|
|
||||||
import org.junit.Before;
|
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
|
|
||||||
|
import java.io.IOException;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
*
|
* Tests that Caching example runs without errors.
|
||||||
* Application test
|
|
||||||
*
|
|
||||||
*/
|
*/
|
||||||
public class AppTest {
|
public class AppTest {
|
||||||
App app;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Setup of application test includes: initializing DB connection and cache size/capacity.
|
|
||||||
*/
|
|
||||||
@Before
|
|
||||||
public void setUp() {
|
|
||||||
AppManager.initDB(false); // VirtualDB (instead of MongoDB) was used in running the JUnit tests
|
|
||||||
// to avoid Maven compilation errors. Set flag to true to run the
|
|
||||||
// tests with MongoDB (provided that MongoDB is installed and socket
|
|
||||||
// connection is open).
|
|
||||||
AppManager.initCacheCapacity(3);
|
|
||||||
app = new App();
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testReadAndWriteThroughStrategy() {
|
public void test() throws IOException {
|
||||||
app.useReadAndWriteThroughStrategy();
|
String[] args = {};
|
||||||
}
|
App.main(args);
|
||||||
|
|
||||||
@Test
|
|
||||||
public void testReadThroughAndWriteAroundStrategy() {
|
|
||||||
app.useReadThroughAndWriteAroundStrategy();
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
public void testReadThroughAndWriteBehindStrategy() {
|
|
||||||
app.useReadThroughAndWriteBehindStrategy();
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
63
caching/src/test/java/com/iluwatar/caching/CachingTest.java
Normal file
63
caching/src/test/java/com/iluwatar/caching/CachingTest.java
Normal file
@ -0,0 +1,63 @@
|
|||||||
|
/**
|
||||||
|
* The MIT License
|
||||||
|
* Copyright (c) 2014 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.caching;
|
||||||
|
|
||||||
|
import org.junit.Before;
|
||||||
|
import org.junit.Test;
|
||||||
|
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
* Application test
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
public class CachingTest {
|
||||||
|
App app;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Setup of application test includes: initializing DB connection and cache size/capacity.
|
||||||
|
*/
|
||||||
|
@Before
|
||||||
|
public void setUp() {
|
||||||
|
AppManager.initDb(false); // VirtualDB (instead of MongoDB) was used in running the JUnit tests
|
||||||
|
// to avoid Maven compilation errors. Set flag to true to run the
|
||||||
|
// tests with MongoDB (provided that MongoDB is installed and socket
|
||||||
|
// connection is open).
|
||||||
|
AppManager.initCacheCapacity(3);
|
||||||
|
app = new App();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testReadAndWriteThroughStrategy() {
|
||||||
|
app.useReadAndWriteThroughStrategy();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testReadThroughAndWriteAroundStrategy() {
|
||||||
|
app.useReadThroughAndWriteAroundStrategy();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testReadThroughAndWriteBehindStrategy() {
|
||||||
|
app.useReadThroughAndWriteBehindStrategy();
|
||||||
|
}
|
||||||
|
}
|
@ -4,19 +4,25 @@ title: Callback
|
|||||||
folder: callback
|
folder: callback
|
||||||
permalink: /patterns/callback/
|
permalink: /patterns/callback/
|
||||||
categories: Other
|
categories: Other
|
||||||
tags: Java
|
tags:
|
||||||
|
- Java
|
||||||
|
- Difficulty-Beginner
|
||||||
|
- Functional
|
||||||
|
- Idiom
|
||||||
---
|
---
|
||||||
|
|
||||||
**Intent:** Callback is a piece of executable code that is passed as an
|
## Intent
|
||||||
|
Callback is a piece of executable code that is passed as an
|
||||||
argument to other code, which is expected to call back (execute) the argument
|
argument to other code, which is expected to call back (execute) the argument
|
||||||
at some convenient time.
|
at some convenient time.
|
||||||
|
|
||||||

|

|
||||||
|
|
||||||
**Applicability:** Use the Callback pattern when
|
## Applicability
|
||||||
|
Use the Callback pattern when
|
||||||
|
|
||||||
* when some arbitrary synchronous or asynchronous action must be performed after execution of some defined activity.
|
* when some arbitrary synchronous or asynchronous action must be performed after execution of some defined activity.
|
||||||
|
|
||||||
**Real world examples:**
|
## Real world examples
|
||||||
|
|
||||||
* [CyclicBarrier] (http://docs.oracle.com/javase/7/docs/api/java/util/concurrent/CyclicBarrier.html#CyclicBarrier%28int,%20java.lang.Runnable%29) constructor can accept callback that will be triggered every time when barrier is tripped.
|
* [CyclicBarrier] (http://docs.oracle.com/javase/7/docs/api/java/util/concurrent/CyclicBarrier.html#CyclicBarrier%28int,%20java.lang.Runnable%29) constructor can accept callback that will be triggered every time when barrier is tripped.
|
||||||
|
@ -1,11 +1,35 @@
|
|||||||
<?xml version="1.0"?>
|
<?xml version="1.0"?>
|
||||||
|
<!--
|
||||||
|
|
||||||
|
The MIT License
|
||||||
|
Copyright (c) 2014 Ilkka Seppälä
|
||||||
|
|
||||||
|
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||||
|
of this software and associated documentation files (the "Software"), to deal
|
||||||
|
in the Software without restriction, including without limitation the rights
|
||||||
|
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||||
|
copies of the Software, and to permit persons to whom the Software is
|
||||||
|
furnished to do so, subject to the following conditions:
|
||||||
|
|
||||||
|
The above copyright notice and this permission notice shall be included in
|
||||||
|
all copies or substantial portions of the Software.
|
||||||
|
|
||||||
|
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||||
|
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||||
|
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||||
|
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||||
|
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||||
|
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||||
|
THE SOFTWARE.
|
||||||
|
|
||||||
|
-->
|
||||||
<project xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" xmlns="http://maven.apache.org/POM/4.0.0"
|
<project xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" xmlns="http://maven.apache.org/POM/4.0.0"
|
||||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
|
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
|
||||||
<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.8.0</version>
|
<version>1.10.0</version>
|
||||||
</parent>
|
</parent>
|
||||||
<artifactId>callback</artifactId>
|
<artifactId>callback</artifactId>
|
||||||
<dependencies>
|
<dependencies>
|
||||||
|
@ -1,3 +1,25 @@
|
|||||||
|
/**
|
||||||
|
* The MIT License
|
||||||
|
* Copyright (c) 2014 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.callback;
|
package com.iluwatar.callback;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -9,6 +31,9 @@ package com.iluwatar.callback;
|
|||||||
*/
|
*/
|
||||||
public class App {
|
public class App {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Program entry point
|
||||||
|
*/
|
||||||
public static void main(String[] args) {
|
public static void main(String[] args) {
|
||||||
Task task = new SimpleTask();
|
Task task = new SimpleTask();
|
||||||
Callback callback = new Callback() {
|
Callback callback = new Callback() {
|
||||||
|
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user