2008-11-09 63 views
0

使用传统的侦听器回调模型。我有几个听众收集各种东西。每个侦听器收集的内容都在内部结构的侦听器中。正确耦合需要访问共享状态数据的多个侦听器

的问题是,我想一些听众要注意的一些在其他听众的“东西”。

我执行侦听器注册顺序,所以如果我明知注册以某种顺序事件后听众可以肯定的是先前听众更新了东西,并以某种方式访问​​它做更多的东西。

我在第一次尝试让每个听者存储其所依赖听众的参考。因此,我按照没有依赖关系的顺序将侦听器注册到那些具有预先注册依赖关系的侦听器中,然后用各种方法在侦听器之间设置引用。

我开始意识到多么糟糕这感觉,我想知道,如果不知何故以前一直沿着这条道路。当其中一位听众需要访问另一位听众时,什么会是更合适的模式?

下面是一些伪代码来说明:

interface Listener { onEvent(char e); } 

class A implements Listener { 
    private int count; 
    public void onEvent(char e) { if(e == 'a') count++; } 
    public int getCount() { return count; } 
} 

class B implements Listener { 
private int count; 
// private A a; 
// private void setA(A a) { this.a = a; } 

public void onEvent(char e) { if(e == 'b') count++; } 
public int getCount() { return count; } 
public int getAPlusBCount() { 
    // We know B count, but we don't know A so how would we change this 
    // so B is A aware? Or not even aware, just somehow coupled? This 
    // is the question 
    // return a.getCount() + count; 
} 

public void doConditionalHere() { 
    // Do some condition in B that relies on the state of data in A 
    int acount = 0; // a.getCount(); ??? 
    if(acount % 2 == 0) { 
    this.count--; 
    } 
} 
} 

class Run { 
A a = new A(); 
B b = new B(); 
List listeners = new List(); 
listeners.add(a); 
listeners.add(b); 

// The ugly way I add coupling right now is to keep a reference to A 
// inside B. It's commented out because I am hoping there is a more intelligent approach 
// b.setA(a); 

for(char c : "ababbabab") { 
    for(listener : listeners) { 
    listener.onEvent(c); 
    } 
} 
} 
+0

“我要继续前进,并接受你的答案,因为它的工作原理,但我希望为一些其他的方式。”这很愚蠢。为什么接受它?为什么不等待更好的答案? – 2008-11-10 02:53:56

+0

好的,我会等的。 – Josh 2008-11-10 14:36:06

回答

1

为什么不能有一个中央对象,将跟踪了多少次的onEvent方法被解雇了所有的监听类

public interface CountObserver { 

public void updateCount(String className); 
public int getCount(String className); 
} 

public class CentralObserver implements CountObserver { 

private int aCount; 
private int bCount; 

public void updateCount(String className) { 

//There's probably a better way to do this than using 
//all these if-elses, but you'll get the idea. 

    if (className.equals("AclassName")) { 
    aCount++; 
    } 
    else if (className.equals("BclassName")) { 
    bCount++; 
    } 
} 

public int getCount(String className) { 

    if (className.equals("AclassName")) { 
    return aCount; 
    } 
    else if (className.equals("BclassName")) { 
    return bCount; 
    } 
} 

class A implements Listener { 

CountObserver countObserver; 

public void registerObserver (CountObserver countObserver) { 

    this.countObserver = countObserver; 
} 

public void onEvent(char e) { 

    if(e == 'a') { 

    countObserver.updateCount (this.getClass.getName); 
    } 
} 

} 

//Same thing for B or any other class implementing Listener. Your Listener interface should, of 

//course, have a method signature for the registerObserver method which all the listener classes 

//will implement. 

class Run { 

private A a; 
private B b; 
private CountObserver centralObserver; 

public runProgram() { 

    centralObserver = new CentralObserver(); 
    a.registerObserver(centralObserver); 
    b.registerObserver(centralObserver); 

//run OnEvent method for A a couple of times, then for B 

} 

public int getAcount() { 

return centralObserver.getCount(a.getClass.getName()); 
} 

public int getBcount() { 

return centralObserver.getCount(b.getClass.getName()); 
} 
} 
//To get the sum of all the counts just call getAcount + getBcount. Of course, you can always add more listeners and more getXCount methods 
2

你描述这里有很多的耦合。最好的做法是消除所有这种反向通道依赖性,但是如果失败了,也许你可以让那些依赖性不在最初的监听器列表上,而是监听它们依赖的任何依赖项。或者你可以让他们等待,直到他们拥有了所有的信号。

您可以通过让侦听器识别他们依赖的人来自动执行依赖关系管理。侦听器列表将不会按插入顺序排序,而是要确保依赖对象遵循其依赖关系。您的侦听器接口会是这个样子:

interface Listener { 
    String getId(); 
    Collection<String> getDependencies(); 
    onEvent(char e); 
} 

或只是有引用,就像这样:

interface Listener { 
    Collection<Listener> getDependencies(); 
    onEvent(char e); 
} 
+0

你能否澄清你的意思是“反向通道”依赖? – Josh 2008-11-09 01:32:05

+0

监听器间依赖关系。 – sblundy 2008-11-09 01:53:14

1

“我们怎么会改变这一点,以便监听B是听者知道或者甚至没有?意识到,只是以某种方式耦合?“

你不经常想要连接两个这样的“同级”对象。你希望两个同龄人依靠一些共同点。

更深层次的问题是什么监听器或监听器B没有与他们收集的所有信息?

听众经常做两件事:收集数据并采取行动。通常这两件事情需要分开。听众应该倾听并收集并做更多。其他一些对象可以由Listener激活。

你可能只有一个听众有几个动作(A和B)。然后,听众可以对提供适当的计数以及B.它提供了一个“一”数到A.它提供了一个“A”或“B”数到B.