简体   繁体   English

Java类设计-最佳方法的建议。 类扩展抽象类并实现接口

[英]Java Class design - Advice for best approach. Class extend abstract class and implement interface

Not sure what approach to take for design classes. 不确定设计类采用什么方法。

public interface AuthoringEvent {
   ProcessingResultEvent authoringEvent(Event data);
}

AbstractAuthoringEvent abstract class implements interface AbstractAuthoringEvent抽象类实现接口

public abstract class AbstractAuthoringEvent implements AuthoringEvent {

   @Override
   public ProcessingResultEvent authoringEvent(Event data) {

       ResponseEntity<Event> resultData = callService(data);
       etc...
   }

   protected abstract ResponseEntity<Event> callService(Event requestData);

}

AuthoringEventIT class extends abstract class AuthoringEventIT类扩展了抽象类

public class AuthoringEventIT extends AbstractAuthoringEvent {

   @Autowired
   private RESTClient restClient;

   protected ResponseEntity<Event> callService(Event requestData) {

     return restClient.callWebServiceWithAction(url, httpPostMethod, requestData, Event.class);
   }

Resource endpoint class 资源端点类

@Autowired
private AuthoringEventIT authoringEventIT;

@PostMapping()
public ResponseEntity<Event> authorEvent(@RequestBody final EventRequest request) {

    ProcessingResultEvent responseData = authoringEventIT.authoringEvent(request);

    return responseData.getProcessedData();
}

l have 2 more same implementations like Event for Venue and Organization model classes. 我还有2个相同的实现,例如Event for Venue和Organization模型类。 l know l can remove interfaces and do all on abstract classes level which will all concrete classes implement for they own purpose as it on AuthoringEventIT.class, but my question is... 我知道我可以删除接口并在抽象类级别上进行所有操作,所有具体类都将按照自己的目的在AuthoringEventIT.class上实现,但是我的问题是...

Do these abstract classes really need separate interfaces to implement? 这些抽象类是否真的需要单独的接口来实现? Maybe all 3 abstract classes can implement one Authoring interface some kind of generic method, but how to avoid then implementation in concrete class like AuthoringEventIT which extends abstract class and need to implement again interface method which is already implemented in abstract class? 也许所有3个抽象类都可以实现某种Authoring接口某种通用方法,但是如何避免在AuthoringEventIT这样的具体类中实现扩展抽象类并需要再次实现已经在抽象类中实现的接口方法呢? Many thanks... 非常感谢...

In the Spring framework, there is no a problem to have multiple implementations of one interface. 在Spring框架中,一个接口具有多个实现没有问题。 It will be not an issue if between the interface and final implementation will be an abstract class. 如果接口和最终实现之间将是一个抽象类,这将不是问题。

There are two ways how to solve the conflict in this case: 在这种情况下,有两种方法可以解决冲突:

  • Use @Qualifier and load all implementations to choose one in runtime 使用@Qualifier并加载所有实现以在运行时选择一个
  • Use one of @Condition and load one implementation which depends on some condition 使用@Condition之一并加载一个取决于某些条件的实现

In the first case it will be something like this 在第一种情况下,将是这样的

@Component("authoringEventIT")
public class AuthoringEventIT extends AbstractAuthoringEvent { //code }

@Autowired
@Qualifier("authoringEventIT")
private AuthoringEvent authoringEventIT;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM