什么构成了Builder模式? [英] What makes up a Builder Pattern?

查看:115
本文介绍了什么构成了Builder模式?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你有什么关于Build Pattern的问题吗?

Hello I have a few questions in regards to the Build Pattern?


  1. 为什么主类中的实例变量是私有的? / li>
  2. 为什么内部类声明为静态?

  3. 为什么内部类的实例变量是私有的和重复的?

  4. 我们在方法中返回Builder对象内包含的内容?

  5. 为什么主构造函数为私有?

  1. Why are the instance variables in the main class private?
  2. Why is the inner class declared static??
  3. Why are the instance variables in the inner class private and repeated?
  4. We return the Builder Object within the methods what exactly is contained within this object?
  5. Why is the main constructor private?





public class Plank {
    //1. Why are these instance variables private?
    private double widthInches;
    private double heightInches;
    private double thicknessInches;

    //2. Why is this class static?
    public static class Builder {
        //Why are these instance variables private and repeated?
        private double widthInches;
        private double heightInches;
        private double thicknessInches;

        public Builder widthInches(double inches) {

            widthInches = inches;
            //3. What is returned here what object is this referencing?
            return this;

        }
        public Builder heightInches(double inches) {
            heightInches = inches;
            return this;
        }
        public Builder thicknessInches(double inches) {
            thicknessInches = inches;
            return this;
        }
        public Plank build() {
            return new Plank(this);
        }
    }
    //4. Why is this constructor private?
    private Plank(Builder build) {
        widthInches = build.widthInches;
        heightInches = build.heightInches;
        thicknessInches = build.thicknessInches;
    }

}


推荐答案

主类中的实例变量是私有的,为了访问控件属性。在Java中,对象的大多数字段按照约定是私有的,具有公共访问器(例如, Type getField() void setField(Type field))必要时公开。

Instance variables in the main class are private for the sake of access control to the object's properties. In Java, most fields of an object are private by convention, with public accessors (ie. Type getField() and void setField(Type field)) exposed where necessary.

这是因为根据面向对象的原则,除了对象本身之外,所有行为的实现都应该隐藏起来。在实践中,这导致覆盖底层实现的能力(比如,如果您希望封装对象提供存储,或者希望实现需要数据库提取和更新,而不是存储数据的本地副本。

This is because according to object-oriented principles, the implementation of all behavior should be hidden from view except by the object itself. In practice, this leads to the ability to override the underlying implementation (say, if you want an encapsulated object to provide storage, or if you want the implementation to require database fetches and updates every time instead of storing a local copy of the data.

Builder class是 Plank 类的静态成员,因为它提供了唯一的外部访问方式来创建一个 Plank 对象,它们紧密耦合这两个类,因为它们彼此依赖。

The Builder class is a static member of the Plank class because it provides the only externally-accessible way to create a Plank object. It tightly couples the two classes, since they are dependent on each other.

重要 :你的代码中有一个非常重要的打字错误: Builder class应该是 public

IMPORTANT: You have a very important typo in your code: The Builder class should be public.

这样做的好处意味着您可以构建一个 Builder ,并使用它来流利地构造一个 Plank 一点一点,而不是将大量的属性传递给构造函数e Builder 为静态意味着您可以使用默认参数来获取 Plank

The benefits of doing this means that you can construct a Builder and use that to fluently construct a Plank bit by bit instead of having a ton of properties passed into a constructor. The Builder being static means you can do this to receive a Plank with default parameters:

Plank p = (new Plank.Builder()).build();



3。为什么内部类的实例变量是私有和重复的?



这些实例变量作为模板 Plank 对象。通过这些可配置的方式,您可以在 Builder 中逐个构建一个 Plank ,然后拉动触发器并弹出一个新的 Plank 。类比是设置木刻机的参数,然后使用这些参数来构造一个或多个相同的木板,并提供合理的可用默认值。

3. Why are the instance variables in the inner class private and repeated?

These instance variables serve as your template for Plank objects. By having these configurable one by one, you can construct a Plank little by little inside your Builder, then pull the trigger and pop out a new Plank. The analogy is setting up the parameters of a woodcutting machine, which would then use those parameters to construct one or many identical planks, and would provide sane, usable defaults.

意味着您可以执行以下操作:

This means you can do the following:

Plank.Builder builder = new Plank.Builder();
builder.widthInches(13);
builder.heightInches(2);
// Don't set the thickness; use the default defined in the Builder's constructor.
Plank p1 = builder.build();
Plank p2 = builder.build();
// Now set the thickness to a new value
builder.thicknessInches(14);
Plank p3 = builder.build();



4。我们在方法内返回Builder对象,这个对象中包含的是什么?



噢,这是我最喜欢的一个。这使我们可以执行所谓的方法链接,它类似于字符串连接和数学运算在语言中的工作,而是使用变量和函数调用而不是运算符和操作数。这可以让我们做这样的技巧:

4. We return the Builder Object within the methods what exactly is contained within this object?

Ooh, this is one of my favorites. This lets us perform what's called method chaining, which is similar to how things like string concatenation and mathematical operations work in the language, but with variables and function calls instead of operators and operands. This lets us do tricks like this:

Plank p = (new Plank.Builder())
           .heightInches(13)
           .thicknessInches(14)
           .widthInches(12)
           .build();

或此:

Plank.Builder builder = (new Plank.Builder())
                         .heightInches(13)
                         .thicknessInches(14)
                         .widthInches(12);
Plank p1 = builder.build();
Plank p2 = builder.build();

当加上更好的方法名称时,读取时可以听起来像一个口语字命令这是一种被称为流畅的界面的模式。 C#的LINQ和Enumerables使用其中的一个。

When coupled with better method names, this can be used to sound like a spoken-word command when read aloud, which is a pattern referred to as a "fluent interface". C#'s LINQ and Enumerables use one of these.

那么因为使用 Builder 更好!我们希望避免使用这些类的开发人员有丑陋的语法,所以我们保持丑陋的位隐藏和不可访问,保持手指远离使这种模式工作的齿轮。

Well, because using a Builder is just better! We want to avoid the developer who uses these classes from having ugly syntax, so we keep the ugly bits hidden and inaccessible, keeping fingers away from the gears which make this pattern work, as it were.

我们不要想要在客户端代码中是这样的:

What we DON'T want is something like this in client code:

Plank.Builder builder = new Plank.Builder().heightInches(13);
Plank p1 = new Plank(builder);

这比非常糟糕:

Plank p1 = (new Plank.Builder()).heightInches(13).build();



添加



如果要清洁这个更加可以让 Plank.Builder 私有的构造函数,并在 Plank 中提供以下方法:

Additions

If you want to clean this up even more, you could make the constructor for Plank.Builder private, and provide the following method inside Plank:

public static Plank.Builder getBuilder() {
    return new Plank.Builder();
}

然后,客户端代码将是:

Then, client code would be:

Plank p1 = Plank.getBuilder()
                 .heightInches(13)
                 .build();

这篇关于什么构成了Builder模式?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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