Java的。正确的模式来实现听众 [英] Java. Correct pattern for implementing listeners
问题描述
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 TigerListener
s. Now, TigerListener
s and ElephantListener
s 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屋!