Java的。正确的模式来实现听众 [英] Java. Correct pattern for implementing listeners

查看:133
本文介绍了Java的。正确的模式来实现听众的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

通常情况下,我有一个情况,给定的对象将需要有许多监听器。例如,我可能有

  class Elephant {
public void addListener(ElephantListener listener){...}
}

但我会有很多这样的情况。也就是说,我还将有一个 Tiger 对象,它们将具有 TigerListener s。现在, TigerListener s和 ElephantListener 是完全不同的:

  interface TigerListener {
void listenForGrowl(Growl qrowl);
void listenForMeow(Meow meow);
}

  interface ElephantListener {
void listenForStomp(String location,double intensity);
}

我发现我总是不得不在每个动物类,并且执行总是一样的。有没有一个首选的模式?

解决方案

而不是每个监听器您可以发送每种事件类型的方法,更改界面以接受一个通用的事件类。如果需要,您可以将 Event 子类化到特定子类型,或者包含诸如 double intensity 的状态。



TigerListener和ElephentListener然后变为

 界面TigerListener {
void听(事件);
}

事实上,你可以进一步将这个界面重构为一个简单的监听器

  interface Listener {
void listen(Event event);
}

您的监听器实现可以包含他们关心的特定事件需要的逻辑

  class TigerListener实现Listener {
@Overrides
void listen(Event event){
if(event instanceof GrowlEvent){
// handle growl ...
}
else if(MeowEvent的事件实例) {
// handle meow
}
//我们不关心任何其他类型的事件
}
}

类ElephentListener {
@Overrides
void listen(Event event){
if(event instanceof StompEvent){
StompEvent stomp =(StompEvent)event;
if(north.equals(stomp.getLocation())&& stomp.getDistance()> 10){
...
}
}
}
}

订阅者和发布商之间的关键关系是发布者可以向订阅者发送事件,它不一定能够向其发送某些类型的事件 - 这种类型的重构将该逻辑从界面推送到具体实现中。


Very typically I have a situation where a given object will need to have many listeners. For instance, I might have

class Elephant {
  public void addListener( ElephantListener listener ) { ... }
}

but I'll have many such situations. That is, I'll also have a Tiger object that'll have TigerListeners. Now, TigerListeners and ElephantListeners are quite different:

interface TigerListener {
  void listenForGrowl( Growl qrowl );
  void listenForMeow( Meow meow );
}

while

interface ElephantListener {
  void listenForStomp( String location, double intensity );
}

I find that I always have to keep re-implementing the broadcasting mechanism in each animal class, and the implementation is always the same. Is there a preferred pattern?

解决方案

Instead of each Listener having specific methods for every event type you can send it, change the interface to accept a generic Event class. You can then subclass Event to specific subtypes if you need, or have it contain state such as double intensity.

TigerListener and ElephentListener then become

interface TigerListener {
    void listen(Event event);
}

In fact, you can then further refactor this interface into a plain Listener:

interface Listener {
    void listen(Event event);
}

Your Listener implementations can then contain the logic that they need for the specific events they care about

class TigerListener implements Listener {
    @Overrides
    void listen(Event event) {
        if (event instanceof GrowlEvent) {
            //handle growl...
        }
        else if (event instance of MeowEvent) {
            //handle meow
        }
        //we don't care about any other types of Events
    }
}

class ElephentListener {
    @Overrides
    void listen(Event event) {
        if (event instanceof StompEvent) {
            StompEvent stomp = (StompEvent) event;
            if ("north".equals(stomp.getLocation()) && stomp.getDistance() > 10) { 
                ... 
            }
        }
    }
}

The key relationship between the subscriber and the publisher is that the publisher can send events to the subscribers, it isn't necessarily that it can send it certain types of events - this type of refactoring pushes that logic from the interface down into the specific implementations.

这篇关于Java的。正确的模式来实现听众的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆