Java 委托事件模型模式
当应用此模式委托事件模型时,将所有代码放在 fire...
方法中并从公共方法传递参数是否正确?
像这样
public void addBananas(Banana banana) {
fireBananaAdded(banana);
}
private void fireBananaAdded(Banana banana) {
//Create event etc and add banana to list here
}
或者我应该在 addBananas
方法中使用此示例中的添加到列表部分吗?因为如果我这样做,我将没有机会将香蕉对象“附加”到将传递给侦听器的事件对象,对吗?
When applying this pattern Delegation Event Model, is it correct to put ALL the code in the fire...
methods and pass the parameters from the public method?
Like this
public void addBananas(Banana banana) {
fireBananaAdded(banana);
}
private void fireBananaAdded(Banana banana) {
//Create event etc and add banana to list here
}
Or should I have the add to list part in this example in the addBananas
method instead? Because if I do it this way I will not have the opportunity to "attach" the banana object to the event-object which will be passed to the listeners, right?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我会在
addBanana()
中放入尽可能多与实际添加香蕉相关的逻辑。当我完成
addBanana()
后,我会调用fireBananaAdded()
,它将生成适当的BananaAddedEvent
并将其发送到BananaAddedListeners
(或者只是BananaListeners
,无论您有什么。)将 ADD 逻辑放入 FIRE 方法中就是简单的,嗯,BANANAS!
I would put as much logic in
addBanana()
that is related to actually adding the Banana as I can.When I'm done with
addBanana()
, I would callfireBananaAdded()
which would generate the appropriateBananaAddedEvent
and send it to theBananaAddedListeners
(or justBananaListeners
, which ever you have.)To put the ADD logic in the FIRE method is simply, well, BANANAS!