我应该在DTO中使用构建器模式吗? [英] Should i use builder pattern in DTO?

查看:143
本文介绍了我应该在DTO中使用构建器模式吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这可能是一个相当含糊的问题,但我想知道更多意见。我已经使用Spring MVC构建了Rest API服务,并且实现了DTO-Domain-Entity模式。我想知道您对在DTO中实现 Builder模式的看法,例如

This might be a pretty subjetive question, but i would to know some more opinions. I've built a Rest API service with Spring MVC, and i implemented the DTO-Domain-Entity pattern. I want to know what do you think about implementing the Builder pattern in DTOs, something like

public class UserResponseDTO
    extends AbstractResponseDTO {

    private String username;
    private Boolean enabled;

    public UserResponseDTO(String username, Boolean enabled) {
        this.username = username;
        this.enabled = enabled;
    }

    public String getUsername() {
        return this.username;
    }

    public Boolean getEnabled() {
        return this.enabled;
    }

    public static class Builder {

        private String username;
        private Boolean enabled;

        public void setUsername(String username) {
            this.username = username;
        }

        public void setEnabled(Boolean enabled) {
            this.enabled = enabled;
        }

        public UserResponseDTO build(){
            return new UserResponseDTO(username, enabled);
        }
    }
}

根据定义:


Builder设计模式的目的是将复杂对象的构造与其表示分离。这样,相同的构造过程可以创建不同的表示形式。

The intent of the Builder design pattern is to separate the construction of a complex object from its representation. By doing so the same construction process can create different representations.

在我的大多数DTO案例中(不要全部说),我没有更复杂的对象可以构建,例如这个案例。而且,说实话,如果我们谈论DTO,我想不出构造复杂对象的任何例子。

In most of my DTO cases (to not say all of them) i don't have a more complex object to build, such this case. And, honestly, i can't think of any example where construct a complex object if we are talking about DTOs.


其中一种模式

One of the patterns which helps make immutable objects easier to use and to add clarity to code is the Builder pattern.

Builder模式提供了对象不变性,这有助于使不可变对象更易于使用,并增加代码的清晰度。然后,我们可以认为DTO本身就是服务响应,因此不应更改,因为这是响应(至少这是我所考虑的方式)

Builder pattern provides objects immutabiltiy. Then, we can think than a DTOs is the service response itself and should not be altered as this is a response (at least that is how i am thinking about it)

那么您怎么看?我是否应该对DTO使用这种模式(考虑到这种情况,并且可能是大多数情况,都不满足复杂对象原理)?

So what do you think? Should i use this pattern to DTOs (given the fact that this case, and probably most of them, does not satisfy the complex object principle)?

推荐答案

我的简短回答是,这是个人喜好问题。如果您喜欢Builder模式的工作方式,请应用它。对于这么小的情况,我认为这都不重要。我个人的喜好是不要在这里使用Builder,因为它似乎并不会增加太多价值。

My short answer is that it's a matter of preference. If you like the way that Builder Pattern works, apply it. For such a small case, I don't think it matters either way. My personal preference would be not to use Builder here as it doesn't seem to add much value.

我的更长的答案是,Builder Pattern旨在简化配置。复杂的对象,而无需诉诸伸缩式构造函数之类的反模式。在这种情况下,两种情况下都没有反模式;

My longer answer is that Builder Pattern is designed to facilitate easier configuration of complex objects without resorting to anti-patterns like telescoping constructor. In this case, you don't have an anti-pattern in either case; both options are relatively small and efficient.

虽然我们在谈论首选项,但我更喜欢使用流畅接口的构建器模式的修改版本;每个方法都返回Builder的实例,以便可以将方法调用链接在一起(而不是在单独的行上)。这类似于您链接的文章中的Java示例。

While we're talking about preferences, though, I prefer a modified version of the builder pattern that uses a fluent interface; each method returns the instance of Builder so that the method calls can be chained together (rather than on separate lines). This is similar to the Java example in the article that you linked.

我将修改您的构建器,使其看起来像这样:

I'd modify your builder to look like this:

public static class Builder {

        private String username;
        private Boolean enabled;

        public Builder setUsername(String username) {
            this.username = username;
            return this; 
        }

        public Builder setEnabled(Boolean enabled) {
            this.enabled = enabled;
            return this;
        }

        public UserResponseDTO build(){
            return new UserResponseDTO(username, enabled);
        }
    }

因此,使用生成器看起来像这样:

Thus, using the builder would look something like this:

UserResponseDTO ur = new Builder().setUsername("user").setEnabled(true).build();

再次,只是个人喜好。

这篇关于我应该在DTO中使用构建器模式吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆