-
-
Notifications
You must be signed in to change notification settings - Fork 27.3k
[WIP] Data Bus #544
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
[WIP] Data Bus #544
Changes from all commits
Commits
Show all changes
18 commits
Select commit
Hold shift + click to select a range
eecffb0
#467 data-bus: add stub
kemitix 3fd6887
#467 data-bus: implement pattern
kemitix b5bdf2d
#467 data-bus: etc: add urm diagrams
kemitix 6b795e5
#467 data-bus: README.md: updated for data-bus
kemitix 30315e7
Merge remote-tracking branch 'upstream/master' into data-bus
kemitix 960eee3
#467 update version
kemitix bc4d029
#467 data-bus: pom.xml: remove surefire plugin
kemitix 146f367
#467 data-bus: remove lombok
kemitix 86009f2
#467 data-bus: add missing javadoc
kemitix 46e0fa4
#467 data-bus: pom.xml: add mockito dependency
kemitix b7a6a01
#467 data-bus: DataBusTest: added
kemitix 8b0c14c
Counter doesn't count anything. Added ability to collect the messages
kemitix f495a88
#467 data-bus: members: MessageCollectorMemberTest: added
kemitix 311bb79
#467 data-bus: members: StatusMember: records start and stop times
kemitix b72d545
#467 data-bus: members: StatusMemberTest: added
kemitix c96ebcb
#467 data-bus: App: add description of the pattern
kemitix 2643dfa
#467 data-bus: App: add notes about this implementation of the patter
kemitix ff8d854
#467 data-bus: README.md: clean up
kemitix File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
/target |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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-SNAPSHOT</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> |
45 changes: 45 additions & 0 deletions
45
data-bus/src/main/java/com/iluwatar/databus/AbstractDataType.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
/* | ||
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; | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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> | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please describe the pattern here. Also add description how the example implements the pattern. |
||
* <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())); | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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)); | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Remove this line since lombok is not used