Compare commits
91 Commits
Author | SHA1 | Date | |
---|---|---|---|
d793160bce | |||
b65b89baaf | |||
edea7d2220 | |||
bf8bc4df4b | |||
d13635533a | |||
37cd4cbb3b | |||
3d29755842 | |||
5b47d48fc2 | |||
73934e25e5 | |||
41d487d32a | |||
a0916aab6c | |||
863589ed29 | |||
85acb1cf6c | |||
09fb79099f | |||
22a7c15acc | |||
0182b840af | |||
f87249e03b | |||
dd0ca2d16e | |||
f2e35ec03d | |||
ae1d9cf7a8 | |||
1bbc597671 | |||
4d6467e435 | |||
17039dc5e9 | |||
30dcbee2cc | |||
08901f3c26 | |||
c746004f73 | |||
09aa44ddcb | |||
992e76ac61 | |||
167a43f72e | |||
fe1e45bd69 | |||
4b32fb65da | |||
0546223bba | |||
857902ab95 | |||
0b57edd211 | |||
fb26d42b51 | |||
6857486f27 | |||
1abd96a9c8 | |||
08c4202852 | |||
6ecf994258 | |||
0687a3f9f8 | |||
64337dff06 | |||
34b09c75ec | |||
66c6f30c1c | |||
e413b116ac | |||
6e0b3e37ea | |||
be3f4dce50 | |||
6d87f63ed5 | |||
c92a8daeda | |||
20062faae6 | |||
678524704c | |||
2b229d8ea1 | |||
03aa99c55f | |||
20a4c054a7 | |||
3ed6cc19d2 | |||
87a9387e62 | |||
8530d01e10 | |||
a34e7be9c2 | |||
ee3744cb0a | |||
2830a407ba | |||
ccfb6709c7 | |||
a1c5131304 | |||
44401988d1 | |||
6e8eaf7593 | |||
645e91ed23 | |||
9e7e8a64f6 | |||
152b2762c3 | |||
82b9f4fea7 | |||
ff8d854a8d | |||
31f4b15e86 | |||
cd54cf5512 | |||
798aee47b3 | |||
dce767c1c5 | |||
60ebcc56f8 | |||
2643dfa0b8 | |||
c96ebcb197 | |||
b72d545349 | |||
311bb79870 | |||
f495a88e91 | |||
8b0c14cae0 | |||
b7a6a018e0 | |||
46e0fa4825 | |||
86009f2261 | |||
146f367188 | |||
bc4d029a87 | |||
960eee3d43 | |||
30315e788f | |||
073d06c0ae | |||
6b795e52c3 | |||
b5bdf2d7d7 | |||
3fd6887975 | |||
eecffb0ea5 |
@ -29,7 +29,7 @@
|
|||||||
<parent>
|
<parent>
|
||||||
<artifactId>java-design-patterns</artifactId>
|
<artifactId>java-design-patterns</artifactId>
|
||||||
<groupId>com.iluwatar</groupId>
|
<groupId>com.iluwatar</groupId>
|
||||||
<version>1.15.0</version>
|
<version>1.16.0</version>
|
||||||
</parent>
|
</parent>
|
||||||
<artifactId>abstract-document</artifactId>
|
<artifactId>abstract-document</artifactId>
|
||||||
<dependencies>
|
<dependencies>
|
||||||
|
@ -27,6 +27,22 @@ Use the Abstract Factory pattern when
|
|||||||
* 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
|
||||||
|
* the lifetime of the dependency is conceptually shorter than the lifetime of the consumer.
|
||||||
|
* you need a run-time value to construct a particular dependency
|
||||||
|
* you want to decide which product to call from a family at runtime.
|
||||||
|
* you need to supply one or more parameters only known at run-time before you can resolve a dependency.
|
||||||
|
|
||||||
|
## Use Cases:
|
||||||
|
|
||||||
|
* Selecting to call the appropriate implementation of FileSystemAcmeService or DatabaseAcmeService or NetworkAcmeService at runtime.
|
||||||
|
* Unit test case writing becomes much easier
|
||||||
|
|
||||||
|
## Consequences:
|
||||||
|
|
||||||
|
* Dependency injection in java hides the service class dependencies that can lead to runtime errors that would have been caught at compile time.
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
## Real world examples
|
## Real world examples
|
||||||
|
|
||||||
|
@ -29,7 +29,7 @@
|
|||||||
<parent>
|
<parent>
|
||||||
<groupId>com.iluwatar</groupId>
|
<groupId>com.iluwatar</groupId>
|
||||||
<artifactId>java-design-patterns</artifactId>
|
<artifactId>java-design-patterns</artifactId>
|
||||||
<version>1.15.0</version>
|
<version>1.16.0</version>
|
||||||
</parent>
|
</parent>
|
||||||
<artifactId>abstract-factory</artifactId>
|
<artifactId>abstract-factory</artifactId>
|
||||||
<dependencies>
|
<dependencies>
|
||||||
|
@ -21,6 +21,9 @@ incompatible interfaces.
|
|||||||
|
|
||||||

|

|
||||||
|
|
||||||
|
## General usage of Adapter Pattern:
|
||||||
|
+ Wrappers used to adopt 3rd parties libraries and frameworks - most of the applications using third party libraries use adapters as a middle layer between the application and the 3rd party library to decouple the application from the library. If another library has to be used only an adapter for the new library is required without having to change the application code.
|
||||||
|
|
||||||
## Applicability
|
## Applicability
|
||||||
Use the Adapter pattern when
|
Use the Adapter pattern when
|
||||||
|
|
||||||
@ -28,6 +31,19 @@ Use the Adapter pattern when
|
|||||||
* 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.
|
||||||
|
|
||||||
|
## Consequences:
|
||||||
|
Class and object adapters have different trade-offs. A class adapter
|
||||||
|
|
||||||
|
* adapts Adaptee to Target by committing to a concrete Adaptee class. As a consequence, a class adapter won’t work when we want to adapt a class and all its subclasses.
|
||||||
|
* let’s Adapter override some of Adaptee’s behavior, since Adapter is a subclass of Adaptee.
|
||||||
|
* introduces only one object, and no additional pointer indirection is needed to get to the adaptee.
|
||||||
|
|
||||||
|
An object adapter
|
||||||
|
|
||||||
|
* let’s a single Adapter work with many Adaptees—that is, the Adaptee itself and all of its subclasses (if any). The Adapter can also add functionality to all Adaptees at once.
|
||||||
|
* makes it harder to override Adaptee behavior. It will require subclassing Adaptee and making Adapter refer to the subclass rather than the Adaptee itself.
|
||||||
|
|
||||||
|
|
||||||
## 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)
|
||||||
|
@ -29,7 +29,7 @@
|
|||||||
<parent>
|
<parent>
|
||||||
<groupId>com.iluwatar</groupId>
|
<groupId>com.iluwatar</groupId>
|
||||||
<artifactId>java-design-patterns</artifactId>
|
<artifactId>java-design-patterns</artifactId>
|
||||||
<version>1.15.0</version>
|
<version>1.16.0</version>
|
||||||
</parent>
|
</parent>
|
||||||
<artifactId>adapter</artifactId>
|
<artifactId>adapter</artifactId>
|
||||||
<dependencies>
|
<dependencies>
|
||||||
|
@ -29,7 +29,7 @@
|
|||||||
<parent>
|
<parent>
|
||||||
<artifactId>aggregator-microservices</artifactId>
|
<artifactId>aggregator-microservices</artifactId>
|
||||||
<groupId>com.iluwatar</groupId>
|
<groupId>com.iluwatar</groupId>
|
||||||
<version>1.15.0</version>
|
<version>1.16.0</version>
|
||||||
</parent>
|
</parent>
|
||||||
<modelVersion>4.0.0</modelVersion>
|
<modelVersion>4.0.0</modelVersion>
|
||||||
|
|
||||||
|
@ -29,7 +29,7 @@
|
|||||||
<parent>
|
<parent>
|
||||||
<artifactId>aggregator-microservices</artifactId>
|
<artifactId>aggregator-microservices</artifactId>
|
||||||
<groupId>com.iluwatar</groupId>
|
<groupId>com.iluwatar</groupId>
|
||||||
<version>1.15.0</version>
|
<version>1.16.0</version>
|
||||||
</parent>
|
</parent>
|
||||||
<modelVersion>4.0.0</modelVersion>
|
<modelVersion>4.0.0</modelVersion>
|
||||||
|
|
||||||
|
@ -29,7 +29,7 @@
|
|||||||
<parent>
|
<parent>
|
||||||
<artifactId>aggregator-microservices</artifactId>
|
<artifactId>aggregator-microservices</artifactId>
|
||||||
<groupId>com.iluwatar</groupId>
|
<groupId>com.iluwatar</groupId>
|
||||||
<version>1.15.0</version>
|
<version>1.16.0</version>
|
||||||
</parent>
|
</parent>
|
||||||
<modelVersion>4.0.0</modelVersion>
|
<modelVersion>4.0.0</modelVersion>
|
||||||
|
|
||||||
|
@ -29,7 +29,7 @@
|
|||||||
<parent>
|
<parent>
|
||||||
<artifactId>java-design-patterns</artifactId>
|
<artifactId>java-design-patterns</artifactId>
|
||||||
<groupId>com.iluwatar</groupId>
|
<groupId>com.iluwatar</groupId>
|
||||||
<version>1.15.0</version>
|
<version>1.16.0</version>
|
||||||
</parent>
|
</parent>
|
||||||
<modelVersion>4.0.0</modelVersion>
|
<modelVersion>4.0.0</modelVersion>
|
||||||
<artifactId>aggregator-microservices</artifactId>
|
<artifactId>aggregator-microservices</artifactId>
|
||||||
|
@ -29,7 +29,7 @@
|
|||||||
<parent>
|
<parent>
|
||||||
<artifactId>api-gateway</artifactId>
|
<artifactId>api-gateway</artifactId>
|
||||||
<groupId>com.iluwatar</groupId>
|
<groupId>com.iluwatar</groupId>
|
||||||
<version>1.15.0</version>
|
<version>1.16.0</version>
|
||||||
</parent>
|
</parent>
|
||||||
<modelVersion>4.0.0</modelVersion>
|
<modelVersion>4.0.0</modelVersion>
|
||||||
<artifactId>api-gateway-service</artifactId>
|
<artifactId>api-gateway-service</artifactId>
|
||||||
|
@ -29,7 +29,7 @@
|
|||||||
<parent>
|
<parent>
|
||||||
<artifactId>api-gateway</artifactId>
|
<artifactId>api-gateway</artifactId>
|
||||||
<groupId>com.iluwatar</groupId>
|
<groupId>com.iluwatar</groupId>
|
||||||
<version>1.15.0</version>
|
<version>1.16.0</version>
|
||||||
</parent>
|
</parent>
|
||||||
|
|
||||||
<modelVersion>4.0.0</modelVersion>
|
<modelVersion>4.0.0</modelVersion>
|
||||||
|
@ -29,7 +29,7 @@
|
|||||||
<parent>
|
<parent>
|
||||||
<artifactId>java-design-patterns</artifactId>
|
<artifactId>java-design-patterns</artifactId>
|
||||||
<groupId>com.iluwatar</groupId>
|
<groupId>com.iluwatar</groupId>
|
||||||
<version>1.15.0</version>
|
<version>1.16.0</version>
|
||||||
</parent>
|
</parent>
|
||||||
<modelVersion>4.0.0</modelVersion>
|
<modelVersion>4.0.0</modelVersion>
|
||||||
<artifactId>api-gateway</artifactId>
|
<artifactId>api-gateway</artifactId>
|
||||||
|
@ -29,7 +29,7 @@
|
|||||||
<parent>
|
<parent>
|
||||||
<artifactId>api-gateway</artifactId>
|
<artifactId>api-gateway</artifactId>
|
||||||
<groupId>com.iluwatar</groupId>
|
<groupId>com.iluwatar</groupId>
|
||||||
<version>1.15.0</version>
|
<version>1.16.0</version>
|
||||||
</parent>
|
</parent>
|
||||||
|
|
||||||
<modelVersion>4.0.0</modelVersion>
|
<modelVersion>4.0.0</modelVersion>
|
||||||
|
@ -29,7 +29,7 @@
|
|||||||
<parent>
|
<parent>
|
||||||
<groupId>com.iluwatar</groupId>
|
<groupId>com.iluwatar</groupId>
|
||||||
<artifactId>java-design-patterns</artifactId>
|
<artifactId>java-design-patterns</artifactId>
|
||||||
<version>1.15.0</version>
|
<version>1.16.0</version>
|
||||||
</parent>
|
</parent>
|
||||||
<artifactId>async-method-invocation</artifactId>
|
<artifactId>async-method-invocation</artifactId>
|
||||||
<dependencies>
|
<dependencies>
|
||||||
|
@ -29,7 +29,7 @@
|
|||||||
<parent>
|
<parent>
|
||||||
<artifactId>java-design-patterns</artifactId>
|
<artifactId>java-design-patterns</artifactId>
|
||||||
<groupId>com.iluwatar</groupId>
|
<groupId>com.iluwatar</groupId>
|
||||||
<version>1.15.0</version>
|
<version>1.16.0</version>
|
||||||
</parent>
|
</parent>
|
||||||
<modelVersion>4.0.0</modelVersion>
|
<modelVersion>4.0.0</modelVersion>
|
||||||
|
|
||||||
|
@ -29,7 +29,7 @@
|
|||||||
<parent>
|
<parent>
|
||||||
<groupId>com.iluwatar</groupId>
|
<groupId>com.iluwatar</groupId>
|
||||||
<artifactId>java-design-patterns</artifactId>
|
<artifactId>java-design-patterns</artifactId>
|
||||||
<version>1.15.0</version>
|
<version>1.16.0</version>
|
||||||
</parent>
|
</parent>
|
||||||
<artifactId>bridge</artifactId>
|
<artifactId>bridge</artifactId>
|
||||||
<dependencies>
|
<dependencies>
|
||||||
|
@ -29,7 +29,7 @@
|
|||||||
<parent>
|
<parent>
|
||||||
<groupId>com.iluwatar</groupId>
|
<groupId>com.iluwatar</groupId>
|
||||||
<artifactId>java-design-patterns</artifactId>
|
<artifactId>java-design-patterns</artifactId>
|
||||||
<version>1.15.0</version>
|
<version>1.16.0</version>
|
||||||
</parent>
|
</parent>
|
||||||
<artifactId>builder</artifactId>
|
<artifactId>builder</artifactId>
|
||||||
<dependencies>
|
<dependencies>
|
||||||
|
@ -30,7 +30,7 @@
|
|||||||
<parent>
|
<parent>
|
||||||
<groupId>com.iluwatar</groupId>
|
<groupId>com.iluwatar</groupId>
|
||||||
<artifactId>java-design-patterns</artifactId>
|
<artifactId>java-design-patterns</artifactId>
|
||||||
<version>1.15.0</version>
|
<version>1.16.0</version>
|
||||||
</parent>
|
</parent>
|
||||||
<artifactId>business-delegate</artifactId>
|
<artifactId>business-delegate</artifactId>
|
||||||
<dependencies>
|
<dependencies>
|
||||||
|
@ -29,7 +29,7 @@
|
|||||||
<parent>
|
<parent>
|
||||||
<groupId>com.iluwatar</groupId>
|
<groupId>com.iluwatar</groupId>
|
||||||
<artifactId>java-design-patterns</artifactId>
|
<artifactId>java-design-patterns</artifactId>
|
||||||
<version>1.15.0</version>
|
<version>1.16.0</version>
|
||||||
</parent>
|
</parent>
|
||||||
<artifactId>caching</artifactId>
|
<artifactId>caching</artifactId>
|
||||||
<dependencies>
|
<dependencies>
|
||||||
|
@ -29,7 +29,7 @@
|
|||||||
<parent>
|
<parent>
|
||||||
<groupId>com.iluwatar</groupId>
|
<groupId>com.iluwatar</groupId>
|
||||||
<artifactId>java-design-patterns</artifactId>
|
<artifactId>java-design-patterns</artifactId>
|
||||||
<version>1.15.0</version>
|
<version>1.16.0</version>
|
||||||
</parent>
|
</parent>
|
||||||
<artifactId>callback</artifactId>
|
<artifactId>callback</artifactId>
|
||||||
<dependencies>
|
<dependencies>
|
||||||
|
@ -29,7 +29,7 @@
|
|||||||
<parent>
|
<parent>
|
||||||
<groupId>com.iluwatar</groupId>
|
<groupId>com.iluwatar</groupId>
|
||||||
<artifactId>java-design-patterns</artifactId>
|
<artifactId>java-design-patterns</artifactId>
|
||||||
<version>1.15.0</version>
|
<version>1.16.0</version>
|
||||||
</parent>
|
</parent>
|
||||||
<artifactId>chain</artifactId>
|
<artifactId>chain</artifactId>
|
||||||
<dependencies>
|
<dependencies>
|
||||||
|
@ -29,7 +29,7 @@
|
|||||||
<parent>
|
<parent>
|
||||||
<groupId>com.iluwatar</groupId>
|
<groupId>com.iluwatar</groupId>
|
||||||
<artifactId>java-design-patterns</artifactId>
|
<artifactId>java-design-patterns</artifactId>
|
||||||
<version>1.15.0</version>
|
<version>1.16.0</version>
|
||||||
</parent>
|
</parent>
|
||||||
<artifactId>command</artifactId>
|
<artifactId>command</artifactId>
|
||||||
<dependencies>
|
<dependencies>
|
||||||
|
@ -29,7 +29,7 @@
|
|||||||
<parent>
|
<parent>
|
||||||
<groupId>com.iluwatar</groupId>
|
<groupId>com.iluwatar</groupId>
|
||||||
<artifactId>java-design-patterns</artifactId>
|
<artifactId>java-design-patterns</artifactId>
|
||||||
<version>1.15.0</version>
|
<version>1.16.0</version>
|
||||||
</parent>
|
</parent>
|
||||||
<artifactId>composite</artifactId>
|
<artifactId>composite</artifactId>
|
||||||
<dependencies>
|
<dependencies>
|
||||||
|
@ -29,7 +29,7 @@
|
|||||||
<parent>
|
<parent>
|
||||||
<artifactId>java-design-patterns</artifactId>
|
<artifactId>java-design-patterns</artifactId>
|
||||||
<groupId>com.iluwatar</groupId>
|
<groupId>com.iluwatar</groupId>
|
||||||
<version>1.15.0</version>
|
<version>1.16.0</version>
|
||||||
</parent>
|
</parent>
|
||||||
<modelVersion>4.0.0</modelVersion>
|
<modelVersion>4.0.0</modelVersion>
|
||||||
<dependencies>
|
<dependencies>
|
||||||
|
@ -30,7 +30,7 @@
|
|||||||
<parent>
|
<parent>
|
||||||
<groupId>com.iluwatar</groupId>
|
<groupId>com.iluwatar</groupId>
|
||||||
<artifactId>java-design-patterns</artifactId>
|
<artifactId>java-design-patterns</artifactId>
|
||||||
<version>1.15.0</version>
|
<version>1.16.0</version>
|
||||||
</parent>
|
</parent>
|
||||||
<artifactId>dao</artifactId>
|
<artifactId>dao</artifactId>
|
||||||
|
|
||||||
|
1
data-bus/.gitignore
vendored
Normal file
1
data-bus/.gitignore
vendored
Normal file
@ -0,0 +1 @@
|
|||||||
|
/target
|
33
data-bus/README.md
Normal file
33
data-bus/README.md
Normal file
@ -0,0 +1,33 @@
|
|||||||
|
---
|
||||||
|
layout: pattern
|
||||||
|
title: Data Bus
|
||||||
|
folder: data-bus
|
||||||
|
permalink: /patterns/data-bus/
|
||||||
|
|
||||||
|
categories: Architectural
|
||||||
|
tags:
|
||||||
|
- Java
|
||||||
|
- Difficulty-Intermediate
|
||||||
|
---
|
||||||
|
|
||||||
|
## Intent
|
||||||
|
|
||||||
|
Allows send of messages/events between components of an application
|
||||||
|
without them needing to know about each other. They only need to know
|
||||||
|
about the type of the message/event being sent.
|
||||||
|
|
||||||
|

|
||||||
|
|
||||||
|
## Applicability
|
||||||
|
Use Data Bus pattern when
|
||||||
|
|
||||||
|
* you want your components to decide themselves which messages/events they want to receive
|
||||||
|
* you want to have many-to-many communication
|
||||||
|
* you want your components to know nothing about each other
|
||||||
|
|
||||||
|
## Related Patterns
|
||||||
|
Data Bus is similar to
|
||||||
|
|
||||||
|
* Mediator pattern with Data Bus Members deciding for themselves if they want to accept any given message
|
||||||
|
* Observer pattern but supporting many-to-many communication
|
||||||
|
* Publish/Subscribe pattern with the Data Bus decoupling the publisher and the subscriber
|
BIN
data-bus/etc/data-bus.urm.png
Normal file
BIN
data-bus/etc/data-bus.urm.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 60 KiB |
77
data-bus/etc/data-bus.urm.puml
Normal file
77
data-bus/etc/data-bus.urm.puml
Normal file
@ -0,0 +1,77 @@
|
|||||||
|
@startuml
|
||||||
|
package com.iluwatar.databus {
|
||||||
|
class AbstractDataType {
|
||||||
|
- dataBus : DataBus
|
||||||
|
+ AbstractDataType()
|
||||||
|
+ getDataBus() : DataBus
|
||||||
|
+ setDataBus(dataBus : DataBus)
|
||||||
|
}
|
||||||
|
~class App {
|
||||||
|
- log : Logger {static}
|
||||||
|
~ App()
|
||||||
|
+ main(args : String[]) {static}
|
||||||
|
}
|
||||||
|
class DataBus {
|
||||||
|
- INSTANCE : DataBus {static}
|
||||||
|
- listeners : Set<Member>
|
||||||
|
+ DataBus()
|
||||||
|
+ getInstance() : DataBus {static}
|
||||||
|
+ publish(event : DataType)
|
||||||
|
+ subscribe(member : Member)
|
||||||
|
+ unsubscribe(member : Member)
|
||||||
|
}
|
||||||
|
interface DataType {
|
||||||
|
+ getDataBus() : DataBus {abstract}
|
||||||
|
+ setDataBus(DataBus) {abstract}
|
||||||
|
}
|
||||||
|
interface Member {
|
||||||
|
+ accept(DataType) {abstract}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
package com.iluwatar.databus.data {
|
||||||
|
class MessageData {
|
||||||
|
- message : String
|
||||||
|
+ MessageData(message : String)
|
||||||
|
+ getMessage() : String
|
||||||
|
+ of(message : String) : DataType {static}
|
||||||
|
}
|
||||||
|
class StartingData {
|
||||||
|
- when : LocalDateTime
|
||||||
|
+ StartingData(when : LocalDateTime)
|
||||||
|
+ getWhen() : LocalDateTime
|
||||||
|
+ of(when : LocalDateTime) : DataType {static}
|
||||||
|
}
|
||||||
|
class StoppingData {
|
||||||
|
- when : LocalDateTime
|
||||||
|
+ StoppingData(when : LocalDateTime)
|
||||||
|
+ getWhen() : LocalDateTime
|
||||||
|
+ of(when : LocalDateTime) : DataType {static}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
package com.iluwatar.databus.members {
|
||||||
|
class CounterMember {
|
||||||
|
- log : Logger {static}
|
||||||
|
- name : String
|
||||||
|
+ CounterMember(name : String)
|
||||||
|
+ accept(data : DataType)
|
||||||
|
- handleEvent(data : MessageData)
|
||||||
|
}
|
||||||
|
class StatusMember {
|
||||||
|
- id : int
|
||||||
|
- log : Logger {static}
|
||||||
|
+ StatusMember(id : int)
|
||||||
|
+ accept(data : DataType)
|
||||||
|
- handleEvent(data : StartingData)
|
||||||
|
- handleEvent(data : StoppingData)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
AbstractDataType --> "-dataBus" DataBus
|
||||||
|
DataBus --> "-INSTANCE" DataBus
|
||||||
|
DataBus --> "-listeners" Member
|
||||||
|
AbstractDataType ..|> DataType
|
||||||
|
MessageData --|> AbstractDataType
|
||||||
|
StartingData --|> AbstractDataType
|
||||||
|
StoppingData --|> AbstractDataType
|
||||||
|
CounterMember ..|> Member
|
||||||
|
StatusMember ..|> Member
|
||||||
|
@enduml
|
51
data-bus/pom.xml
Normal file
51
data-bus/pom.xml
Normal file
@ -0,0 +1,51 @@
|
|||||||
|
<?xml version="1.0"?>
|
||||||
|
<!--
|
||||||
|
|
||||||
|
The MIT License
|
||||||
|
Copyright (c) 2014-2016 Ilkka Seppälä
|
||||||
|
|
||||||
|
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||||
|
of this software and associated documentation files (the "Software"), to deal
|
||||||
|
in the Software without restriction, including without limitation the rights
|
||||||
|
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||||
|
copies of the Software, and to permit persons to whom the Software is
|
||||||
|
furnished to do so, subject to the following conditions:
|
||||||
|
|
||||||
|
The above copyright notice and this permission notice shall be included in
|
||||||
|
all copies or substantial portions of the Software.
|
||||||
|
|
||||||
|
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||||
|
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||||
|
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||||
|
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||||
|
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||||
|
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||||
|
THE SOFTWARE.
|
||||||
|
|
||||||
|
-->
|
||||||
|
<project xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"
|
||||||
|
xmlns="http://maven.apache.org/POM/4.0.0"
|
||||||
|
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
|
||||||
|
<modelVersion>4.0.0</modelVersion>
|
||||||
|
<properties>
|
||||||
|
<lombok.version>1.16.14</lombok.version>
|
||||||
|
</properties>
|
||||||
|
<parent>
|
||||||
|
<groupId>com.iluwatar</groupId>
|
||||||
|
<artifactId>java-design-patterns</artifactId>
|
||||||
|
<version>1.16.0</version>
|
||||||
|
</parent>
|
||||||
|
<artifactId>data-bus</artifactId>
|
||||||
|
<dependencies>
|
||||||
|
<dependency>
|
||||||
|
<groupId>junit</groupId>
|
||||||
|
<artifactId>junit</artifactId>
|
||||||
|
<scope>test</scope>
|
||||||
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.mockito</groupId>
|
||||||
|
<artifactId>mockito-core</artifactId>
|
||||||
|
<scope>test</scope>
|
||||||
|
</dependency>
|
||||||
|
</dependencies>
|
||||||
|
</project>
|
@ -0,0 +1,67 @@
|
|||||||
|
/**
|
||||||
|
* 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.
|
||||||
|
*/
|
||||||
|
/*
|
||||||
|
The MIT License (MIT)
|
||||||
|
|
||||||
|
Copyright (c) 2016 Paul Campbell
|
||||||
|
|
||||||
|
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.databus;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Base for data to send via the Data-Bus.
|
||||||
|
*
|
||||||
|
* @author Paul Campbell (pcampbell@kemitix.net)
|
||||||
|
*/
|
||||||
|
public class AbstractDataType implements DataType {
|
||||||
|
|
||||||
|
private DataBus dataBus;
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public DataBus getDataBus() {
|
||||||
|
return dataBus;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void setDataBus(DataBus dataBus) {
|
||||||
|
this.dataBus = dataBus;
|
||||||
|
}
|
||||||
|
}
|
79
data-bus/src/main/java/com/iluwatar/databus/App.java
Normal file
79
data-bus/src/main/java/com/iluwatar/databus/App.java
Normal file
@ -0,0 +1,79 @@
|
|||||||
|
/**
|
||||||
|
* The MIT License
|
||||||
|
* Copyright (c) 2014-2016 Ilkka Seppälä
|
||||||
|
* <p>
|
||||||
|
* 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:
|
||||||
|
* <p>
|
||||||
|
* The above copyright notice and this permission notice shall be included in
|
||||||
|
* all copies or substantial portions of the Software.
|
||||||
|
* <p>
|
||||||
|
* 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.databus;
|
||||||
|
|
||||||
|
import com.iluwatar.databus.data.MessageData;
|
||||||
|
import com.iluwatar.databus.data.StartingData;
|
||||||
|
import com.iluwatar.databus.data.StoppingData;
|
||||||
|
import com.iluwatar.databus.members.MessageCollectorMember;
|
||||||
|
import com.iluwatar.databus.members.StatusMember;
|
||||||
|
|
||||||
|
import java.time.LocalDateTime;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The Data Bus pattern
|
||||||
|
* <p>
|
||||||
|
* <p>{@see http://wiki.c2.com/?DataBusPattern}</p>
|
||||||
|
* <p>
|
||||||
|
* <p>The Data-Bus pattern provides a method where different parts of an application may
|
||||||
|
* pass messages between each other without needing to be aware of the other's existence.</p>
|
||||||
|
* <p>Similar to the {@code ObserverPattern}, members register themselves with the {@link DataBus}
|
||||||
|
* and may then receive each piece of data that is published to the Data-Bus. The member
|
||||||
|
* may react to any given message or not.</p>
|
||||||
|
* <p>It allows for Many-to-Many distribution of data, as there may be any number of
|
||||||
|
* publishers to a Data-Bus, and any number of members receiving the data. All members
|
||||||
|
* will receive the same data, the order each receives a given piece of data, is an
|
||||||
|
* implementation detail.</p>
|
||||||
|
* <p>Members may unsubscribe from the Data-Bus to stop receiving data.</p>
|
||||||
|
* <p>This example of the pattern implements a Synchronous Data-Bus, meaning that
|
||||||
|
* when data is published to the Data-Bus, the publish method will not return until
|
||||||
|
* all members have received the data and returned.</p>
|
||||||
|
* <p>The {@link DataBus} class is a Singleton.</p>
|
||||||
|
* <p>Members of the Data-Bus must implement the {@link Member} interface.</p>
|
||||||
|
* <p>Data to be published via the Data-Bus must implement the {@link DataType} interface.</p>
|
||||||
|
* <p>The {@code data} package contains example {@link DataType} implementations.</p>
|
||||||
|
* <p>The {@code members} package contains example {@link Member} implementations.</p>
|
||||||
|
* <p>The {@link StatusMember} demonstrates using the DataBus to publish a message
|
||||||
|
* to the Data-Bus when it receives a message.</p>
|
||||||
|
*
|
||||||
|
* @author Paul Campbell (pcampbell@kemitix.net)
|
||||||
|
*/
|
||||||
|
class App {
|
||||||
|
|
||||||
|
public static void main(String[] args) {
|
||||||
|
final DataBus bus = DataBus.getInstance();
|
||||||
|
bus.subscribe(new StatusMember(1));
|
||||||
|
bus.subscribe(new StatusMember(2));
|
||||||
|
final MessageCollectorMember foo = new MessageCollectorMember("Foo");
|
||||||
|
final MessageCollectorMember bar = new MessageCollectorMember("Bar");
|
||||||
|
bus.subscribe(foo);
|
||||||
|
bus.publish(StartingData.of(LocalDateTime.now()));
|
||||||
|
bus.publish(MessageData.of("Only Foo should see this"));
|
||||||
|
bus.subscribe(bar);
|
||||||
|
bus.publish(MessageData.of("Foo and Bar should see this"));
|
||||||
|
bus.unsubscribe(foo);
|
||||||
|
bus.publish(MessageData.of("Only Bar should see this"));
|
||||||
|
bus.publish(StoppingData.of(LocalDateTime.now()));
|
||||||
|
}
|
||||||
|
}
|
73
data-bus/src/main/java/com/iluwatar/databus/DataBus.java
Normal file
73
data-bus/src/main/java/com/iluwatar/databus/DataBus.java
Normal file
@ -0,0 +1,73 @@
|
|||||||
|
/**
|
||||||
|
* The MIT License
|
||||||
|
* Copyright (c) 2014-2016 Ilkka Seppälä
|
||||||
|
* <p>
|
||||||
|
* 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:
|
||||||
|
* <p>
|
||||||
|
* The above copyright notice and this permission notice shall be included in
|
||||||
|
* all copies or substantial portions of the Software.
|
||||||
|
* <p>
|
||||||
|
* 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.databus;
|
||||||
|
|
||||||
|
import java.util.HashSet;
|
||||||
|
import java.util.Set;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The Data-Bus implementation.
|
||||||
|
*
|
||||||
|
* <p>This implementation uses a Singleton.</p>
|
||||||
|
*
|
||||||
|
* @author Paul Campbell (pcampbell@kemitix.net)
|
||||||
|
*/
|
||||||
|
public class DataBus {
|
||||||
|
|
||||||
|
private static final DataBus INSTANCE = new DataBus();
|
||||||
|
|
||||||
|
private final Set<Member> listeners = new HashSet<>();
|
||||||
|
|
||||||
|
public static DataBus getInstance() {
|
||||||
|
return INSTANCE;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Register a member with the data-bus to start receiving events.
|
||||||
|
*
|
||||||
|
* @param member The member to register
|
||||||
|
*/
|
||||||
|
public void subscribe(final Member member) {
|
||||||
|
this.listeners.add(member);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Deregister a member to stop receiving events.
|
||||||
|
*
|
||||||
|
* @param member The member to deregister
|
||||||
|
*/
|
||||||
|
public void unsubscribe(final Member member) {
|
||||||
|
this.listeners.remove(member);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Publish and event to all members.
|
||||||
|
*
|
||||||
|
* @param event The event
|
||||||
|
*/
|
||||||
|
public void publish(final DataType event) {
|
||||||
|
event.setDataBus(this);
|
||||||
|
listeners.forEach(listener -> listener.accept(event));
|
||||||
|
}
|
||||||
|
}
|
70
data-bus/src/main/java/com/iluwatar/databus/DataType.java
Normal file
70
data-bus/src/main/java/com/iluwatar/databus/DataType.java
Normal file
@ -0,0 +1,70 @@
|
|||||||
|
/**
|
||||||
|
* 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.
|
||||||
|
*/
|
||||||
|
/*
|
||||||
|
The MIT License (MIT)
|
||||||
|
|
||||||
|
Copyright (c) 2016 Paul Campbell
|
||||||
|
|
||||||
|
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.databus;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Events are sent via the Data-Bus.
|
||||||
|
*
|
||||||
|
* @author Paul Campbell (pcampbell@kemitix.net)
|
||||||
|
*/
|
||||||
|
|
||||||
|
public interface DataType {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns the data-bus the event is being sent on.
|
||||||
|
*
|
||||||
|
* @return The data-bus
|
||||||
|
*/
|
||||||
|
DataBus getDataBus();
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Set the data-bus the event will be sent on.
|
||||||
|
*
|
||||||
|
* @param dataBus The data-bus
|
||||||
|
*/
|
||||||
|
void setDataBus(DataBus dataBus);
|
||||||
|
}
|
59
data-bus/src/main/java/com/iluwatar/databus/Member.java
Normal file
59
data-bus/src/main/java/com/iluwatar/databus/Member.java
Normal file
@ -0,0 +1,59 @@
|
|||||||
|
/**
|
||||||
|
* 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.
|
||||||
|
*/
|
||||||
|
/*
|
||||||
|
The MIT License (MIT)
|
||||||
|
|
||||||
|
Copyright (c) 2016 Paul Campbell
|
||||||
|
|
||||||
|
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.databus;
|
||||||
|
|
||||||
|
import java.util.function.Consumer;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Members receive events from the Data-Bus.
|
||||||
|
*
|
||||||
|
* @author Paul Campbell (pcampbell@kemitix.net)
|
||||||
|
*/
|
||||||
|
public interface Member extends Consumer<DataType> {
|
||||||
|
|
||||||
|
void accept(DataType event);
|
||||||
|
}
|
@ -0,0 +1,49 @@
|
|||||||
|
/**
|
||||||
|
* The MIT License
|
||||||
|
* Copyright (c) 2014-2016 Ilkka Seppälä
|
||||||
|
* <p>
|
||||||
|
* 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:
|
||||||
|
* <p>
|
||||||
|
* The above copyright notice and this permission notice shall be included in
|
||||||
|
* all copies or substantial portions of the Software.
|
||||||
|
* <p>
|
||||||
|
* 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.databus.data;
|
||||||
|
|
||||||
|
import com.iluwatar.databus.AbstractDataType;
|
||||||
|
import com.iluwatar.databus.DataType;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* An event raised when a string message is sent.
|
||||||
|
*
|
||||||
|
* @author Paul Campbell (pcampbell@kemitix.net)
|
||||||
|
*/
|
||||||
|
public class MessageData extends AbstractDataType {
|
||||||
|
|
||||||
|
private final String message;
|
||||||
|
|
||||||
|
public MessageData(String message) {
|
||||||
|
this.message = message;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getMessage() {
|
||||||
|
return message;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static DataType of(final String message) {
|
||||||
|
return new MessageData(message);
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,51 @@
|
|||||||
|
/**
|
||||||
|
* The MIT License
|
||||||
|
* Copyright (c) 2014-2016 Ilkka Seppälä
|
||||||
|
* <p>
|
||||||
|
* 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:
|
||||||
|
* <p>
|
||||||
|
* The above copyright notice and this permission notice shall be included in
|
||||||
|
* all copies or substantial portions of the Software.
|
||||||
|
* <p>
|
||||||
|
* 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.databus.data;
|
||||||
|
|
||||||
|
import com.iluwatar.databus.AbstractDataType;
|
||||||
|
import com.iluwatar.databus.DataType;
|
||||||
|
|
||||||
|
import java.time.LocalDateTime;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* An event raised when applications starts, containing the start time of the application.
|
||||||
|
*
|
||||||
|
* @author Paul Campbell (pcampbell@kemitix.net)
|
||||||
|
*/
|
||||||
|
public class StartingData extends AbstractDataType {
|
||||||
|
|
||||||
|
private final LocalDateTime when;
|
||||||
|
|
||||||
|
public StartingData(LocalDateTime when) {
|
||||||
|
this.when = when;
|
||||||
|
}
|
||||||
|
|
||||||
|
public LocalDateTime getWhen() {
|
||||||
|
return when;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static DataType of(final LocalDateTime when) {
|
||||||
|
return new StartingData(when);
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,51 @@
|
|||||||
|
/**
|
||||||
|
* The MIT License
|
||||||
|
* Copyright (c) 2014-2016 Ilkka Seppälä
|
||||||
|
* <p>
|
||||||
|
* 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:
|
||||||
|
* <p>
|
||||||
|
* The above copyright notice and this permission notice shall be included in
|
||||||
|
* all copies or substantial portions of the Software.
|
||||||
|
* <p>
|
||||||
|
* 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.databus.data;
|
||||||
|
|
||||||
|
import com.iluwatar.databus.AbstractDataType;
|
||||||
|
import com.iluwatar.databus.DataType;
|
||||||
|
|
||||||
|
import java.time.LocalDateTime;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* An event raised when applications stops, containing the stop time of the application.
|
||||||
|
*
|
||||||
|
* @author Paul Campbell (pcampbell@kemitix.net)
|
||||||
|
*/
|
||||||
|
public class StoppingData extends AbstractDataType {
|
||||||
|
|
||||||
|
private final LocalDateTime when;
|
||||||
|
|
||||||
|
public StoppingData(LocalDateTime when) {
|
||||||
|
this.when = when;
|
||||||
|
}
|
||||||
|
|
||||||
|
public LocalDateTime getWhen() {
|
||||||
|
return when;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static DataType of(final LocalDateTime when) {
|
||||||
|
return new StoppingData(when);
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,67 @@
|
|||||||
|
/**
|
||||||
|
* The MIT License
|
||||||
|
* Copyright (c) 2014-2016 Ilkka Seppälä
|
||||||
|
* <p>
|
||||||
|
* 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:
|
||||||
|
* <p>
|
||||||
|
* The above copyright notice and this permission notice shall be included in
|
||||||
|
* all copies or substantial portions of the Software.
|
||||||
|
* <p>
|
||||||
|
* 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.databus.members;
|
||||||
|
|
||||||
|
import com.iluwatar.databus.DataType;
|
||||||
|
import com.iluwatar.databus.Member;
|
||||||
|
import com.iluwatar.databus.data.MessageData;
|
||||||
|
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.Collections;
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.logging.Logger;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Receiver of Data-Bus events that collects the messages from each {@link MessageData}.
|
||||||
|
*
|
||||||
|
* @author Paul Campbell (pcampbell@kemitix.net)
|
||||||
|
*/
|
||||||
|
public class MessageCollectorMember implements Member {
|
||||||
|
|
||||||
|
private static final Logger LOGGER = Logger.getLogger(MessageCollectorMember.class.getName());
|
||||||
|
|
||||||
|
private final String name;
|
||||||
|
|
||||||
|
private List<String> messages = new ArrayList<>();
|
||||||
|
|
||||||
|
public MessageCollectorMember(String name) {
|
||||||
|
this.name = name;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void accept(final DataType data) {
|
||||||
|
if (data instanceof MessageData) {
|
||||||
|
handleEvent((MessageData) data);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private void handleEvent(MessageData data) {
|
||||||
|
LOGGER.info(String.format("%s sees message %s", name, data.getMessage()));
|
||||||
|
messages.add(data.getMessage());
|
||||||
|
}
|
||||||
|
|
||||||
|
public List<String> getMessages() {
|
||||||
|
return Collections.unmodifiableList(messages);
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,82 @@
|
|||||||
|
/**
|
||||||
|
* The MIT License
|
||||||
|
* Copyright (c) 2014-2016 Ilkka Seppälä
|
||||||
|
* <p>
|
||||||
|
* 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:
|
||||||
|
* <p>
|
||||||
|
* The above copyright notice and this permission notice shall be included in
|
||||||
|
* all copies or substantial portions of the Software.
|
||||||
|
* <p>
|
||||||
|
* 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.databus.members;
|
||||||
|
|
||||||
|
import com.iluwatar.databus.DataType;
|
||||||
|
import com.iluwatar.databus.Member;
|
||||||
|
import com.iluwatar.databus.data.MessageData;
|
||||||
|
import com.iluwatar.databus.data.StartingData;
|
||||||
|
import com.iluwatar.databus.data.StoppingData;
|
||||||
|
|
||||||
|
import java.time.LocalDateTime;
|
||||||
|
import java.util.logging.Logger;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Receiver of Data-Bus events.
|
||||||
|
*
|
||||||
|
* @author Paul Campbell (pcampbell@kemitix.net)
|
||||||
|
*/
|
||||||
|
public class StatusMember implements Member {
|
||||||
|
|
||||||
|
private static final Logger LOGGER = Logger.getLogger(StatusMember.class.getName());
|
||||||
|
|
||||||
|
private final int id;
|
||||||
|
|
||||||
|
private LocalDateTime started;
|
||||||
|
|
||||||
|
private LocalDateTime stopped;
|
||||||
|
|
||||||
|
public StatusMember(int id) {
|
||||||
|
this.id = id;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void accept(final DataType data) {
|
||||||
|
if (data instanceof StartingData) {
|
||||||
|
handleEvent((StartingData) data);
|
||||||
|
} else if (data instanceof StoppingData) {
|
||||||
|
handleEvent((StoppingData) data);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private void handleEvent(StartingData data) {
|
||||||
|
started = data.getWhen();
|
||||||
|
LOGGER.info(String.format("Receiver #%d sees application started at %s", id, started));
|
||||||
|
}
|
||||||
|
|
||||||
|
private void handleEvent(StoppingData data) {
|
||||||
|
stopped = data.getWhen();
|
||||||
|
LOGGER.info(String.format("Receiver #%d sees application stopping at %s", id, stopped));
|
||||||
|
LOGGER.info(String.format("Receiver #%d sending goodbye message", id));
|
||||||
|
data.getDataBus().publish(MessageData.of(String.format("Goodbye cruel world from #%d!", id)));
|
||||||
|
}
|
||||||
|
|
||||||
|
public LocalDateTime getStarted() {
|
||||||
|
return started;
|
||||||
|
}
|
||||||
|
|
||||||
|
public LocalDateTime getStopped() {
|
||||||
|
return stopped;
|
||||||
|
}
|
||||||
|
}
|
74
data-bus/src/test/java/com/iluwatar/databus/DataBusTest.java
Normal file
74
data-bus/src/test/java/com/iluwatar/databus/DataBusTest.java
Normal file
@ -0,0 +1,74 @@
|
|||||||
|
/**
|
||||||
|
* 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.databus;
|
||||||
|
|
||||||
|
import org.junit.Before;
|
||||||
|
import org.junit.Test;
|
||||||
|
import org.mockito.Mock;
|
||||||
|
import org.mockito.MockitoAnnotations;
|
||||||
|
|
||||||
|
import static org.mockito.BDDMockito.then;
|
||||||
|
import static org.mockito.Mockito.never;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Tests for {@link DataBus}.
|
||||||
|
*
|
||||||
|
* @author Paul Campbell (pcampbell@kemitix.net)
|
||||||
|
*/
|
||||||
|
public class DataBusTest {
|
||||||
|
|
||||||
|
@Mock
|
||||||
|
private Member member;
|
||||||
|
|
||||||
|
@Mock
|
||||||
|
private DataType event;
|
||||||
|
|
||||||
|
@Before
|
||||||
|
public void setUp() {
|
||||||
|
MockitoAnnotations.initMocks(this);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void publishedEventIsReceivedBySubscribedMember() {
|
||||||
|
//given
|
||||||
|
final DataBus dataBus = DataBus.getInstance();
|
||||||
|
dataBus.subscribe(member);
|
||||||
|
//when
|
||||||
|
dataBus.publish(event);
|
||||||
|
//then
|
||||||
|
then(member).should().accept(event);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void publishedEventIsNotReceivedByMemberAfterUnsubscribing() {
|
||||||
|
//given
|
||||||
|
final DataBus dataBus = DataBus.getInstance();
|
||||||
|
dataBus.subscribe(member);
|
||||||
|
dataBus.unsubscribe(member);
|
||||||
|
//when
|
||||||
|
dataBus.publish(event);
|
||||||
|
//then
|
||||||
|
then(member).should(never()).accept(event);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -0,0 +1,62 @@
|
|||||||
|
/**
|
||||||
|
* 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.databus.members;
|
||||||
|
|
||||||
|
import com.iluwatar.databus.data.MessageData;
|
||||||
|
import com.iluwatar.databus.data.StartingData;
|
||||||
|
import org.junit.Assert;
|
||||||
|
import org.junit.Test;
|
||||||
|
|
||||||
|
import java.time.LocalDateTime;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Tests for {@link MessageCollectorMember}.
|
||||||
|
*
|
||||||
|
* @author Paul Campbell (pcampbell@kemitix.net)
|
||||||
|
*/
|
||||||
|
public class MessageCollectorMemberTest {
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void collectMessageFromMessageData() {
|
||||||
|
//given
|
||||||
|
final String message = "message";
|
||||||
|
final MessageData messageData = new MessageData(message);
|
||||||
|
final MessageCollectorMember collector = new MessageCollectorMember("collector");
|
||||||
|
//when
|
||||||
|
collector.accept(messageData);
|
||||||
|
//then
|
||||||
|
Assert.assertTrue(collector.getMessages().contains(message));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void collectIgnoresMessageFromOtherDataTypes() {
|
||||||
|
//given
|
||||||
|
final StartingData startingData = new StartingData(LocalDateTime.now());
|
||||||
|
final MessageCollectorMember collector = new MessageCollectorMember("collector");
|
||||||
|
//when
|
||||||
|
collector.accept(startingData);
|
||||||
|
//then
|
||||||
|
Assert.assertEquals(0, collector.getMessages().size());
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -0,0 +1,79 @@
|
|||||||
|
/**
|
||||||
|
* 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.databus.members;
|
||||||
|
|
||||||
|
import com.iluwatar.databus.DataBus;
|
||||||
|
import com.iluwatar.databus.data.MessageData;
|
||||||
|
import com.iluwatar.databus.data.StartingData;
|
||||||
|
import com.iluwatar.databus.data.StoppingData;
|
||||||
|
import org.junit.Assert;
|
||||||
|
import org.junit.Test;
|
||||||
|
|
||||||
|
import java.time.LocalDateTime;
|
||||||
|
import java.time.Month;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Tests for {@link StatusMember}.
|
||||||
|
*
|
||||||
|
* @author Paul Campbell (pcampbell@kemitix.net)
|
||||||
|
*/
|
||||||
|
public class StatusMemberTest {
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void statusRecordsTheStartTime() {
|
||||||
|
//given
|
||||||
|
final LocalDateTime startTime = LocalDateTime.of(2017, Month.APRIL, 1, 19, 9);
|
||||||
|
final StartingData startingData = new StartingData(startTime);
|
||||||
|
final StatusMember statusMember = new StatusMember(1);
|
||||||
|
//when
|
||||||
|
statusMember.accept(startingData);
|
||||||
|
//then
|
||||||
|
Assert.assertEquals(startTime, statusMember.getStarted());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void statusRecordsTheStopTime() {
|
||||||
|
//given
|
||||||
|
final LocalDateTime stop = LocalDateTime.of(2017, Month.APRIL, 1, 19, 12);
|
||||||
|
final StoppingData stoppingData = new StoppingData(stop);
|
||||||
|
stoppingData.setDataBus(DataBus.getInstance());
|
||||||
|
final StatusMember statusMember = new StatusMember(1);
|
||||||
|
//when
|
||||||
|
statusMember.accept(stoppingData);
|
||||||
|
//then
|
||||||
|
Assert.assertEquals(stop, statusMember.getStopped());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void statusIgnoresMessageData() {
|
||||||
|
//given
|
||||||
|
final MessageData messageData = new MessageData("message");
|
||||||
|
final StatusMember statusMember = new StatusMember(1);
|
||||||
|
//when
|
||||||
|
statusMember.accept(messageData);
|
||||||
|
//then
|
||||||
|
Assert.assertNull(statusMember.getStarted());
|
||||||
|
Assert.assertNull(statusMember.getStopped());
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -28,7 +28,7 @@
|
|||||||
<parent>
|
<parent>
|
||||||
<groupId>com.iluwatar</groupId>
|
<groupId>com.iluwatar</groupId>
|
||||||
<artifactId>java-design-patterns</artifactId>
|
<artifactId>java-design-patterns</artifactId>
|
||||||
<version>1.15.0</version>
|
<version>1.16.0</version>
|
||||||
</parent>
|
</parent>
|
||||||
<artifactId>data-mapper</artifactId>
|
<artifactId>data-mapper</artifactId>
|
||||||
<dependencies>
|
<dependencies>
|
||||||
|
@ -29,7 +29,7 @@
|
|||||||
<parent>
|
<parent>
|
||||||
<groupId>com.iluwatar</groupId>
|
<groupId>com.iluwatar</groupId>
|
||||||
<artifactId>java-design-patterns</artifactId>
|
<artifactId>java-design-patterns</artifactId>
|
||||||
<version>1.15.0</version>
|
<version>1.16.0</version>
|
||||||
</parent>
|
</parent>
|
||||||
<artifactId>decorator</artifactId>
|
<artifactId>decorator</artifactId>
|
||||||
<dependencies>
|
<dependencies>
|
||||||
|
@ -30,7 +30,7 @@
|
|||||||
<parent>
|
<parent>
|
||||||
<artifactId>java-design-patterns</artifactId>
|
<artifactId>java-design-patterns</artifactId>
|
||||||
<groupId>com.iluwatar</groupId>
|
<groupId>com.iluwatar</groupId>
|
||||||
<version>1.15.0</version>
|
<version>1.16.0</version>
|
||||||
</parent>
|
</parent>
|
||||||
<modelVersion>4.0.0</modelVersion>
|
<modelVersion>4.0.0</modelVersion>
|
||||||
|
|
||||||
|
@ -29,7 +29,7 @@
|
|||||||
<parent>
|
<parent>
|
||||||
<groupId>com.iluwatar</groupId>
|
<groupId>com.iluwatar</groupId>
|
||||||
<artifactId>java-design-patterns</artifactId>
|
<artifactId>java-design-patterns</artifactId>
|
||||||
<version>1.15.0</version>
|
<version>1.16.0</version>
|
||||||
</parent>
|
</parent>
|
||||||
<artifactId>dependency-injection</artifactId>
|
<artifactId>dependency-injection</artifactId>
|
||||||
<dependencies>
|
<dependencies>
|
||||||
|
@ -0,0 +1,65 @@
|
|||||||
|
/**
|
||||||
|
* 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.dependency.injection;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The MIT License
|
||||||
|
* Copyright (c) 2014-2017 Ilkka Seppälä
|
||||||
|
* <p>
|
||||||
|
* 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:
|
||||||
|
* <p>
|
||||||
|
* The above copyright notice and this permission notice shall be included in
|
||||||
|
* all copies or substantial portions of the Software.
|
||||||
|
* <p>
|
||||||
|
* 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.
|
||||||
|
*/
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* AdvancedSorceress implements inversion of control. It depends on abstraction that can be injected
|
||||||
|
* through its setter.
|
||||||
|
*/
|
||||||
|
public class AdvancedSorceress implements Wizard {
|
||||||
|
|
||||||
|
private Tobacco tobacco;
|
||||||
|
|
||||||
|
public void setTobacco(Tobacco tobacco) {
|
||||||
|
this.tobacco = tobacco;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void smoke() {
|
||||||
|
tobacco.smoke(this);
|
||||||
|
}
|
||||||
|
}
|
@ -1,17 +1,17 @@
|
|||||||
/**
|
/**
|
||||||
* The MIT License
|
* The MIT License
|
||||||
* Copyright (c) 2014-2016 Ilkka Seppälä
|
* Copyright (c) 2014-2016 Ilkka Seppälä
|
||||||
*
|
* <p>
|
||||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||||
* of this software and associated documentation files (the "Software"), to deal
|
* of this software and associated documentation files (the "Software"), to deal
|
||||||
* in the Software without restriction, including without limitation the rights
|
* in the Software without restriction, including without limitation the rights
|
||||||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||||
* copies of the Software, and to permit persons to whom the Software is
|
* copies of the Software, and to permit persons to whom the Software is
|
||||||
* furnished to do so, subject to the following conditions:
|
* furnished to do so, subject to the following conditions:
|
||||||
*
|
* <p>
|
||||||
* The above copyright notice and this permission notice shall be included in
|
* The above copyright notice and this permission notice shall be included in
|
||||||
* all copies or substantial portions of the Software.
|
* all copies or substantial portions of the Software.
|
||||||
*
|
* <p>
|
||||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||||
@ -26,7 +26,6 @@ import com.google.inject.Guice;
|
|||||||
import com.google.inject.Injector;
|
import com.google.inject.Injector;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
*
|
|
||||||
* Dependency Injection pattern deals with how objects handle their dependencies. The pattern
|
* Dependency Injection pattern deals with how objects handle their dependencies. The pattern
|
||||||
* implements so called inversion of control principle. Inversion of control has two specific rules:
|
* implements so called inversion of control principle. Inversion of control has two specific rules:
|
||||||
* - High-level modules should not depend on low-level modules. Both should depend on abstractions.
|
* - High-level modules should not depend on low-level modules. Both should depend on abstractions.
|
||||||
@ -36,21 +35,21 @@ import com.google.inject.Injector;
|
|||||||
* naive implementation violating the inversion of control principle. It depends directly on a
|
* naive implementation violating the inversion of control principle. It depends directly on a
|
||||||
* concrete implementation which cannot be changed.
|
* concrete implementation which cannot be changed.
|
||||||
* <p>
|
* <p>
|
||||||
* The second wizard ({@link AdvancedWizard}) is more flexible. It does not depend on any concrete
|
* The second and third wizards({@link AdvancedWizard} and {@link AdvancedSorceress}) are more flexible.
|
||||||
* implementation but abstraction. It utilizes Dependency Injection pattern allowing its
|
* They do not depend on any concrete implementation but abstraction. They utilizes Dependency Injection
|
||||||
* {@link Tobacco} dependency to be injected through its constructor. This way, handling the
|
* pattern allowing their {@link Tobacco} dependency to be injected through constructor ({@link AdvancedWizard})
|
||||||
* dependency is no longer the wizard's responsibility. It is resolved outside the wizard class.
|
* or setter ({@link AdvancedSorceress}). This way, handling the dependency is no longer the wizard's
|
||||||
|
* responsibility. It is resolved outside the wizard class.
|
||||||
* <p>
|
* <p>
|
||||||
* The third example takes the pattern a step further. It uses Guice framework for Dependency
|
* The fourth example takes the pattern a step further. It uses Guice framework for Dependency
|
||||||
* Injection. {@link TobaccoModule} binds a concrete implementation to abstraction. Injector is then
|
* Injection. {@link TobaccoModule} binds a concrete implementation to abstraction. Injector is then
|
||||||
* used to create {@link GuiceWizard} object with correct dependencies.
|
* used to create {@link GuiceWizard} object with correct dependencies.
|
||||||
*
|
|
||||||
*/
|
*/
|
||||||
public class App {
|
public class App {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Program entry point
|
* Program entry point
|
||||||
*
|
*
|
||||||
* @param args command line args
|
* @param args command line args
|
||||||
*/
|
*/
|
||||||
public static void main(String[] args) {
|
public static void main(String[] args) {
|
||||||
@ -60,6 +59,10 @@ public class App {
|
|||||||
AdvancedWizard advancedWizard = new AdvancedWizard(new SecondBreakfastTobacco());
|
AdvancedWizard advancedWizard = new AdvancedWizard(new SecondBreakfastTobacco());
|
||||||
advancedWizard.smoke();
|
advancedWizard.smoke();
|
||||||
|
|
||||||
|
AdvancedSorceress advancedSorceress = new AdvancedSorceress();
|
||||||
|
advancedSorceress.setTobacco(new SecondBreakfastTobacco());
|
||||||
|
advancedSorceress.smoke();
|
||||||
|
|
||||||
Injector injector = Guice.createInjector(new TobaccoModule());
|
Injector injector = Guice.createInjector(new TobaccoModule());
|
||||||
GuiceWizard guiceWizard = injector.getInstance(GuiceWizard.class);
|
GuiceWizard guiceWizard = injector.getInstance(GuiceWizard.class);
|
||||||
guiceWizard.smoke();
|
guiceWizard.smoke();
|
||||||
|
@ -0,0 +1,76 @@
|
|||||||
|
/**
|
||||||
|
* The MIT License
|
||||||
|
* Copyright (c) 2014-2016 Ilkka Seppälä
|
||||||
|
* <p>
|
||||||
|
* 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:
|
||||||
|
* <p>
|
||||||
|
* The above copyright notice and this permission notice shall be included in
|
||||||
|
* all copies or substantial portions of the Software.
|
||||||
|
* <p>
|
||||||
|
* 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.dependency.injection;
|
||||||
|
|
||||||
|
import com.iluwatar.dependency.injection.utils.InMemoryAppender;
|
||||||
|
import org.junit.After;
|
||||||
|
import org.junit.Before;
|
||||||
|
import org.junit.Test;
|
||||||
|
|
||||||
|
import static org.junit.Assert.assertEquals;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Date: 28/04/17 - 7:40 AM
|
||||||
|
*
|
||||||
|
* @author Stanislav Kapinus
|
||||||
|
*/
|
||||||
|
|
||||||
|
public class AdvancedSorceressTest {
|
||||||
|
|
||||||
|
private InMemoryAppender appender;
|
||||||
|
|
||||||
|
@Before
|
||||||
|
public void setUp() {
|
||||||
|
appender = new InMemoryAppender(Tobacco.class);
|
||||||
|
}
|
||||||
|
|
||||||
|
@After
|
||||||
|
public void tearDown() {
|
||||||
|
appender.stop();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Test if the {@link AdvancedSorceress} smokes whatever instance of {@link Tobacco} is passed to her
|
||||||
|
* through the setter's parameter
|
||||||
|
*/
|
||||||
|
@Test
|
||||||
|
public void testSmokeEveryThing() throws Exception {
|
||||||
|
|
||||||
|
final Tobacco[] tobaccos = {
|
||||||
|
new OldTobyTobacco(), new RivendellTobacco(), new SecondBreakfastTobacco()
|
||||||
|
};
|
||||||
|
|
||||||
|
for (final Tobacco tobacco : tobaccos) {
|
||||||
|
final AdvancedSorceress advancedSorceress = new AdvancedSorceress();
|
||||||
|
advancedSorceress.setTobacco(tobacco);
|
||||||
|
advancedSorceress.smoke();
|
||||||
|
// Verify if the sorceress is smoking the correct tobacco ...
|
||||||
|
assertEquals("AdvancedSorceress smoking " + tobacco.getClass().getSimpleName(), appender.getLastMessage());
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
// ... and nothing else is happening.
|
||||||
|
assertEquals(tobaccos.length, appender.getLogSize());
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
@ -27,7 +27,7 @@
|
|||||||
<parent>
|
<parent>
|
||||||
<groupId>com.iluwatar</groupId>
|
<groupId>com.iluwatar</groupId>
|
||||||
<artifactId>java-design-patterns</artifactId>
|
<artifactId>java-design-patterns</artifactId>
|
||||||
<version>1.15.0</version>
|
<version>1.16.0</version>
|
||||||
</parent>
|
</parent>
|
||||||
<artifactId>double-checked-locking</artifactId>
|
<artifactId>double-checked-locking</artifactId>
|
||||||
<dependencies>
|
<dependencies>
|
||||||
|
@ -29,7 +29,7 @@
|
|||||||
<parent>
|
<parent>
|
||||||
<groupId>com.iluwatar</groupId>
|
<groupId>com.iluwatar</groupId>
|
||||||
<artifactId>java-design-patterns</artifactId>
|
<artifactId>java-design-patterns</artifactId>
|
||||||
<version>1.15.0</version>
|
<version>1.16.0</version>
|
||||||
</parent>
|
</parent>
|
||||||
<artifactId>double-dispatch</artifactId>
|
<artifactId>double-dispatch</artifactId>
|
||||||
<dependencies>
|
<dependencies>
|
||||||
|
@ -28,7 +28,7 @@
|
|||||||
<parent>
|
<parent>
|
||||||
<groupId>com.iluwatar</groupId>
|
<groupId>com.iluwatar</groupId>
|
||||||
<artifactId>java-design-patterns</artifactId>
|
<artifactId>java-design-patterns</artifactId>
|
||||||
<version>1.15.0</version>
|
<version>1.16.0</version>
|
||||||
</parent>
|
</parent>
|
||||||
<artifactId>event-aggregator</artifactId>
|
<artifactId>event-aggregator</artifactId>
|
||||||
<dependencies>
|
<dependencies>
|
||||||
|
@ -29,7 +29,7 @@
|
|||||||
<parent>
|
<parent>
|
||||||
<groupId>com.iluwatar</groupId>
|
<groupId>com.iluwatar</groupId>
|
||||||
<artifactId>java-design-patterns</artifactId>
|
<artifactId>java-design-patterns</artifactId>
|
||||||
<version>1.15.0</version>
|
<version>1.16.0</version>
|
||||||
</parent>
|
</parent>
|
||||||
<artifactId>event-asynchronous</artifactId>
|
<artifactId>event-asynchronous</artifactId>
|
||||||
<dependencies>
|
<dependencies>
|
||||||
|
@ -32,7 +32,7 @@ Use an Event-driven architecture when
|
|||||||
|
|
||||||
## Credits
|
## Credits
|
||||||
|
|
||||||
* [Event-driven architecture - Wikipedia](http://www.computerweekly.com/feature/Write-through-write-around-write-back-Cache-explained)
|
* [Event-driven architecture - Wikipedia](https://en.wikipedia.org/wiki/Event-driven_architecture)
|
||||||
* [Fundamental Components of an Event-Driven Architecture](http://giocc.com/fundamental-components-of-an-event-driven-architecture.html)
|
* [Fundamental Components of an Event-Driven Architecture](http://giocc.com/fundamental-components-of-an-event-driven-architecture.html)
|
||||||
* [Real World Applications/Event Driven Applications](https://wiki.haskell.org/Real_World_Applications/Event_Driven_Applications)
|
* [Real World Applications/Event Driven Applications](https://wiki.haskell.org/Real_World_Applications/Event_Driven_Applications)
|
||||||
* [Event-driven architecture definition](http://searchsoa.techtarget.com/definition/event-driven-architecture)
|
* [Event-driven architecture definition](http://searchsoa.techtarget.com/definition/event-driven-architecture)
|
||||||
|
@ -31,7 +31,7 @@
|
|||||||
<parent>
|
<parent>
|
||||||
<groupId>com.iluwatar</groupId>
|
<groupId>com.iluwatar</groupId>
|
||||||
<artifactId>java-design-patterns</artifactId>
|
<artifactId>java-design-patterns</artifactId>
|
||||||
<version>1.15.0</version>
|
<version>1.16.0</version>
|
||||||
</parent>
|
</parent>
|
||||||
|
|
||||||
<artifactId>event-driven-architecture</artifactId>
|
<artifactId>event-driven-architecture</artifactId>
|
||||||
|
29
event-queue/README.md
Normal file
29
event-queue/README.md
Normal file
@ -0,0 +1,29 @@
|
|||||||
|
---
|
||||||
|
layout: pattern
|
||||||
|
title: Event Queue
|
||||||
|
folder: event-queue
|
||||||
|
permalink: /patterns/event-queue/
|
||||||
|
categories: Concurrency
|
||||||
|
tags:
|
||||||
|
- Java
|
||||||
|
- Difficulty Intermediate
|
||||||
|
- Queue
|
||||||
|
---
|
||||||
|
|
||||||
|
## Intent
|
||||||
|
Event Queue is a good pattern if You have a limited accesibility resource (for example:
|
||||||
|
Audio or Database), but You need to handle all the requests that want to use that.
|
||||||
|
It puts all the requests in a queue and process them asynchronously.
|
||||||
|
Gives the resource for the event when it is the next in the queue and in same time
|
||||||
|
removes it from the queue.
|
||||||
|
|
||||||
|

|
||||||
|
|
||||||
|
## Applicability
|
||||||
|
Use the Event Queue pattern when
|
||||||
|
|
||||||
|
* You have a limited accesibility resource and the asynchronous process is acceptable to reach that
|
||||||
|
|
||||||
|
## Credits
|
||||||
|
|
||||||
|
* [Mihaly Kuprivecz - Event Queue] (http://gameprogrammingpatterns.com/event-queue.html)
|
BIN
event-queue/etc/Bass-Drum-1.aif
Normal file
BIN
event-queue/etc/Bass-Drum-1.aif
Normal file
Binary file not shown.
BIN
event-queue/etc/Bass-Drum-1.wav
Normal file
BIN
event-queue/etc/Bass-Drum-1.wav
Normal file
Binary file not shown.
BIN
event-queue/etc/Closed-Hi-Hat-1.aif
Normal file
BIN
event-queue/etc/Closed-Hi-Hat-1.aif
Normal file
Binary file not shown.
BIN
event-queue/etc/Closed-Hi-Hat-1.wav
Normal file
BIN
event-queue/etc/Closed-Hi-Hat-1.wav
Normal file
Binary file not shown.
26
event-queue/etc/event-queue.urm.puml
Normal file
26
event-queue/etc/event-queue.urm.puml
Normal file
@ -0,0 +1,26 @@
|
|||||||
|
@startuml
|
||||||
|
package com.iluwatar.event.queue {
|
||||||
|
class App {
|
||||||
|
+ App()
|
||||||
|
+ getAudioStream(filePath : String) : AudioInputStream {static}
|
||||||
|
+ main(args : String[]) {static}
|
||||||
|
}
|
||||||
|
class Audio {
|
||||||
|
- MAX_PENDING : int {static}
|
||||||
|
- headIndex : int {static}
|
||||||
|
- pendingAudio : PlayMessage[] {static}
|
||||||
|
- tailIndex : int {static}
|
||||||
|
- updateThread : Thread {static}
|
||||||
|
+ Audio()
|
||||||
|
+ init() {static}
|
||||||
|
+ playSound(stream : AudioInputStream, volume : float) {static}
|
||||||
|
+ stopService() {static}
|
||||||
|
+ update() {static}
|
||||||
|
}
|
||||||
|
class PlayMessage {
|
||||||
|
~ stream : AudioInputStream
|
||||||
|
~ volume : float
|
||||||
|
+ PlayMessage()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
@enduml
|
BIN
event-queue/etc/model.png
Normal file
BIN
event-queue/etc/model.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 17 KiB |
36
event-queue/etc/model.ucls
Normal file
36
event-queue/etc/model.ucls
Normal file
@ -0,0 +1,36 @@
|
|||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<class-diagram version="1.2.0" icons="true" automaticImage="PNG" always-add-relationships="false" generalizations="true"
|
||||||
|
realizations="true" associations="true" dependencies="false" nesting-relationships="true" router="FAN">
|
||||||
|
<class id="1" language="java" name="com.iluwatar.event.queue.Audio" project="event-queue"
|
||||||
|
file="/event-queue/src/main/java/com/iluwatar/event/queue/Audio.java" binary="false" corner="BOTTOM_RIGHT">
|
||||||
|
<position height="-1" width="-1" x="285" y="179"/>
|
||||||
|
<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="2" language="java" name="com.iluwatar.event.queue.PlayMessage" project="event-queue"
|
||||||
|
file="/event-queue/src/main/java/com/iluwatar/event/queue/PlayMessage.java" binary="false" corner="BOTTOM_RIGHT">
|
||||||
|
<position height="-1" width="-1" x="633" y="179"/>
|
||||||
|
<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>
|
||||||
|
<association id="3">
|
||||||
|
<end type="SOURCE" refId="1" navigable="false">
|
||||||
|
<attribute id="4" name="pendingAudio"/>
|
||||||
|
<multiplicity id="5" minimum="0" maximum="2147483647"/>
|
||||||
|
</end>
|
||||||
|
<end type="TARGET" refId="2" navigable="true"/>
|
||||||
|
<display labels="true" multiplicity="true"/>
|
||||||
|
</association>
|
||||||
|
<classifier-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"/>
|
||||||
|
</classifier-display>
|
||||||
|
<association-display labels="true" multiplicity="true"/>
|
||||||
|
</class-diagram>
|
43
event-queue/pom.xml
Normal file
43
event-queue/pom.xml
Normal file
@ -0,0 +1,43 @@
|
|||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<!--
|
||||||
|
|
||||||
|
The MIT License
|
||||||
|
Copyright (c) 2014-2016 Ilkka Sepp<70>l<EFBFBD>
|
||||||
|
|
||||||
|
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||||
|
of this software and associated documentation files (the "Software"), to deal
|
||||||
|
in the Software without restriction, including without limitation the rights
|
||||||
|
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||||
|
copies of the Software, and to permit persons to whom the Software is
|
||||||
|
furnished to do so, subject to the following conditions:
|
||||||
|
|
||||||
|
The above copyright notice and this permission notice shall be included in
|
||||||
|
all copies or substantial portions of the Software.
|
||||||
|
|
||||||
|
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||||
|
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||||
|
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||||
|
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||||
|
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||||
|
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||||
|
THE SOFTWARE.
|
||||||
|
|
||||||
|
-->
|
||||||
|
<project xmlns="http://maven.apache.org/POM/4.0.0"
|
||||||
|
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||||
|
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||||
|
<modelVersion>4.0.0</modelVersion>
|
||||||
|
<parent>
|
||||||
|
<artifactId>java-design-patterns</artifactId>
|
||||||
|
<groupId>com.iluwatar</groupId>
|
||||||
|
<version>1.16.0</version>
|
||||||
|
</parent>
|
||||||
|
<artifactId>event-queue</artifactId>
|
||||||
|
<dependencies>
|
||||||
|
<dependency>
|
||||||
|
<groupId>junit</groupId>
|
||||||
|
<artifactId>junit</artifactId>
|
||||||
|
<scope>test</scope>
|
||||||
|
</dependency>
|
||||||
|
</dependencies>
|
||||||
|
</project>
|
59
event-queue/src/main/java/com/iluwatar/event/queue/App.java
Normal file
59
event-queue/src/main/java/com/iluwatar/event/queue/App.java
Normal file
@ -0,0 +1,59 @@
|
|||||||
|
/**
|
||||||
|
* The MIT License
|
||||||
|
* Copyright (c) 2014-2016 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.event.queue;
|
||||||
|
|
||||||
|
import java.io.BufferedReader;
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.io.InputStreamReader;
|
||||||
|
|
||||||
|
import javax.sound.sampled.UnsupportedAudioFileException;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Event or message queues provide an asynchronous communications protocol, meaning that the sender
|
||||||
|
* and receiver of the message do not need to interact with the message queue at the same time.
|
||||||
|
* Events or messages placed onto the queue are stored until the recipient retrieves them. Event
|
||||||
|
* or message queues have implicit or explicit limits on the size of data that may be transmitted
|
||||||
|
* in a single message and the number of messages that may remain outstanding on the queue.
|
||||||
|
* A queue stores a series of notifications or requests in first-in, first-out order.
|
||||||
|
* Sending a notification enqueues the request and returns. The request processor then processes
|
||||||
|
* items from the queue at a later time.
|
||||||
|
*/
|
||||||
|
public class App {
|
||||||
|
/**
|
||||||
|
* Program entry point.
|
||||||
|
*
|
||||||
|
* @param args command line args
|
||||||
|
* @throws IOException when there is a problem with the audio file loading
|
||||||
|
* @throws UnsupportedAudioFileException when the loaded audio file is unsupported
|
||||||
|
*/
|
||||||
|
public static void main(String[] args) throws UnsupportedAudioFileException, IOException {
|
||||||
|
Audio.playSound(Audio.getAudioStream("./etc/Bass-Drum-1.wav"), -10.0f);
|
||||||
|
Audio.playSound(Audio.getAudioStream("./etc/Closed-Hi-Hat-1.wav"), -8.0f);
|
||||||
|
|
||||||
|
System.out.println("Press Enter key to stop the program...");
|
||||||
|
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
|
||||||
|
br.read();
|
||||||
|
Audio.stopService();
|
||||||
|
}
|
||||||
|
}
|
169
event-queue/src/main/java/com/iluwatar/event/queue/Audio.java
Normal file
169
event-queue/src/main/java/com/iluwatar/event/queue/Audio.java
Normal file
@ -0,0 +1,169 @@
|
|||||||
|
/**
|
||||||
|
* The MIT License
|
||||||
|
* Copyright (c) 2014-2016 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.event.queue;
|
||||||
|
|
||||||
|
import java.io.File;
|
||||||
|
import java.io.IOException;
|
||||||
|
|
||||||
|
import javax.sound.sampled.AudioInputStream;
|
||||||
|
import javax.sound.sampled.AudioSystem;
|
||||||
|
import javax.sound.sampled.Clip;
|
||||||
|
import javax.sound.sampled.LineUnavailableException;
|
||||||
|
import javax.sound.sampled.UnsupportedAudioFileException;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* This class implements the Event Queue pattern.
|
||||||
|
* @author mkuprivecz
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
public class Audio {
|
||||||
|
|
||||||
|
private static final int MAX_PENDING = 16;
|
||||||
|
|
||||||
|
private static int headIndex;
|
||||||
|
|
||||||
|
private static int tailIndex;
|
||||||
|
|
||||||
|
private static Thread updateThread = null;
|
||||||
|
|
||||||
|
private static PlayMessage[] pendingAudio = new PlayMessage[MAX_PENDING];
|
||||||
|
|
||||||
|
/**
|
||||||
|
* This method stops the Update Method's thread.
|
||||||
|
*/
|
||||||
|
public static synchronized void stopService() {
|
||||||
|
if (updateThread != null) {
|
||||||
|
updateThread.interrupt();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* This method stops the Update Method's thread.
|
||||||
|
* @return boolean
|
||||||
|
*/
|
||||||
|
public static synchronized boolean isServiceRunning() {
|
||||||
|
if (updateThread != null && updateThread.isAlive() ) {
|
||||||
|
return true;
|
||||||
|
} else {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Starts the thread for the Update Method pattern if it was not started previously.
|
||||||
|
* Also when the thread is is ready initializes the indexes of the queue
|
||||||
|
*/
|
||||||
|
public static void init() {
|
||||||
|
if (updateThread == null) {
|
||||||
|
updateThread = new Thread(new Runnable() {
|
||||||
|
public void run() {
|
||||||
|
while (!Thread.currentThread().isInterrupted()) {
|
||||||
|
Audio.update();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
startThread();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* This is a synchronized thread starter
|
||||||
|
*/
|
||||||
|
public static synchronized void startThread() {
|
||||||
|
if (!updateThread.isAlive()) {
|
||||||
|
updateThread.start();
|
||||||
|
headIndex = 0;
|
||||||
|
tailIndex = 0;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* This method adds a new audio into the queue.
|
||||||
|
* @param stream is the AudioInputStream for the method
|
||||||
|
* @param volume is the level of the audio's volume
|
||||||
|
*/
|
||||||
|
public static void playSound(AudioInputStream stream, float volume) {
|
||||||
|
init();
|
||||||
|
// Walk the pending requests.
|
||||||
|
for (int i = headIndex; i != tailIndex; i = (i + 1) % MAX_PENDING) {
|
||||||
|
if (getPendingAudio()[i].getStream() == stream) {
|
||||||
|
// Use the larger of the two volumes.
|
||||||
|
getPendingAudio()[i].setVolume(Math.max(volume, getPendingAudio()[i].getVolume()));
|
||||||
|
|
||||||
|
// Don't need to enqueue.
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
getPendingAudio()[tailIndex] = new PlayMessage(stream, volume);
|
||||||
|
tailIndex = (tailIndex + 1) % MAX_PENDING;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* This method uses the Update Method pattern.
|
||||||
|
* It takes the audio from the queue and plays it
|
||||||
|
*/
|
||||||
|
public static void update() {
|
||||||
|
// If there are no pending requests, do nothing.
|
||||||
|
if (headIndex == tailIndex) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
Clip clip = null;
|
||||||
|
try {
|
||||||
|
AudioInputStream audioStream = getPendingAudio()[headIndex].getStream();
|
||||||
|
headIndex++;
|
||||||
|
clip = AudioSystem.getClip();
|
||||||
|
clip.open(audioStream);
|
||||||
|
clip.start();
|
||||||
|
} catch (LineUnavailableException e) {
|
||||||
|
System.err.println("Error occoured while loading the audio: The line is unavailable");
|
||||||
|
e.printStackTrace();
|
||||||
|
} catch (IOException e) {
|
||||||
|
System.err.println("Input/Output error while loading the audio");
|
||||||
|
e.printStackTrace();
|
||||||
|
} catch (IllegalArgumentException e) {
|
||||||
|
System.err.println("The system doesn't support the sound: " + e.getMessage());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns the AudioInputStream of a file
|
||||||
|
* @param filePath is the path of the audio file
|
||||||
|
* @return AudioInputStream
|
||||||
|
* @throws UnsupportedAudioFileException when the audio file is not supported
|
||||||
|
* @throws IOException when the file is not readable
|
||||||
|
*/
|
||||||
|
public static AudioInputStream getAudioStream(String filePath)
|
||||||
|
throws UnsupportedAudioFileException, IOException {
|
||||||
|
return AudioSystem.getAudioInputStream(new File(filePath).getAbsoluteFile());
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns with the message array of the queue
|
||||||
|
* @return PlayMessage[]
|
||||||
|
*/
|
||||||
|
public static PlayMessage[] getPendingAudio() {
|
||||||
|
return pendingAudio;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -0,0 +1,59 @@
|
|||||||
|
/**
|
||||||
|
* The MIT License
|
||||||
|
* Copyright (c) 2014-2016 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.event.queue;
|
||||||
|
|
||||||
|
import javax.sound.sampled.AudioInputStream;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The Event Queue's queue will store the instances of this class.
|
||||||
|
* @author mkuprivecz
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
public class PlayMessage {
|
||||||
|
|
||||||
|
private AudioInputStream stream;
|
||||||
|
|
||||||
|
private float volume;
|
||||||
|
|
||||||
|
public PlayMessage(AudioInputStream stream, float volume) {
|
||||||
|
setStream(stream);
|
||||||
|
setVolume(volume);
|
||||||
|
}
|
||||||
|
|
||||||
|
public AudioInputStream getStream() {
|
||||||
|
return stream;
|
||||||
|
}
|
||||||
|
|
||||||
|
private void setStream(AudioInputStream stream) {
|
||||||
|
this.stream = stream;
|
||||||
|
}
|
||||||
|
|
||||||
|
public float getVolume() {
|
||||||
|
return volume;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setVolume(float volume) {
|
||||||
|
this.volume = volume;
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,77 @@
|
|||||||
|
/**
|
||||||
|
* The MIT License
|
||||||
|
* Copyright (c) 2014-2016 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.event.queue;
|
||||||
|
import static org.junit.Assert.*;
|
||||||
|
|
||||||
|
import java.io.IOException;
|
||||||
|
|
||||||
|
import javax.sound.sampled.UnsupportedAudioFileException;
|
||||||
|
|
||||||
|
import org.junit.Test;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Testing the Audio service of the Queue
|
||||||
|
* @author mkuprivecz
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
public class AudioTest {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Test here that the playSound method works correctly
|
||||||
|
* @throws UnsupportedAudioFileException when the audio file is not supported
|
||||||
|
* @throws IOException when the file is not readable
|
||||||
|
* @throws InterruptedException when the test is interrupted externally
|
||||||
|
*/
|
||||||
|
@Test
|
||||||
|
public void testPlaySound() throws UnsupportedAudioFileException, IOException, InterruptedException {
|
||||||
|
Audio.playSound(Audio.getAudioStream("./etc/Bass-Drum-1.wav"), -10.0f);
|
||||||
|
// test that service is started
|
||||||
|
assertTrue(Audio.isServiceRunning());
|
||||||
|
// adding a small pause to be sure that the sound is ended
|
||||||
|
Thread.sleep(5000);
|
||||||
|
// test that service is finished
|
||||||
|
assertFalse(!Audio.isServiceRunning());
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Test here that the Queue
|
||||||
|
* @throws UnsupportedAudioFileException when the audio file is not supported
|
||||||
|
* @throws IOException when the file is not readable
|
||||||
|
* @throws InterruptedException when the test is interrupted externally
|
||||||
|
*/
|
||||||
|
@Test
|
||||||
|
public void testQueue() throws UnsupportedAudioFileException, IOException, InterruptedException {
|
||||||
|
Audio.playSound(Audio.getAudioStream("./etc/Bass-Drum-1.aif"), -10.0f);
|
||||||
|
Audio.playSound(Audio.getAudioStream("./etc/Bass-Drum-1.aif"), -10.0f);
|
||||||
|
Audio.playSound(Audio.getAudioStream("./etc/Bass-Drum-1.aif"), -10.0f);
|
||||||
|
assertTrue(Audio.getPendingAudio().length > 0);
|
||||||
|
// test that service is started
|
||||||
|
assertTrue(Audio.isServiceRunning());
|
||||||
|
// adding a small pause to be sure that the sound is ended
|
||||||
|
Thread.sleep(10000);
|
||||||
|
// test that service is finished
|
||||||
|
assertFalse(!Audio.isServiceRunning());
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -29,7 +29,7 @@
|
|||||||
<parent>
|
<parent>
|
||||||
<groupId>com.iluwatar</groupId>
|
<groupId>com.iluwatar</groupId>
|
||||||
<artifactId>java-design-patterns</artifactId>
|
<artifactId>java-design-patterns</artifactId>
|
||||||
<version>1.15.0</version>
|
<version>1.16.0</version>
|
||||||
</parent>
|
</parent>
|
||||||
<artifactId>execute-around</artifactId>
|
<artifactId>execute-around</artifactId>
|
||||||
<dependencies>
|
<dependencies>
|
||||||
|
28
extension-objects/README.md
Normal file
28
extension-objects/README.md
Normal file
@ -0,0 +1,28 @@
|
|||||||
|
---
|
||||||
|
layout: pattern
|
||||||
|
title: Extension objects
|
||||||
|
folder: extension-objects
|
||||||
|
permalink: /patterns/extension-objects/
|
||||||
|
categories: Behavioral
|
||||||
|
tags:
|
||||||
|
- Java
|
||||||
|
- Difficulty-Intermediate
|
||||||
|
---
|
||||||
|
|
||||||
|
## Intent
|
||||||
|
Anticipate that an object’s interface needs to be extended in the future. Additional
|
||||||
|
interfaces are defined by extension objects.
|
||||||
|
|
||||||
|

|
||||||
|
|
||||||
|
## Applicability
|
||||||
|
Use the Extension Objects pattern when:
|
||||||
|
|
||||||
|
* you need to support the addition of new or unforeseen interfaces to existing classes and you don't want to impact clients that don't need this new interface. Extension Objects lets you keep related operations together by defining them in a separate class
|
||||||
|
* a class representing a key abstraction plays different roles for different clients. The number of roles the class can play should be open-ended. There is a need to preserve the key abstraction itself. For example, a customer object is still a customer object even if different subsystems view it differently.
|
||||||
|
* a class should be extensible with new behavior without subclassing from it.
|
||||||
|
|
||||||
|
## Real world examples
|
||||||
|
|
||||||
|
* [OpenDoc](https://en.wikipedia.org/wiki/OpenDoc)
|
||||||
|
* [Object Linking and Embedding](https://en.wikipedia.org/wiki/Object_Linking_and_Embedding)
|
2
extension-objects/etc/extension-objects.urm.puml
Normal file
2
extension-objects/etc/extension-objects.urm.puml
Normal file
@ -0,0 +1,2 @@
|
|||||||
|
@startuml
|
||||||
|
@enduml
|
BIN
extension-objects/etc/extension_obj.png
Normal file
BIN
extension-objects/etc/extension_obj.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 37 KiB |
180
extension-objects/etc/extension_obj.ucls
Normal file
180
extension-objects/etc/extension_obj.ucls
Normal file
@ -0,0 +1,180 @@
|
|||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<class-diagram version="1.2.0" icons="true" automaticImage="PNG" always-add-relationships="false" generalizations="true"
|
||||||
|
realizations="true" associations="true" dependencies="false" nesting-relationships="true" router="FAN">
|
||||||
|
<class id="1" language="java" name="concreteextensions.Soldier" project="extension-objects"
|
||||||
|
file="/extension-objects/src/main/java/concreteextensions/Soldier.java" binary="false" corner="BOTTOM_RIGHT">
|
||||||
|
<position height="-1" width="-1" x="483" y="339"/>
|
||||||
|
<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="2" language="java" name="units.Unit" project="extension-objects"
|
||||||
|
file="/extension-objects/src/main/java/units/Unit.java" binary="false" corner="BOTTOM_RIGHT">
|
||||||
|
<position height="-1" width="-1" x="192" y="115"/>
|
||||||
|
<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="3" language="java" name="abstractextensions.SoldierExtension" project="extension-objects"
|
||||||
|
file="/extension-objects/src/main/java/abstractextensions/SoldierExtension.java" binary="false"
|
||||||
|
corner="BOTTOM_RIGHT">
|
||||||
|
<position height="-1" width="-1" x="510" y="229"/>
|
||||||
|
<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>
|
||||||
|
</interface>
|
||||||
|
<interface id="4" language="java" name="abstractextensions.UnitExtension" project="extension-objects"
|
||||||
|
file="/extension-objects/src/main/java/abstractextensions/UnitExtension.java" binary="false" corner="BOTTOM_RIGHT">
|
||||||
|
<position height="-1" width="-1" x="510" y="116"/>
|
||||||
|
<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>
|
||||||
|
</interface>
|
||||||
|
<class id="5" language="java" name="units.SoldierUnit" project="extension-objects"
|
||||||
|
file="/extension-objects/src/main/java/units/SoldierUnit.java" binary="false" corner="BOTTOM_RIGHT">
|
||||||
|
<position height="-1" width="-1" x="157" y="355"/>
|
||||||
|
<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="6" language="java" name="concreteextensions.Sergeant" project="extension-objects"
|
||||||
|
file="/extension-objects/src/main/java/concreteextensions/Sergeant.java" binary="false" corner="BOTTOM_RIGHT">
|
||||||
|
<position height="-1" width="-1" x="650" y="375"/>
|
||||||
|
<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="7" language="java" name="abstractextensions.SergeantExtension" project="extension-objects"
|
||||||
|
file="/extension-objects/src/main/java/abstractextensions/SergeantExtension.java" binary="false"
|
||||||
|
corner="BOTTOM_RIGHT">
|
||||||
|
<position height="-1" width="-1" x="672" y="230"/>
|
||||||
|
<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>
|
||||||
|
</interface>
|
||||||
|
<class id="8" language="java" name="units.SergeantUnit" project="extension-objects"
|
||||||
|
file="/extension-objects/src/main/java/units/SergeantUnit.java" binary="false" corner="BOTTOM_RIGHT">
|
||||||
|
<position height="-1" width="-1" x="315" y="460"/>
|
||||||
|
<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="9" language="java" name="units.CommanderUnit" project="extension-objects"
|
||||||
|
file="/extension-objects/src/main/java/units/CommanderUnit.java" binary="false" corner="BOTTOM_RIGHT">
|
||||||
|
<position height="99" width="210" x="429" y="476"/>
|
||||||
|
<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="10" language="java" name="concreteextensions.Commander" project="extension-objects"
|
||||||
|
file="/extension-objects/src/main/java/concreteextensions/Commander.java" binary="false" corner="BOTTOM_RIGHT">
|
||||||
|
<position height="-1" width="-1" x="823" y="477"/>
|
||||||
|
<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="11" language="java" name="abstractextensions.CommanderExtension" project="extension-objects"
|
||||||
|
file="/extension-objects/src/main/java/abstractextensions/CommanderExtension.java" binary="false"
|
||||||
|
corner="BOTTOM_RIGHT">
|
||||||
|
<position height="-1" width="-1" x="827" y="217"/>
|
||||||
|
<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>
|
||||||
|
</interface>
|
||||||
|
<realization id="12">
|
||||||
|
<end type="SOURCE" refId="1"/>
|
||||||
|
<end type="TARGET" refId="3"/>
|
||||||
|
</realization>
|
||||||
|
<realization id="13">
|
||||||
|
<end type="SOURCE" refId="10"/>
|
||||||
|
<end type="TARGET" refId="11"/>
|
||||||
|
</realization>
|
||||||
|
<generalization id="14">
|
||||||
|
<end type="SOURCE" refId="9"/>
|
||||||
|
<end type="TARGET" refId="2"/>
|
||||||
|
</generalization>
|
||||||
|
<association id="15">
|
||||||
|
<end type="SOURCE" refId="1" navigable="false">
|
||||||
|
<attribute id="16" name="unit"/>
|
||||||
|
<multiplicity id="17" minimum="0" maximum="1"/>
|
||||||
|
</end>
|
||||||
|
<end type="TARGET" refId="5" navigable="true"/>
|
||||||
|
<display labels="true" multiplicity="true"/>
|
||||||
|
</association>
|
||||||
|
<association id="18">
|
||||||
|
<end type="SOURCE" refId="10" navigable="false">
|
||||||
|
<attribute id="19" name="unit"/>
|
||||||
|
<multiplicity id="20" minimum="0" maximum="1"/>
|
||||||
|
</end>
|
||||||
|
<end type="TARGET" refId="9" navigable="true"/>
|
||||||
|
<display labels="true" multiplicity="true"/>
|
||||||
|
</association>
|
||||||
|
<association id="21">
|
||||||
|
<end type="SOURCE" refId="2" navigable="false">
|
||||||
|
<attribute id="22" name="unitExtension"/>
|
||||||
|
<multiplicity id="23" minimum="0" maximum="1"/>
|
||||||
|
</end>
|
||||||
|
<end type="TARGET" refId="4" navigable="true"/>
|
||||||
|
<display labels="true" multiplicity="true"/>
|
||||||
|
</association>
|
||||||
|
<realization id="24">
|
||||||
|
<end type="SOURCE" refId="6"/>
|
||||||
|
<end type="TARGET" refId="7"/>
|
||||||
|
</realization>
|
||||||
|
<generalization id="25">
|
||||||
|
<end type="SOURCE" refId="3"/>
|
||||||
|
<end type="TARGET" refId="4"/>
|
||||||
|
</generalization>
|
||||||
|
<generalization id="26">
|
||||||
|
<end type="SOURCE" refId="5"/>
|
||||||
|
<end type="TARGET" refId="2"/>
|
||||||
|
</generalization>
|
||||||
|
<generalization id="27">
|
||||||
|
<end type="SOURCE" refId="7"/>
|
||||||
|
<end type="TARGET" refId="4"/>
|
||||||
|
</generalization>
|
||||||
|
<generalization id="28">
|
||||||
|
<end type="SOURCE" refId="8"/>
|
||||||
|
<end type="TARGET" refId="2"/>
|
||||||
|
</generalization>
|
||||||
|
<association id="29">
|
||||||
|
<end type="SOURCE" refId="6" navigable="false">
|
||||||
|
<attribute id="30" name="unit"/>
|
||||||
|
<multiplicity id="31" minimum="0" maximum="1"/>
|
||||||
|
</end>
|
||||||
|
<end type="TARGET" refId="8" navigable="true"/>
|
||||||
|
<display labels="true" multiplicity="true"/>
|
||||||
|
</association>
|
||||||
|
<generalization id="32">
|
||||||
|
<end type="SOURCE" refId="11"/>
|
||||||
|
<end type="TARGET" refId="4"/>
|
||||||
|
</generalization>
|
||||||
|
<classifier-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"/>
|
||||||
|
</classifier-display>
|
||||||
|
<association-display labels="true" multiplicity="true"/>
|
||||||
|
</class-diagram>
|
45
extension-objects/pom.xml
Normal file
45
extension-objects/pom.xml
Normal file
@ -0,0 +1,45 @@
|
|||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<!--
|
||||||
|
|
||||||
|
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 xmlns="http://maven.apache.org/POM/4.0.0"
|
||||||
|
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||||
|
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||||
|
<parent>
|
||||||
|
<artifactId>java-design-patterns</artifactId>
|
||||||
|
<groupId>com.iluwatar</groupId>
|
||||||
|
<version>1.16.0</version>
|
||||||
|
</parent>
|
||||||
|
<modelVersion>4.0.0</modelVersion>
|
||||||
|
|
||||||
|
<artifactId>extension-objects</artifactId>
|
||||||
|
<dependencies>
|
||||||
|
<dependency>
|
||||||
|
<groupId>junit</groupId>
|
||||||
|
<artifactId>junit</artifactId>
|
||||||
|
<scope>test</scope>
|
||||||
|
</dependency>
|
||||||
|
</dependencies>
|
||||||
|
|
||||||
|
</project>
|
84
extension-objects/src/main/java/App.java
Normal file
84
extension-objects/src/main/java/App.java
Normal file
@ -0,0 +1,84 @@
|
|||||||
|
/**
|
||||||
|
* 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.
|
||||||
|
*/
|
||||||
|
import abstractextensions.CommanderExtension;
|
||||||
|
import abstractextensions.SergeantExtension;
|
||||||
|
import abstractextensions.SoldierExtension;
|
||||||
|
import units.CommanderUnit;
|
||||||
|
import units.SergeantUnit;
|
||||||
|
import units.SoldierUnit;
|
||||||
|
import units.Unit;
|
||||||
|
import org.slf4j.Logger;
|
||||||
|
import org.slf4j.LoggerFactory;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Anticipate that an object’s interface needs to be extended in the future.
|
||||||
|
* Additional interfaces are defined by extension objects.
|
||||||
|
*/
|
||||||
|
public class App {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Program entry point
|
||||||
|
*
|
||||||
|
* @param args command line args
|
||||||
|
*/
|
||||||
|
public static void main(String[] args) {
|
||||||
|
|
||||||
|
//Create 3 different units
|
||||||
|
Unit soldierUnit = new SoldierUnit("SoldierUnit1");
|
||||||
|
Unit sergeantUnit = new SergeantUnit("SergeantUnit1");
|
||||||
|
Unit commanderUnit = new CommanderUnit("CommanderUnit1");
|
||||||
|
|
||||||
|
//check for each unit to have an extension
|
||||||
|
checkExtensionsForUnit(soldierUnit);
|
||||||
|
checkExtensionsForUnit(sergeantUnit);
|
||||||
|
checkExtensionsForUnit(commanderUnit);
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
private static void checkExtensionsForUnit(Unit unit) {
|
||||||
|
final Logger logger = LoggerFactory.getLogger(App.class);
|
||||||
|
|
||||||
|
SoldierExtension soldierExtension = (SoldierExtension) unit.getUnitExtension("SoldierExtension");
|
||||||
|
SergeantExtension sergeantExtension = (SergeantExtension) unit.getUnitExtension("SergeantExtension");
|
||||||
|
CommanderExtension commanderExtension = (CommanderExtension) unit.getUnitExtension("CommanderExtension");
|
||||||
|
|
||||||
|
//if unit have extension call the method
|
||||||
|
if (soldierExtension != null) {
|
||||||
|
soldierExtension.soldierReady();
|
||||||
|
} else {
|
||||||
|
logger.info(unit.getName() + " without SoldierExtension");
|
||||||
|
}
|
||||||
|
|
||||||
|
if (sergeantExtension != null) {
|
||||||
|
sergeantExtension.sergeantReady();
|
||||||
|
} else {
|
||||||
|
logger.info(unit.getName() + " without SergeantExtension");
|
||||||
|
}
|
||||||
|
|
||||||
|
if (commanderExtension != null) {
|
||||||
|
commanderExtension.commanderReady();
|
||||||
|
} else {
|
||||||
|
logger.info(unit.getName() + " without CommanderExtension");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,31 @@
|
|||||||
|
/**
|
||||||
|
* 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 abstractextensions;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Interface with their method
|
||||||
|
*/
|
||||||
|
public interface CommanderExtension extends UnitExtension {
|
||||||
|
|
||||||
|
void commanderReady();
|
||||||
|
}
|
@ -0,0 +1,31 @@
|
|||||||
|
/**
|
||||||
|
* 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 abstractextensions;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Interface with their method
|
||||||
|
*/
|
||||||
|
public interface SergeantExtension extends UnitExtension {
|
||||||
|
|
||||||
|
void sergeantReady();
|
||||||
|
}
|
@ -0,0 +1,30 @@
|
|||||||
|
/**
|
||||||
|
* 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 abstractextensions;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Interface with their method
|
||||||
|
*/
|
||||||
|
public interface SoldierExtension extends UnitExtension {
|
||||||
|
void soldierReady();
|
||||||
|
}
|
@ -0,0 +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 abstractextensions;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Other Extensions will extend this interface
|
||||||
|
*/
|
||||||
|
public interface UnitExtension {
|
||||||
|
}
|
@ -0,0 +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 concreteextensions;
|
||||||
|
|
||||||
|
import abstractextensions.CommanderExtension;
|
||||||
|
import org.slf4j.Logger;
|
||||||
|
import org.slf4j.LoggerFactory;
|
||||||
|
import units.CommanderUnit;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Class defining Commander
|
||||||
|
*/
|
||||||
|
public class Commander implements CommanderExtension {
|
||||||
|
|
||||||
|
private CommanderUnit unit;
|
||||||
|
|
||||||
|
public Commander(CommanderUnit commanderUnit) {
|
||||||
|
this.unit = commanderUnit;
|
||||||
|
}
|
||||||
|
|
||||||
|
final Logger logger = LoggerFactory.getLogger(Commander.class);
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void commanderReady() {
|
||||||
|
logger.info("[Commander] " + unit.getName() + " is ready!");
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +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 concreteextensions;
|
||||||
|
|
||||||
|
import abstractextensions.SergeantExtension;
|
||||||
|
import org.slf4j.Logger;
|
||||||
|
import org.slf4j.LoggerFactory;
|
||||||
|
import units.SergeantUnit;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Class defining Sergeant
|
||||||
|
*/
|
||||||
|
public class Sergeant implements SergeantExtension {
|
||||||
|
|
||||||
|
private SergeantUnit unit;
|
||||||
|
|
||||||
|
public Sergeant(SergeantUnit sergeantUnit) {
|
||||||
|
this.unit = sergeantUnit;
|
||||||
|
}
|
||||||
|
|
||||||
|
final Logger logger = LoggerFactory.getLogger(Sergeant.class);
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void sergeantReady() {
|
||||||
|
logger.info("[Sergeant] " + unit.getName() + " is ready! ");
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +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 concreteextensions;
|
||||||
|
|
||||||
|
import abstractextensions.SoldierExtension;
|
||||||
|
import org.slf4j.Logger;
|
||||||
|
import org.slf4j.LoggerFactory;
|
||||||
|
import units.SoldierUnit;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Class defining Soldier
|
||||||
|
*/
|
||||||
|
public class Soldier implements SoldierExtension {
|
||||||
|
|
||||||
|
private SoldierUnit unit;
|
||||||
|
|
||||||
|
public Soldier(SoldierUnit soldierUnit) {
|
||||||
|
this.unit = soldierUnit;
|
||||||
|
}
|
||||||
|
|
||||||
|
final Logger logger = LoggerFactory.getLogger(Soldier.class);
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void soldierReady() {
|
||||||
|
logger.info("[Solider] " + unit.getName() + " is ready!");
|
||||||
|
}
|
||||||
|
}
|
49
extension-objects/src/main/java/units/CommanderUnit.java
Normal file
49
extension-objects/src/main/java/units/CommanderUnit.java
Normal file
@ -0,0 +1,49 @@
|
|||||||
|
/**
|
||||||
|
* 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 units;
|
||||||
|
|
||||||
|
import abstractextensions.UnitExtension;
|
||||||
|
import concreteextensions.Commander;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Class defining CommanderUnit
|
||||||
|
*/
|
||||||
|
public class CommanderUnit extends Unit {
|
||||||
|
|
||||||
|
public CommanderUnit(String name) {
|
||||||
|
super(name);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public UnitExtension getUnitExtension(String extensionName) {
|
||||||
|
|
||||||
|
if (extensionName.equals("CommanderExtension")) {
|
||||||
|
if (unitExtension == null) {
|
||||||
|
unitExtension = new Commander(this);
|
||||||
|
}
|
||||||
|
return unitExtension;
|
||||||
|
}
|
||||||
|
|
||||||
|
return super.getUnitExtension(extensionName);
|
||||||
|
}
|
||||||
|
}
|
49
extension-objects/src/main/java/units/SergeantUnit.java
Normal file
49
extension-objects/src/main/java/units/SergeantUnit.java
Normal file
@ -0,0 +1,49 @@
|
|||||||
|
/**
|
||||||
|
* 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 units;
|
||||||
|
|
||||||
|
import abstractextensions.UnitExtension;
|
||||||
|
import concreteextensions.Sergeant;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Class defining SergeantUnit
|
||||||
|
*/
|
||||||
|
public class SergeantUnit extends Unit {
|
||||||
|
|
||||||
|
public SergeantUnit(String name) {
|
||||||
|
super(name);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public UnitExtension getUnitExtension(String extensionName) {
|
||||||
|
|
||||||
|
if (extensionName.equals("SergeantExtension")) {
|
||||||
|
if (unitExtension == null) {
|
||||||
|
unitExtension = new Sergeant(this);
|
||||||
|
}
|
||||||
|
return unitExtension;
|
||||||
|
}
|
||||||
|
|
||||||
|
return super.getUnitExtension(extensionName);
|
||||||
|
}
|
||||||
|
}
|
49
extension-objects/src/main/java/units/SoldierUnit.java
Normal file
49
extension-objects/src/main/java/units/SoldierUnit.java
Normal file
@ -0,0 +1,49 @@
|
|||||||
|
/**
|
||||||
|
* 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 units;
|
||||||
|
|
||||||
|
import abstractextensions.UnitExtension;
|
||||||
|
import concreteextensions.Soldier;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Class defining SoldierUnit
|
||||||
|
*/
|
||||||
|
public class SoldierUnit extends Unit {
|
||||||
|
|
||||||
|
public SoldierUnit(String name) {
|
||||||
|
super(name);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public UnitExtension getUnitExtension(String extensionName) {
|
||||||
|
|
||||||
|
if (extensionName.equals("SoldierExtension")) {
|
||||||
|
if (unitExtension == null) {
|
||||||
|
unitExtension = new Soldier(this);
|
||||||
|
}
|
||||||
|
|
||||||
|
return unitExtension;
|
||||||
|
}
|
||||||
|
return super.getUnitExtension(extensionName);
|
||||||
|
}
|
||||||
|
}
|
50
extension-objects/src/main/java/units/Unit.java
Normal file
50
extension-objects/src/main/java/units/Unit.java
Normal file
@ -0,0 +1,50 @@
|
|||||||
|
/**
|
||||||
|
* 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 units;
|
||||||
|
|
||||||
|
import abstractextensions.UnitExtension;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Class defining Unit, other units will extend this class
|
||||||
|
*/
|
||||||
|
public class Unit {
|
||||||
|
|
||||||
|
private String name;
|
||||||
|
protected UnitExtension unitExtension = null;
|
||||||
|
|
||||||
|
public Unit(String name) {
|
||||||
|
this.name = name;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getName() {
|
||||||
|
return name;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setName(String name) {
|
||||||
|
this.name = name;
|
||||||
|
}
|
||||||
|
|
||||||
|
public UnitExtension getUnitExtension(String extensionName) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
}
|
36
extension-objects/src/test/java/AppTest.java
Normal file
36
extension-objects/src/test/java/AppTest.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.
|
||||||
|
*/
|
||||||
|
import org.junit.Test;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Created by Srdjan on 03-May-17.
|
||||||
|
*/
|
||||||
|
public class AppTest {
|
||||||
|
@Test
|
||||||
|
public void main() throws Exception {
|
||||||
|
|
||||||
|
String[] args = {};
|
||||||
|
App.main(args);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -0,0 +1,39 @@
|
|||||||
|
/**
|
||||||
|
* 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 concreteextensions;
|
||||||
|
|
||||||
|
import org.junit.Test;
|
||||||
|
import units.CommanderUnit;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Created by Srdjan on 03-May-17.
|
||||||
|
*/
|
||||||
|
public class CommanderTest {
|
||||||
|
@Test
|
||||||
|
public void commanderReady() throws Exception {
|
||||||
|
final Commander commander = new Commander(new CommanderUnit("CommanderUnitTest"));
|
||||||
|
|
||||||
|
commander.commanderReady();
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -0,0 +1,39 @@
|
|||||||
|
/**
|
||||||
|
* 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 concreteextensions;
|
||||||
|
|
||||||
|
import org.junit.Test;
|
||||||
|
import units.SergeantUnit;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Created by Srdjan on 03-May-17.
|
||||||
|
*/
|
||||||
|
public class SergeantTest {
|
||||||
|
@Test
|
||||||
|
public void sergeantReady() throws Exception {
|
||||||
|
final Sergeant sergeant = new Sergeant(new SergeantUnit("SergeantUnitTest"));
|
||||||
|
|
||||||
|
sergeant.sergeantReady();
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -0,0 +1,39 @@
|
|||||||
|
/**
|
||||||
|
* 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 concreteextensions;
|
||||||
|
|
||||||
|
import org.junit.Test;
|
||||||
|
import units.SoldierUnit;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Created by Srdjan on 03-May-17.
|
||||||
|
*/
|
||||||
|
public class SoldierTest {
|
||||||
|
@Test
|
||||||
|
public void soldierReady() throws Exception {
|
||||||
|
final Soldier soldier = new Soldier(new SoldierUnit("SoldierUnitTest"));
|
||||||
|
|
||||||
|
soldier.soldierReady();
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
45
extension-objects/src/test/java/units/CommanderUnitTest.java
Normal file
45
extension-objects/src/test/java/units/CommanderUnitTest.java
Normal file
@ -0,0 +1,45 @@
|
|||||||
|
/**
|
||||||
|
* 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 units;
|
||||||
|
|
||||||
|
import abstractextensions.CommanderExtension;
|
||||||
|
import org.junit.Test;
|
||||||
|
|
||||||
|
import static org.junit.Assert.assertNotNull;
|
||||||
|
import static org.junit.Assert.assertNull;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Created by Srdjan on 03-May-17.
|
||||||
|
*/
|
||||||
|
public class CommanderUnitTest {
|
||||||
|
@Test
|
||||||
|
public void getUnitExtension() throws Exception {
|
||||||
|
|
||||||
|
final Unit unit = new CommanderUnit("CommanderUnitName");
|
||||||
|
|
||||||
|
assertNull(unit.getUnitExtension("SoldierExtension"));
|
||||||
|
assertNull(unit.getUnitExtension("SergeantExtension"));
|
||||||
|
assertNotNull((CommanderExtension) unit.getUnitExtension("CommanderExtension"));
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
45
extension-objects/src/test/java/units/SergeantUnitTest.java
Normal file
45
extension-objects/src/test/java/units/SergeantUnitTest.java
Normal file
@ -0,0 +1,45 @@
|
|||||||
|
/**
|
||||||
|
* 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 units;
|
||||||
|
|
||||||
|
import abstractextensions.SergeantExtension;
|
||||||
|
import org.junit.Test;
|
||||||
|
|
||||||
|
import static org.junit.Assert.assertNotNull;
|
||||||
|
import static org.junit.Assert.assertNull;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Created by Srdjan on 03-May-17.
|
||||||
|
*/
|
||||||
|
public class SergeantUnitTest {
|
||||||
|
@Test
|
||||||
|
public void getUnitExtension() throws Exception {
|
||||||
|
|
||||||
|
final Unit unit = new SergeantUnit("SergeantUnitName");
|
||||||
|
|
||||||
|
assertNull(unit.getUnitExtension("SoldierExtension"));
|
||||||
|
assertNotNull((SergeantExtension) unit.getUnitExtension("SergeantExtension"));
|
||||||
|
assertNull(unit.getUnitExtension("CommanderExtension"));
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
47
extension-objects/src/test/java/units/SoldierUnitTest.java
Normal file
47
extension-objects/src/test/java/units/SoldierUnitTest.java
Normal file
@ -0,0 +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 units;
|
||||||
|
|
||||||
|
import abstractextensions.SoldierExtension;
|
||||||
|
import org.junit.Test;
|
||||||
|
|
||||||
|
import static org.junit.Assert.assertNotNull;
|
||||||
|
import static org.junit.Assert.assertNull;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Created by Srdjan on 03-May-17.
|
||||||
|
*/
|
||||||
|
public class SoldierUnitTest {
|
||||||
|
@Test
|
||||||
|
public void getUnitExtension() throws Exception {
|
||||||
|
|
||||||
|
final Unit unit = new SoldierUnit("SoldierUnitName");
|
||||||
|
|
||||||
|
assertNotNull((SoldierExtension) unit.getUnitExtension("SoldierExtension"));
|
||||||
|
assertNull(unit.getUnitExtension("SergeantExtension"));
|
||||||
|
assertNull(unit.getUnitExtension("CommanderExtension"));
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
52
extension-objects/src/test/java/units/UnitTest.java
Normal file
52
extension-objects/src/test/java/units/UnitTest.java
Normal file
@ -0,0 +1,52 @@
|
|||||||
|
/**
|
||||||
|
* 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 units;
|
||||||
|
|
||||||
|
import org.junit.Test;
|
||||||
|
|
||||||
|
import static org.junit.Assert.assertEquals;
|
||||||
|
import static org.junit.Assert.assertNull;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Created by Srdjan on 03-May-17.
|
||||||
|
*/
|
||||||
|
public class UnitTest {
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testConstGetSet() throws Exception {
|
||||||
|
final String name = "testName";
|
||||||
|
final Unit unit = new Unit(name);
|
||||||
|
assertEquals(name, unit.getName());
|
||||||
|
|
||||||
|
final String newName = "newName";
|
||||||
|
unit.setName(newName);
|
||||||
|
assertEquals(newName, unit.getName());
|
||||||
|
|
||||||
|
|
||||||
|
assertNull(unit.getUnitExtension(""));
|
||||||
|
assertNull(unit.getUnitExtension("SoldierExtension"));
|
||||||
|
assertNull(unit.getUnitExtension("SergeantExtension"));
|
||||||
|
assertNull(unit.getUnitExtension("CommanderExtension"));
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -29,7 +29,7 @@
|
|||||||
<parent>
|
<parent>
|
||||||
<groupId>com.iluwatar</groupId>
|
<groupId>com.iluwatar</groupId>
|
||||||
<artifactId>java-design-patterns</artifactId>
|
<artifactId>java-design-patterns</artifactId>
|
||||||
<version>1.15.0</version>
|
<version>1.16.0</version>
|
||||||
</parent>
|
</parent>
|
||||||
<artifactId>facade</artifactId>
|
<artifactId>facade</artifactId>
|
||||||
<dependencies>
|
<dependencies>
|
||||||
|
@ -30,7 +30,7 @@
|
|||||||
<parent>
|
<parent>
|
||||||
<groupId>com.iluwatar</groupId>
|
<groupId>com.iluwatar</groupId>
|
||||||
<artifactId>java-design-patterns</artifactId>
|
<artifactId>java-design-patterns</artifactId>
|
||||||
<version>1.15.0</version>
|
<version>1.16.0</version>
|
||||||
</parent>
|
</parent>
|
||||||
<artifactId>factory-kit</artifactId>
|
<artifactId>factory-kit</artifactId>
|
||||||
<dependencies>
|
<dependencies>
|
||||||
|
@ -29,7 +29,7 @@
|
|||||||
<parent>
|
<parent>
|
||||||
<groupId>com.iluwatar</groupId>
|
<groupId>com.iluwatar</groupId>
|
||||||
<artifactId>java-design-patterns</artifactId>
|
<artifactId>java-design-patterns</artifactId>
|
||||||
<version>1.15.0</version>
|
<version>1.16.0</version>
|
||||||
</parent>
|
</parent>
|
||||||
<artifactId>factory-method</artifactId>
|
<artifactId>factory-method</artifactId>
|
||||||
<dependencies>
|
<dependencies>
|
||||||
|
@ -30,7 +30,7 @@
|
|||||||
<parent>
|
<parent>
|
||||||
<artifactId>java-design-patterns</artifactId>
|
<artifactId>java-design-patterns</artifactId>
|
||||||
<groupId>com.iluwatar</groupId>
|
<groupId>com.iluwatar</groupId>
|
||||||
<version>1.15.0</version>
|
<version>1.16.0</version>
|
||||||
</parent>
|
</parent>
|
||||||
<modelVersion>4.0.0</modelVersion>
|
<modelVersion>4.0.0</modelVersion>
|
||||||
|
|
||||||
|
@ -25,6 +25,7 @@ package com.iluwatar.featuretoggle;
|
|||||||
|
|
||||||
import com.iluwatar.featuretoggle.pattern.Service;
|
import com.iluwatar.featuretoggle.pattern.Service;
|
||||||
import com.iluwatar.featuretoggle.pattern.propertiesversion.PropertiesFeatureToggleVersion;
|
import com.iluwatar.featuretoggle.pattern.propertiesversion.PropertiesFeatureToggleVersion;
|
||||||
|
import com.iluwatar.featuretoggle.pattern.tieredversion.TieredFeatureToggleVersion;
|
||||||
import com.iluwatar.featuretoggle.user.User;
|
import com.iluwatar.featuretoggle.user.User;
|
||||||
import com.iluwatar.featuretoggle.user.UserGroup;
|
import com.iluwatar.featuretoggle.user.UserGroup;
|
||||||
import org.slf4j.Logger;
|
import org.slf4j.Logger;
|
||||||
@ -85,6 +86,8 @@ public class App {
|
|||||||
LOGGER.info(welcomeMessageturnedOff);
|
LOGGER.info(welcomeMessageturnedOff);
|
||||||
|
|
||||||
// --------------------------------------------
|
// --------------------------------------------
|
||||||
|
|
||||||
|
Service service2 = new TieredFeatureToggleVersion();
|
||||||
|
|
||||||
final User paidUser = new User("Jamie Coder");
|
final User paidUser = new User("Jamie Coder");
|
||||||
final User freeUser = new User("Alan Defect");
|
final User freeUser = new User("Alan Defect");
|
||||||
@ -92,8 +95,8 @@ public class App {
|
|||||||
UserGroup.addUserToPaidGroup(paidUser);
|
UserGroup.addUserToPaidGroup(paidUser);
|
||||||
UserGroup.addUserToFreeGroup(freeUser);
|
UserGroup.addUserToFreeGroup(freeUser);
|
||||||
|
|
||||||
final String welcomeMessagePaidUser = service.getWelcomeMessage(paidUser);
|
final String welcomeMessagePaidUser = service2.getWelcomeMessage(paidUser);
|
||||||
final String welcomeMessageFreeUser = service.getWelcomeMessage(freeUser);
|
final String welcomeMessageFreeUser = service2.getWelcomeMessage(freeUser);
|
||||||
LOGGER.info(welcomeMessageFreeUser);
|
LOGGER.info(welcomeMessageFreeUser);
|
||||||
LOGGER.info(welcomeMessagePaidUser);
|
LOGGER.info(welcomeMessagePaidUser);
|
||||||
}
|
}
|
||||||
|
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user