Skip to content

Commit 3fd6887

Browse files
committed
iluwatar#467 data-bus: implement pattern
1 parent eecffb0 commit 3fd6887

12 files changed

Lines changed: 555 additions & 1 deletion

File tree

data-bus/README.md

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
--- # this is so called 'Yaml Front Matter', read up on it here: http://jekyllrb.com/docs/frontmatter/
2+
layout: pattern # layout must allways be pattern
3+
title: Data Bus # the properly formatted title
4+
folder: data-bus # the folder name in which this pattern lies
5+
permalink: /patterns/data-bus/ # the permalink to the pattern, to keep this uniform please stick to /patterns/FOLDER/
6+
7+
# both categories and tags are Yaml Lists
8+
# you can either just pick one or write a list with '-'s
9+
# usable categories and tags are listed here: https://github.com/iluwatar/java-design-patterns/blob/gh-pages/_config.yml
10+
categories: creational # categories of the pattern
11+
tags: # tags of the pattern
12+
- best
13+
- ever
14+
- awesome
15+
---
16+
17+
## Intent
18+
Makes your code awesome
19+
20+
![alt text](./etc/best_pattern.png "Best Pattern Ever")
21+
22+
## Applicability
23+
Use the Best Pattern Ever pattern when
24+
25+
* you want to be the best
26+
* you need to ...
27+
28+
## Real world examples
29+
30+
* [Nowhere](http://no.where.com)
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
/*
2+
The MIT License (MIT)
3+
4+
Copyright (c) 2016 Paul Campbell
5+
6+
Permission is hereby granted, free of charge, to any person obtaining a copy
7+
of this software and associated documentation files (the "Software"), to deal
8+
in the Software without restriction, including without limitation the rights
9+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10+
copies of the Software, and to permit persons to whom the Software is
11+
furnished to do so, subject to the following conditions:
12+
13+
The above copyright notice and this permission notice shall be included in all
14+
copies or substantial portions of the Software.
15+
16+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22+
SOFTWARE.
23+
*/
24+
25+
package com.iluwatar.databus;
26+
27+
/**
28+
* .
29+
*
30+
* @author Paul Campbell (pcampbell@kemitix.net)
31+
*/
32+
public class AbstractDataType implements DataType {
33+
34+
private DataBus dataBus;
35+
36+
@Override
37+
public DataBus getDataBus() {
38+
return dataBus;
39+
}
40+
41+
@Override
42+
public void setDataBus(DataBus dataBus) {
43+
this.dataBus = dataBus;
44+
}
45+
}
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
/**
2+
* The MIT License
3+
* Copyright (c) 2014-2016 Ilkka Seppälä
4+
* <p>
5+
* Permission is hereby granted, free of charge, to any person obtaining a copy
6+
* of this software and associated documentation files (the "Software"), to deal
7+
* in the Software without restriction, including without limitation the rights
8+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
* copies of the Software, and to permit persons to whom the Software is
10+
* furnished to do so, subject to the following conditions:
11+
* <p>
12+
* The above copyright notice and this permission notice shall be included in
13+
* all copies or substantial portions of the Software.
14+
* <p>
15+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21+
* THE SOFTWARE.
22+
*/
23+
24+
package com.iluwatar.databus;
25+
26+
import com.iluwatar.databus.data.StoppingData;
27+
import com.iluwatar.databus.data.StartingData;
28+
import com.iluwatar.databus.data.MessageData;
29+
import com.iluwatar.databus.members.CounterMember;
30+
import com.iluwatar.databus.members.StatusMember;
31+
import lombok.extern.slf4j.Slf4j;
32+
33+
import java.time.LocalDateTime;
34+
35+
/**
36+
* The Data Bus pattern
37+
* <p>
38+
* <p>{@see http://wiki.c2.com/?DataBusPattern}</p>
39+
*
40+
* @author Paul Campbell (pcampbell@kemitix.net)
41+
*/
42+
@Slf4j
43+
class App {
44+
45+
public static void main(String[] args) {
46+
final DataBus bus = DataBus.getInstance();
47+
bus.subscribe(new StatusMember(1));
48+
bus.subscribe(new StatusMember(2));
49+
final CounterMember foo = new CounterMember("Foo");
50+
final CounterMember bar = new CounterMember("Bar");
51+
bus.subscribe(foo);
52+
bus.publish(StartingData.of(LocalDateTime.now()));
53+
bus.publish(MessageData.of("Only Foo should see this"));
54+
bus.subscribe(bar);
55+
bus.publish(MessageData.of("Foo and Bar should see this"));
56+
bus.unsubscribe(foo);
57+
bus.publish(MessageData.of("Only Bar should see this"));
58+
bus.publish(StoppingData.of(LocalDateTime.now()));
59+
}
60+
}
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
/**
2+
* The MIT License
3+
* Copyright (c) 2014-2016 Ilkka Seppälä
4+
* <p>
5+
* Permission is hereby granted, free of charge, to any person obtaining a copy
6+
* of this software and associated documentation files (the "Software"), to deal
7+
* in the Software without restriction, including without limitation the rights
8+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
* copies of the Software, and to permit persons to whom the Software is
10+
* furnished to do so, subject to the following conditions:
11+
* <p>
12+
* The above copyright notice and this permission notice shall be included in
13+
* all copies or substantial portions of the Software.
14+
* <p>
15+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21+
* THE SOFTWARE.
22+
*/
23+
24+
package com.iluwatar.databus;
25+
26+
import java.util.HashSet;
27+
import java.util.Set;
28+
29+
/**
30+
* The Data-Bus implementation.
31+
*
32+
* <p>This implementation uses a Singleton.</p>
33+
*
34+
* @author Paul Campbell (pcampbell@kemitix.net)
35+
*/
36+
public class DataBus {
37+
38+
private static final DataBus INSTANCE = new DataBus();
39+
40+
private final Set<Member> listeners = new HashSet<>();
41+
42+
public static DataBus getInstance() {
43+
return INSTANCE;
44+
}
45+
46+
/**
47+
* Register a member with the data-bus to start receiving events.
48+
*
49+
* @param member The member to register
50+
*/
51+
public void subscribe(final Member member) {
52+
this.listeners.add(member);
53+
}
54+
55+
/**
56+
* Deregister a member to stop receiving events.
57+
*
58+
* @param member The member to deregister
59+
*/
60+
public void unsubscribe(final Member member) {
61+
this.listeners.remove(member);
62+
}
63+
64+
/**
65+
* Publish and event to all members.
66+
*
67+
* @param event The event
68+
*/
69+
public void publish(final DataType event) {
70+
event.setDataBus(this);
71+
listeners.forEach(listener -> listener.accept(event));
72+
}
73+
}
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
/*
2+
The MIT License (MIT)
3+
4+
Copyright (c) 2016 Paul Campbell
5+
6+
Permission is hereby granted, free of charge, to any person obtaining a copy
7+
of this software and associated documentation files (the "Software"), to deal
8+
in the Software without restriction, including without limitation the rights
9+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10+
copies of the Software, and to permit persons to whom the Software is
11+
furnished to do so, subject to the following conditions:
12+
13+
The above copyright notice and this permission notice shall be included in all
14+
copies or substantial portions of the Software.
15+
16+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22+
SOFTWARE.
23+
*/
24+
25+
package com.iluwatar.databus;
26+
27+
/**
28+
* Events are sent via the Data-Bus.
29+
*
30+
* @author Paul Campbell (pcampbell@kemitix.net)
31+
*/
32+
33+
public interface DataType {
34+
35+
/**
36+
* Returns the data-bus the event is being sent on.
37+
*
38+
* @return The data-bus
39+
*/
40+
DataBus getDataBus();
41+
42+
/**
43+
* Set the data-bus the event will be sent on.
44+
*
45+
* @param dataBus The data-bus
46+
*/
47+
void setDataBus(DataBus dataBus);
48+
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
/*
2+
The MIT License (MIT)
3+
4+
Copyright (c) 2016 Paul Campbell
5+
6+
Permission is hereby granted, free of charge, to any person obtaining a copy
7+
of this software and associated documentation files (the "Software"), to deal
8+
in the Software without restriction, including without limitation the rights
9+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10+
copies of the Software, and to permit persons to whom the Software is
11+
furnished to do so, subject to the following conditions:
12+
13+
The above copyright notice and this permission notice shall be included in all
14+
copies or substantial portions of the Software.
15+
16+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22+
SOFTWARE.
23+
*/
24+
25+
package com.iluwatar.databus;
26+
27+
import java.util.function.Consumer;
28+
29+
/**
30+
* Members receive events from the Data-Bus.
31+
*
32+
* @author Paul Campbell (pcampbell@kemitix.net)
33+
*/
34+
public interface Member extends Consumer<DataType> {
35+
36+
void accept(DataType event);
37+
}
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
/**
2+
* The MIT License
3+
* Copyright (c) 2014-2016 Ilkka Seppälä
4+
* <p>
5+
* Permission is hereby granted, free of charge, to any person obtaining a copy
6+
* of this software and associated documentation files (the "Software"), to deal
7+
* in the Software without restriction, including without limitation the rights
8+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
* copies of the Software, and to permit persons to whom the Software is
10+
* furnished to do so, subject to the following conditions:
11+
* <p>
12+
* The above copyright notice and this permission notice shall be included in
13+
* all copies or substantial portions of the Software.
14+
* <p>
15+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21+
* THE SOFTWARE.
22+
*/
23+
24+
package com.iluwatar.databus.data;
25+
26+
import com.iluwatar.databus.AbstractDataType;
27+
import com.iluwatar.databus.DataType;
28+
import lombok.RequiredArgsConstructor;
29+
30+
/**
31+
* .
32+
*
33+
* @author Paul Campbell (pcampbell@kemitix.net)
34+
*/
35+
@RequiredArgsConstructor
36+
public class MessageData extends AbstractDataType {
37+
38+
private final String message;
39+
40+
public String getMessage() {
41+
return message;
42+
}
43+
44+
public static DataType of(final String message) {
45+
return new MessageData(message);
46+
}
47+
}
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
/**
2+
* The MIT License
3+
* Copyright (c) 2014-2016 Ilkka Seppälä
4+
* <p>
5+
* Permission is hereby granted, free of charge, to any person obtaining a copy
6+
* of this software and associated documentation files (the "Software"), to deal
7+
* in the Software without restriction, including without limitation the rights
8+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
* copies of the Software, and to permit persons to whom the Software is
10+
* furnished to do so, subject to the following conditions:
11+
* <p>
12+
* The above copyright notice and this permission notice shall be included in
13+
* all copies or substantial portions of the Software.
14+
* <p>
15+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21+
* THE SOFTWARE.
22+
*/
23+
24+
package com.iluwatar.databus.data;
25+
26+
import com.iluwatar.databus.AbstractDataType;
27+
import com.iluwatar.databus.DataType;
28+
import lombok.RequiredArgsConstructor;
29+
30+
import java.time.LocalDateTime;
31+
32+
/**
33+
* An event raised when applications starts, containing the start time of the application.
34+
*
35+
* @author Paul Campbell (pcampbell@kemitix.net)
36+
*/
37+
@RequiredArgsConstructor
38+
public class StartingData extends AbstractDataType {
39+
40+
private final LocalDateTime when;
41+
42+
public LocalDateTime getWhen() {
43+
return when;
44+
}
45+
46+
public static DataType of(final LocalDateTime when) {
47+
return new StartingData(when);
48+
}
49+
}

0 commit comments

Comments
 (0)