Java – multiple enumerations and one enum
When I stumbled upon something I didn't understand why, I was looking at the example implementation of publisher (asynciterablepublisher. Java) of the reaction flow specification
static interface Signal {}; enum Cancel implements Signal { Instance; }; enum Subscribe implements Signal { Instance; }; enum Send implements Signal { Instance; };
To tell you the truth, I'm not such a senior programmer, but the person who wrote this. I'm sure I have reason to do so But I can't explain why it's better than that (that's what I'll do)
enum Signal { Cancel,Subscribe,Send; }
Can someone explain to me why it's better? Advantages and disadvantages?
Solution
Don't be too strict. Here is my explanation of this code Let's call it Roland's owner of the reaction stream
Initially, Roland needed a common interface for all inbound signals
static interface Signal {}; ConcurrentLinkedQueue<Signal> inboundSignals = new ConcurrentLinkedQueue<Signal>();
Signals such as cancel, subscribe and send always have the same purpose, are immutable and often occur, so it is a good idea to implement them as Joshua Bloch's singleton:
enum Cancel implements Signal { Instance; }; enum Subscribe implements Signal { Instance; }; enum Send implements Signal { Instance; };
Another way is to do the same advice and my favorite:
enum CommonSignals implements Signal{ Cancel { @Override void debug() { System.out.println("Cancel"); } },Subscribe { @Override void debug() { System.out.println("Subscribe"); } },Send { @Override void debug() { System.out.println("Send"); } }; abstract void debug(); [...] some other methods I Could need in the future }
As you can see, this is a different implementation But the idea is the same - signal singleness
Let's move on and find this Code:
static final class Request implements Signal { final long n; Request(final long n) { // every Request has different value of n this.n = n; } };
Because inboundsignals can contain multiple request objects, this type of signal cannot be implemented as singleton Therefore, it cannot be a member of commonsignals or implemented as an enumeration
conclusion
Roland uses many possibilities to achieve singles I think it's more like a taste of how to do it