重命名 Java 中观察者模式的实现方法
我使用 Java 中的 Observable 类/Observer 接口来实现观察者模式。 Observer 接口需要重写 update(Observable o, Object arg) 方法。
问题是我正在观察相当数量的类,并且我的 update() 方法变得非常大:
public class Foo implements Observer {
....
public void update(Observable o, Object param) {
if (o instanceof A) {
// large chunk of code
...
} else if (o instanceof B) {
// large chunk of code
...
}
...
} else if (o instanceof H) {
...
}
}
}
为了拆分该方法,我正在考虑使用例如 AObserver 接口、BObserver 接口来扩展 Observer 接口。 . 需要分别覆盖 onAUpdate, onBUpdate ..。此方法还可以根据类所实现的接口轻松确定该类正在观察哪些 Observable。
class Foo implements AObserver, BObserver {
....
public void onAUpdate(Observable o, Object param) {
if (o instanceof A) {
// large chunk of code
...
}
public void onBUpdate(Observable o, Object param) {
if (o instanceof B) {
// large chunk of code
...
}
}
问题是,如果我继承Observer,我仍然必须实现update()方法。我无法将其重命名为 onAUpdate 或我选择的其他名称。
有什么建议吗?谢谢。
I'm using the Observable class / Observer interface in Java to implement the observer pattern. The Observer interface requires overwriting the update(Observable o, Object arg) method.
The problem is that I'm observing a fair number of classes, and my update() method has gotten very large:
public class Foo implements Observer {
....
public void update(Observable o, Object param) {
if (o instanceof A) {
// large chunk of code
...
} else if (o instanceof B) {
// large chunk of code
...
}
...
} else if (o instanceof H) {
...
}
}
}
In order to split up the method, I'm thinking of extending the Observer interface with e.g. AObserver interface, BObserver interface.. which requires overwriting onAUpdate, onBUpdate .. respectively. This method will also make it easy to determine what Observables the class is observing based on the interfaces it's implementing.
class Foo implements AObserver, BObserver {
....
public void onAUpdate(Observable o, Object param) {
if (o instanceof A) {
// large chunk of code
...
}
public void onBUpdate(Observable o, Object param) {
if (o instanceof B) {
// large chunk of code
...
}
}
The problem is that if I inherit Observer, I still have to implement the update() method. I can't rename it to onAUpdate or some other name of my choosing.
Any advice? Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我建议创建一个 UpdateHandler 接口来对任何给定的代码块进行处理。每个案例都可以用地图来处理。
示例代码如下:
将实例字段添加到主类,如下所示:
创建一个方法来初始化处理程序
更新方法将简单地查找适当的处理程序并分派调用
此实现将允许您根据需要添加新的处理程序,只需进行最少的更改。
另一种可能的实现可以基于之前的工作来完成,只需 3 个步骤。
1)更改handlers字段的声明。
2) 更改初始化方法
3) 更改主更新方法以创建所提供的 UpdateHandler 实现的新实例。
I'll suggest to create an UpdateHandler interface to do the processing for any given code block. Each case can be handle with a map.
Sample code follows:
Add an instance field to your main class as follows:
Create a method to have the handlers initialized
The updates method will simply find the appropriate handler and dispatch the call
This implementation will allow you to add new handler as needed with minimal changes.
Another possible implementation can be done base on the previous work, just a 3 step procedure.
1) Change the declaration of the handlers field.
2) change the initialize method
3) change the main update method to create a new instance of the provided UpdateHandler implementation.
在这里,我使用观察者和访问者模式编写了一个原始实现。您可以将此作为一个想法来增强它并填补您看到的任何漏洞。
希望你没有迷路。 :)
Here I coded a raw implementation using observer and visitor pattern. You can take this as an idea to enhance upon it and to fill wherever you see some holes.
I hope you are not lost. :)
将每个条件中的代码块移动到适当命名的方法中。
Move the chunks of code within each condition to appropriately named methods.