Java实现构建器模式的最佳方法 [英] Java best way to implement builder pattern

查看:94
本文介绍了Java实现构建器模式的最佳方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下哪个是实现构建器模式的更好方法?

Which of the following is the better approach to implement the builder pattern?

1)使用对象而不是构建器中的所有属性来构建(并创建)

1) Using the object to build instead of all its properties in the builder (and create it in the builder constructor):

public class Person {
    private String firstName;
    // other properties ...

    private Person() {}

    // getters ...

    public static class Builder {
        // person object instead of all the person properties
        private Person person;

        public Builder() {
            person = new Person();
        }

        public Builder setFirstName(String firstName) {
            person.firstName = firstName;

            return this;
        }

        // other setters ...

        public Person build() {
            if (null == person.firstName) {
                throw new IllegalStateException("Invalid data.");
            }

            return person;
        }
    }
}

2)使用以下属性要构建的对象而不是直接在构建器中创建的对象(并在build()方法中创建):

2) Using the properties of the object to build instead of the object directly in the builder (and create it in the build() method):

public class Person {
    private String firstName;
    // other properties ...

    private Person() {}

    // getters ...

    public static class Builder {
        // person properties instead of object
        private String firstName;
        // other properties ...

        public Builder() {}

        public Builder setFirstName(String firstName) {
            this.firstName = firstName;

            return this;
        }

        // other setters ...

        public Person build() {
            if (null == this.firstName) {
                throw new IllegalStateException("Invalid data.");
            }

            Person person = new Person();
            person.firstName = firstName;

            return person;
        }
    }
}

我更喜欢第一种方法,因为我认为具有许多属性,在构建器中重复它们是多余的。第一种方法有一些缺点吗?

I prefer the first way because i think that with lots of properties repeat them in the builder is redundant. Are there some disadvantages with the first approach?

在此先感谢您的英语不好。

Thanks in advance and sorry for my bad english.

推荐答案

小注释:是的,这些属性可能是重复的,但它们具有优势

Small Note : Yes the properties might be a repeat but they have advantages

以下详细信息:
如果您查看详细信息此处

Pizza pizza = new Pizza(12);
pizza.setCheese(true);
pizza.setPepperoni(true);
pizza.setBacon(true);

这里的问题是,由于对象是通过多个调用创建的,因此可能处于不一致的状态通过其建设。这也需要大量额外的努力来确保线程安全。

The problem here is that because the object is created over several calls it may be in an inconsistent state partway through its construction. This also requires a lot of extra effort to ensure thread safety.

更好的选择是使用构建器模式。

The better alternative is to use the Builder Pattern.

请注意以下在Builder和各自的构造函数或父Pizza类中的方法-链接此处

Notice below method in Builder and respective constructor or parent Pizza class - full code in link here

 public static class Builder {

    public Pizza build() {    // Notice this method
      return new Pizza(this);
    }
  }

  private Pizza(Builder builder) {  // Notice this Constructor
    size = builder.size;
    cheese = builder.cheese;
    pepperoni = builder.pepperoni;
    bacon = builder.bacon;
  }

这篇关于Java实现构建器模式的最佳方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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