问题
As already mentioned in this SO answer and the other posts in the same question, C# delegates can be implemented using interfaces or Java FuncationInterfaces.
However I am looking to implement a proper event model and not a delegate model in Java. For a brief on the difference of the two, please see this. Especially the first comment.
Below is what I have tried so far:
Event.java
public class Event {
public interface EventHandler{
void invoke();
}
private Set<EventHandler> mEventHandlers = new HashSet<>();
public void add(EventHandler eventHandler){
mEventHandlers.add(eventHandler);
}
public void remove(EventHandler eventHandler){
mEventHandlers.remove(eventHandler);
}
public void invoke(){
for(EventHandler eventHandler : mEventHandlers){
if(eventHandler!=null) {
eventHandler.invoke();
}
}
}
}
EventPubisher.java
public class EventPublisher {
public Event ValueUpdatedEvent;
public void UpdateValue(){
ValueUpdatedEvent.invoke();
}
}
EventConsumer.java
public class EventConsumer {
EventPublisher ep = new EventPublisher();
public EventConsumer(){
ep.ValueUpdatedEvent.add(this::ValueUpdatedEventHandler);
}
private void ValueUpdatedEventHandler(){
// do stuff
}
}
The problem with this design is that I can write code like below as well:
public class EventConsumer {
.....
private void abuse(){
ep.ValueUpdatedEvent.invoke();
}
}
And this is particularly what events restrict. The event should be raised only from the declaring class and not from outside.
回答1:
If you wanted to avoid the add/remove methods on your publisher, could you use the code below? It uses two classes (Event and EventImpl) to separate the add/remove and the invoke, so invoke can be made private to the publisher. It is generic so different listener interfaces can be used.
This code saves a lot of duplicated boilerplate, but do you think it would it be considered idiomatic @JonSkeet?
Here are the event classes:
class Event<TListener> {
private final Set<TListener> listeners;
Event(Set<TListener> listeners) {
this.listeners = listeners;
}
public void add(TListener listener) {
listeners.add(listener);
}
public void remove(TListener listener) {
listeners.remove(listener);
}
}
class EventImpl<TListener> {
private Set<TListener> listeners = new HashSet<>();
private Event<TListener> event = new Event<>(listeners);
Event<TListener> getEvent() {
return event;
}
interface Invoker<TListener> {
void invoke(TListener listener);
}
public void invoke(Invoker<TListener> invoker) {
for (TListener listener : listeners){
if (listener != null) {
invoker.invoke(listener);
}
}
}
}
And here is an example of a publisher and subscriber with some test code to exercise them:
class MyEventPublisher {
interface Listener {
void listenToThis(int intArg, String stringArg, Object... etc);
}
private EventImpl<Listener> eventImpl = new EventImpl<>();
Event<Listener> getEvent() {
return eventImpl.getEvent();
}
void somethingCausingAnEvent() {
eventImpl.invoke(
listener -> listener.listenToThis(1, "blah", 10,11, 12));
}
}
class MyEventSubscriber {
private String eventRecord = "";
MyEventSubscriber(MyEventPublisher publisher) {
publisher.getEvent().add(
(intArg, stringArg, etc) -> eventRecord += intArg + stringArg + Arrays.toString(etc));
}
String getEventRecord() {
return eventRecord;
}
}
public class TestEvents {
@Test
public void testEvent() {
MyEventPublisher p = new MyEventPublisher();
MyEventSubscriber s = new MyEventSubscriber(p);
p.somethingCausingAnEvent();
assertEquals("1blah[10, 11, 12]", s.getEventRecord());
}
}
回答2:
As mentioned by @Jon Skeet in the comments, changing the code as below meets my requirement:
EventPubisher.java
public class EventPublisher {
private final Event ValueUpdatedEvent = new Event();
public void addEventHandler(Event.EventHandler eventHandler){
ValueUpdatedEvent.add(eventHandler);
}
public void removeEventHandler(Event.EventHandler eventHandler){
ValueUpdatedEvent.remove(eventHandler);
}
public void UpdateValue(){
ValueUpdatedEvent.invoke();
}
}
EventConsumer.java
public class EventConsumer {
.....
private void abuse(){
// ep.ValueUpdatedEvent.invoke(); //Compilation error
}
}
来源:https://stackoverflow.com/questions/56567562/implementing-a-proper-c-sharp-event-not-delegate-in-java