清除对建造者模式的疑虑 [英] Clearing doubts about the builder pattern

查看:163
本文介绍了清除对建造者模式的疑虑的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在学习构建器模式,到目前为止我明白,它是用于初始化的常用模式的一个很好的替代方法:

I am learning about the builder pattern, and so far I understood that, it is a great alternative to the commonly patterns used for initialization:


  • 伸缩构造函数模式

  • Telescoping Constructor Pattern

JavaBean模式

JavaBean Pattern

问题是,我真的不想从我的域模型中的对象中删除getter和setter。我总是喜欢把它们当作POJO。我不喜欢它的原因之一是:
如果我不使用POJO,那么在使用ORM框架时注释变量并不容易......

The thing is, I don't really like to remove the getters and setters from the objects in my domain model. I always like to keep them as POJOs. One of the reasons I don't like it is: If i don't use POJOs, then it is not easy to annotate the variables when using ORM frameworks...

所以这是我的疑惑:
- 是否可以在不使用静态内部类的情况下实现构建器模式?
- 如果我必须使用内部类来使用构建器模式,你认为保持getter和setter是正确的吗?
-I在练习中做了一个小例子,我试图避开内部类。
你能告诉我你怎么看?

So here are my doubts: -Is it possible to implement the builder pattern without using static inner classes? -If I have to use the builder pattern by using the inner class, do you think it is correct to keep the getters and the setters? -I did a little example for practice where I tried to avoid the inner class. Could you let me what do you think about it?

产品

    public class Product
{
    private String color;
    private int price;

    public Product() {
    }

    public String getColor() {
        return color;
    }

    public void setColor(String color) {
        this.color = color;
    }

    public int getPrice() {
        return price;
    }

    public void setPrice(int price) {
        this.price = price;
    }

    public String toString() {
        return getColor() + "\n" + getPrice();
    }    
}

构建器

public class Builder
{
    private Product product;

    public Builder() {
        product = new Product();
    }
    public Builder withColor(String color) {        
        product.setColor(color);
        return this;
    }

     public Builder withPrice(int price) {        
        product.setPrice(price);
        return this;
    }
    public Product build() {
        return product;
    }
}**

客户

public class Client
{

    public static void main(String[] args) {
        System.out.println(new Builder().withColor("Black").withPrice(11).build());
        System.out.println("-----------------------------------------------------");
        System.out.println(new Builder().withColor("Blue").withPrice(12).build());
    }
}

推荐答案

Builder模式对于创建不可变对象并避免使用带有可选参数的多个构造函数。

The Builder pattern is useful to create immutable objects and avoid several constructors with optional parameters.

IMO使用Builder模式构建可以使用setter更新的POJO是没用的。您只需创建一个额外的类。

IMO using Builder pattern to build a POJO which can be updated using setters is useless. You only create an additional class.

根据使用的ORM框架,可能不需要存在setter方法。但只能通过反思来分配成员价值。

Depending on the ORM framework used, it might not need the presence of setter method. But only assigning members values through reflection.

产品类别:

public final class Product {
    private final String color;
    private final int price;

    public Product(Builder builder) {
        this.color = builder.getColor();
        this.price = builder.getPrice();
    }

    public String getColor() {
        return color;
    }

    public int getPrice() {
        return price;
    }

    public String toString() {
        return getColor() + "\n" + getPrice();
    }    
}

构建器类:

public final class Builder {

    private String color;
    private int price;

    public Builder() {
        // Assign any default values
    }

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

    public Builder price(int price) {        
        this.price = price;
        return this;
    }

    protected String getColor() {
        return color;
    }

    protected int getPrice() {
        return price;
    }

    public Product build() {
        return new Product(this);
    }
}

这篇关于清除对建造者模式的疑虑的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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