gpt4 book ai didi

java - OOP 设计——什么时候有一个共同的基类 [缺陷设计?]

转载 作者:塔克拉玛干 更新时间:2023-11-02 20:23:45 26 4
gpt4 key购买 nike

我有一个简化的设计如下(基本上是一堆处理程序来处理 2 种不同类型的请求:EventRequest 和 SpeechRequest)。下面是伪代码:

class SpeechRequest {sessionId: String; slot: String}
class EventRequest {sessionId: String; event: String}

class SpeechRequestHandler;
class EventRequestHandler;

class SpeechRequestHandler[A/B/C] extends SpeechRequestHandler {
- handle(request: SpeechRequest) {
doSt(request.slot)
}
}

class EventRequestHandler[A/B/C] extends EventRequestHandler {
- handle(request: EventRequest) {
doSt(request.event)
}
}

有 2 个不同的调度程序为每种类型的请求找到合适的处理程序并将它们转发给处理程序来处理:

class SpeechDispatcher {
- handle(request: SpeechRequest) {
handler: SpeechRequestHandler = findHandlerToHandle(request);
handler.handle(request);
}
}

class EventDispatcher {
- handle(request: EventRequest) {
handler: EventRequestHandler = findHandlerToHandle(request);
handler.handle(request);
}
}

现在,我想重构并创建一个基类/公共(public)类。自然地,我想到了这个:

class Request {sessionId: String}
class SpeechRequest extends Request {slot: String}
class EventRequest extends Request {event: String}

class RequestHandler {
- canHandleRequest(Request): bool
- handle(Request)
}

class SpeechRequestHandler extends RequestHandler {
- canHandleRequest(request: Request): bool = request instanceof SpeechRequest
}

class EventRequestHandler extends RequestHandler {
- canHandleRequest(request: Request): bool = request instanceof EventRequest
}

class SpeechRequestHandler[A/B/C] extends SpeechRequestHandler {
- handle(Request: request) {
//need to cast to specific type to extract a certain fields to do some operation
//!! I feel something is not right because of that
speechRequest:SpeechRequest = (SpeechRequest)request;
doSt(speechRequest.slot)

//other operation can work with base Request object; so it's OK
}
}

class EventRequestHandler[A/B/C] extends EventRequestHandler {
- handle(Request: request) {
eventRequest:EventRequest = (EventRequest)request;
doSt(eventRequest.event)

//other operation can work with base Request object; so it's OK
}
}

对于所有 SpeechRequestHandler[A/B/C]:handle 函数,我现在需要将 Request 对象具体转换为 (SpeechRequest) 对象:speechRequest:SpeechRequest = (SpeechRequest)request;

我觉得我的设计有缺陷。如果我需要将每个 SpeechRequestHandler 对象转换为 (SpeechRequest) 以便我可以对这些信息执行某些操作,这是否意味着在这种情况下重构基类没有意义?

请您提出一个更好的方法或设计模式来干净地处理这个问题。

谢谢。

最佳答案

如果你想保持类型安全,你可以用双重分派(dispatch)来做这种事情。参见 https://en.wikipedia.org/wiki/Double_dispatch

看起来你想要这样的东西:

interface HandlerSet{
SpeechHandler getSpeechHandler(Request request);
EventHandler getEventHandler(Request request);
}

abstract Request {
... common stuff ...
abstract handleWith(HandlerSet handlers);
}
class SpeechRequest extends Request {
sessionId: String; slot: String
handleWith(HandlerSet handlers) {
return handlers.getSpeechHandler(this).handle(this);
}
}
class EventRequest extends Request {
sessionId: String; event: String
handleWith(HandlerSet handlers) {
return handlers.getEventHandler(this).handle(this);
}
}

关于java - OOP 设计——什么时候有一个共同的基类 [缺陷设计?],我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48431941/

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