Java实现构建器模式的最佳方法

编程入门 行业动态 更新时间:2024-10-26 09:26:36
本文介绍了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实现构建器模式的最佳方法

本文发布于:2023-05-23 14:43:49,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1326140.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:模式   方法   Java

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!