gpt4 book ai didi

java - 在 Java 中实现事件生成器习惯用法

转载 作者:行者123 更新时间:2023-12-01 16:11:59 26 4
gpt4 key购买 nike

我正在尝试实现事件生成器习惯用法( http://www.javaworld.com/javaworld/jw-09-1998/jw-09-techniques.html )。不过,当涉及到可观察类时,我发现事情有点“奇怪”。假设我有以下类(class):

interface BakeryListener  + orderReceived(BakeryEvent event)  + orderProcessing(BakeryEvent event)  + orderFinished(BakeryEvent event)  + orderDelivered(BakeryEvent event)  LogView, OrderReadyView etc. implements BakeryListener  Creates a GUI for each own useOrder  VO / DTO object, used as the source in BakeryEvent BakeryDAO (the observable)  - orders : Vector  - listeners : Vector  + takeOrder, cancelOrder, bake and other regular DAO methods, triggering an event by calling fireEvent+ addBakeryListener(BakeryEvent event)  + removeBakeryListener(BakeryEvent event)  - fireEvent(Order source, EVENTTYPE????)BakeryGUI  Creates/gets a reference to BakeryDAO. Creates and attaches LogView, OrderReadyView as listeners on BakeryDAO.

In the link I gave initially he recommends to "Name the event propagator method fire[listener-method-name].". I find this to be redundant: making a snapshot and iterating over the listeners in each fire-method, when the only thing changing is which method to call on the interface. Thus I have made a single fireEvent method, and it is working sort of. The problem is to keep the datatype of the event parameter in fireEvent "in sync" with the methods defined in BakeryListeners. Currently the fireEvent looks like this (excerpt):

for(BakeryListener listener : copyOfListeners){    if(eventType.equals("received")) listener.orderReceived(event);    else if(eventType.equals("processing")) listener.orderProcessing(event);}

... etc. I guess I could use an enum instead of a string to make it impossible to call fireEvent with an eventType that doesn't exist, but I still have to map Type.RECEIVED to listener.orderReceived etc?

Is it possible for the fireEvent method to take the BakeryListeners methods as a parameter? I.e (pseudo-code) method declaration:

fireEvent(BakeryListeners.methods eventType, Order source)

然后直接在 fireEvent 中调用适当的方法(无需 if/switching):

call(listener, eventType( source)) 

那么也不可能创建未在接口(interface) BakeryDAO.takeOrder() -> fireEvent(eventWhoDoesntExist) -> 异常中定义的事件?

用Java可以做到这一点吗?或者如果我理解错误的话更好的方法?

最佳答案

我认为枚举事件类型并将事件类型包含在事件中比每个事件类型一个方法更清晰。

首先,您可以避免所有面包师监听器必须实现所有方法,然后如果他们只对一个事件感兴趣,则将它们留空。 (你实现过MouseListener吗?)

其次,当您发现需要 orderBilled 和 orderPaied 时,您可以稍后添加到枚举中,而无需向所有不需要处理该类型事件的监听器添加更多方法。

public class BakeryEvent {
public enum Type { Received, Processing,Finished,Delivered,Billed,Paied };

private Type myType;
private Order myOrder;

BakeryEvent( Order order, BakeryEvent.Type bet ) {//...

}
//...
}


public interface BakeryListener {
public void handleBakeryEvent( BakeryEvent be );
}

然后

public class OvenScheduler implements BakeryListener {

public void handleBakeryEvent( BakeryEvent be ){
if ( BakeryEvent.Type.Received.equals(be.getEventType() ) {
scheduldeOven( be.getOrder() );
}
}
// hey look I don't have to implement orderBilled() and then do nothing!

}

关于java - 在 Java 中实现事件生成器习惯用法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/806282/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com