|
| 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.MessageData; |
| 27 | +import com.iluwatar.databus.data.StartingData; |
| 28 | +import com.iluwatar.databus.data.StoppingData; |
| 29 | +import com.iluwatar.databus.members.MessageCollectorMember; |
| 30 | +import com.iluwatar.databus.members.StatusMember; |
| 31 | + |
| 32 | +import java.time.LocalDateTime; |
| 33 | + |
| 34 | +/** |
| 35 | + * The Data Bus pattern |
| 36 | + * <p> |
| 37 | + * <p>{@see http://wiki.c2.com/?DataBusPattern}</p> |
| 38 | + * <p> |
| 39 | + * <p>The Data-Bus pattern provides a method where different parts of an application may |
| 40 | + * pass messages between each other without needing to be aware of the other's existence.</p> |
| 41 | + * <p>Similar to the {@code ObserverPattern}, members register themselves with the {@link DataBus} |
| 42 | + * and may then receive each piece of data that is published to the Data-Bus. The member |
| 43 | + * may react to any given message or not.</p> |
| 44 | + * <p>It allows for Many-to-Many distribution of data, as there may be any number of |
| 45 | + * publishers to a Data-Bus, and any number of members receiving the data. All members |
| 46 | + * will receive the same data, the order each receives a given piece of data, is an |
| 47 | + * implementation detail.</p> |
| 48 | + * <p>Members may unsubscribe from the Data-Bus to stop receiving data.</p> |
| 49 | + * <p>This example of the pattern implements a Synchronous Data-Bus, meaning that |
| 50 | + * when data is published to the Data-Bus, the publish method will not return until |
| 51 | + * all members have received the data and returned.</p> |
| 52 | + * <p>The {@link DataBus} class is a Singleton.</p> |
| 53 | + * <p>Members of the Data-Bus must implement the {@link Member} interface.</p> |
| 54 | + * <p>Data to be published via the Data-Bus must implement the {@link DataType} interface.</p> |
| 55 | + * <p>The {@code data} package contains example {@link DataType} implementations.</p> |
| 56 | + * <p>The {@code members} package contains example {@link Member} implementations.</p> |
| 57 | + * <p>The {@link StatusMember} demonstrates using the DataBus to publish a message |
| 58 | + * to the Data-Bus when it receives a message.</p> |
| 59 | + * |
| 60 | + * @author Paul Campbell (pcampbell@kemitix.net) |
| 61 | + */ |
| 62 | +class App { |
| 63 | + |
| 64 | + public static void main(String[] args) { |
| 65 | + final DataBus bus = DataBus.getInstance(); |
| 66 | + bus.subscribe(new StatusMember(1)); |
| 67 | + bus.subscribe(new StatusMember(2)); |
| 68 | + final MessageCollectorMember foo = new MessageCollectorMember("Foo"); |
| 69 | + final MessageCollectorMember bar = new MessageCollectorMember("Bar"); |
| 70 | + bus.subscribe(foo); |
| 71 | + bus.publish(StartingData.of(LocalDateTime.now())); |
| 72 | + bus.publish(MessageData.of("Only Foo should see this")); |
| 73 | + bus.subscribe(bar); |
| 74 | + bus.publish(MessageData.of("Foo and Bar should see this")); |
| 75 | + bus.unsubscribe(foo); |
| 76 | + bus.publish(MessageData.of("Only Bar should see this")); |
| 77 | + bus.publish(StoppingData.of(LocalDateTime.now())); |
| 78 | + } |
| 79 | +} |
0 commit comments