简体   繁体   English

Java设计,调用相关的业务逻辑/服务

[英]Java design, calling the associated business logic/service

I have 3 objects, a DTO called BalanceDTO which implements a interface RequestDTO and a Balance Entity. 我有3个对象,一个称为BalanceDTO的DTO,它实现了一个接口RequestDTO和一个Balance Entity。 I created the DTO because the entity I can't use, JAXB compliance (legacy code). 我创建DTO是因为我不能使用实体,即JAXB合规性(旧版代码)。

The DTO is used in the web service layer, BalanceService , and the Entity in the API I integrate to from the web service. DTO用于Web服务层BalanceService ,并且我从Web服务集成到的API中的Entity。 Between the web service and the API there is validation. 在Web服务和API之间存在验证。 RequestValidation which has sub validations for each type of RequestDTO ie BalanceRequestValidation . RequestValidation具有对每种RequestDTO类型的子验证,即BalanceRequestValidation

The validation component takes in a RequestDTO as a parameter and then needs to do validation for the specific component. 验证组件接受RequestDTO作为参数,然后需要对特定组件进行验证。 At the point of input the validation component doesn't know which object has been passed to it ie BalanceDTO , it only sees the interface. 在输入时,验证组件不知道哪个对象已传递给它,即BalanceDTO ,它仅看到接口。

I want to avoid using instanceof so I was thinking of using a visitor on the DTO so that it delegates itself to the validation that needs to be performed on it. 我想避免使用instanceof,因此我考虑在DTO上使用访问者,以便将其委托给需要在其上执行的验证。

But the validation needs more/other components as well not just the BalanceDTO as input parameters and different validations needs different input params. 但是验证不仅需要BalanceDTO作为输入参数,还需要更多/其他组件,并且不同的验证需要不同的输入参数。

Is there another way to know which object you are working with and the validation to choose without using instanceof? 是否有另一种方法可以知道您正在使用哪个对象以及无需使用instanceof即可选择的验证? Another design that I can follow? 我可以遵循的另一种设计?

You are well on the right track here - the Visitor design pattern is often the best way to avoid downcasting. 您在这里的路线正确-访客设计模式通常是避免垂头丧气的最佳方法。

I am going to suggest a combination of the visitor and delegation design patterns, though let's walk through some alternatives. 我将建议将visitordelegation设计模式组合在一起,尽管让我们逐步介绍一些替代方法。

Having the object do the validation itself via the RequestDTO interface is not viable since you need different components and the validation is not trivial in nature. 让对象自己通过RequestDTO接口进行验证是不可行的,因为您需要使用不同的组件,并且验证本质上并不简单。

Using instanceof and downcasting looks a little messy, and the compiler won't complain if you add a new validatable class and forget to add the validator - you'll be relying on a runtime error via ...else { throw new IllegalArgumentException("Unknown RequestDTO subtype!"); } 使用instanceof和downcast看起来有些混乱,并且如果您添加新的Validatable类而忘记添加验证器,则编译器将不会抱怨-您将通过...else { throw new IllegalArgumentException("Unknown RequestDTO subtype!"); } ...else { throw new IllegalArgumentException("Unknown RequestDTO subtype!"); }

The visitor design pattern is the classic way to avoid downcasting plus it also gives you a compiler error if you add a new class that should be validatable and forget to add the validation. visitor设计模式是避免向下转换的经典方法,如果您添加了一个应该可验证的新类,却忘记了添加验证,它还会给您带来编译器错误。

You can use accept() and visit() methods, or you can use method naming that is closer to your domain, eg validate() , like this: 您可以使用accept()visit()方法,也可以使用更接近您的域的方法命名,例如validate() ,如下所示:

public interface RequestDTO {
    boolean validate(RequestValidation validator);
}

public class BalanceDTO implements RequestDTO {
    // ...

    @Override
    public boolean validate(RequestValidation validator) {
        return validator.validate(this);
    }
}

public class RequestValidation {
    // components...

    public boolean validate(BalanceDTO balanceDTO) {
        return true;    // todo...
    }

    public boolean validate(AnotherDTO anotherDTO) {
        return true;    // todo...
    }
}

If you want to go a bit further you can delegate the validation to specific validation components, like this: 如果想进一步介绍,可以delegate验证delegate给特定的验证组件,如下所示:

public class RequestValidation {
    BalanceRequestValidation balanceRequestValidation;
    AnotherRequestValidation anotherRequestValidation;

    public boolean validate(BalanceDTO balanceDTO) {
        return balanceRequestValidation.validate(balanceDTO, a, b, c);
    }

    public boolean validate(AnotherDTO anotherDTO) {
        return anotherRequestValidation.validate(anotherDTO, x, y, z);
    }
}

Given I have understood your problem correctly, the visitor design pattern, possibly combined with the delegation design pattern, is indeed a good approach. 鉴于我已经正确理解了您的问题,将visitor设计模式(可能与delegation设计模式结合使用)确实是一个好方法。

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

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