具有所有类属性的构造函数或带有setter的默认构造函数? [英] Constructor with all class properties or default constructor with setters?

查看:108
本文介绍了具有所有类属性的构造函数或带有setter的默认构造函数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下是两种方法:




  • 具有所有类属性的构造函数



优点:我必须提供确切数量的参数类型,所以如果我犯了一个错误编译器警告我(顺便说一句,有一种方法,以防止错误地切换两个整数在参数列表?)



缺点:如果我有很多属性,实例化行可能变得很长,它可以跨越两行或更多行




  • setters和默认的空构造函数



我可以清楚地看到我的设置,所以如果我做错了我可以指出它,一旦我键入它(我不能使切换两个相同类型的变量previuos错误) p>

缺点:具有很多属性的对象的实例化可能需要几行(不知道这是否真的是骗局),如果我忘记设置属性编译器



你会做什么,为什么?
你知道任何光模式(考虑应该使用每次一个对象的7+属性被实例化)建议?
我问这个,因为我倾向于不喜欢大型构造函数,我不能弄清楚哪里是我正在寻找的变量,另一方面,我发现设置所有属性容易丢失一些





p>更新 - 我发现了一个与此相关的问题:构建大的不可变对象而不使用具有长参数列表的构造函数

解决方案

由Joshua Bloch倡导并在有效Java 中描述的构建器模式。在开发人员介绍了主要内容。 sun.com/learning/javaoneonline/2007/pdf/TS-2689.pdf ;无疑你可以挖掘一个更好的引用。



基本上,你有另一个类,可能是一个内部类,提供方法命名的属性设置,原来的生成器让你可以链接调用。



例如,假设我有一个简单的 Message 几个属性。构建此代码的客户端代码可以使用构建器来准备消息,如下所示:

 消息消息= new Message.Builder()
.sender(new User(...))
.recipient(new User(...))
.subject Hello,world!)
.text(messageText)
.build();

Message.Builder 类似如下:

  public class Builder {

private User sender = null;
//其他属性

public Builder sender(用户发件人){
this.sender = sender;
return this;
}
//其他属性的方法

public Message build(){
消息消息= new Message();
message.setSender(sender);
//设置其他属性
return message;
}

}


Following are the two approaches:

  • constructor with all the class properties

Pros: I have to put an exact number of types of parameters so if I make an error the compiler warns me (by the way, is there a way to prevent the problem of having erroneously switched two Integer on the parameter list?)

Cons: if I have lots of properties the instantiation line can become really long and it could span over two or more lines

  • setters and the default empty constructor

Pros: I can clearly see what I'm setting, so if I'm doing something wrong I can pinpoint it as soon as I'm typing it (I can't make the previuos error of switching two variables of the same type)

Cons: the instantiation of an object with lots of properties could take several lines (don't know if this is really a con) and if I forget to set a property the compiler doesn't say anything.

What will you do and why? Do you know of any light pattern (consider that it should be used everytime an object wth 7+ properties is instantiated) to suggest? I'm asking this because I tend to dislike large constructors where I can't figure out fast where is the variable I'm looking for, on the other hand I find the "set all properties" vulnerable to missing some of the properties.

Feel free to argument my assumptions in pros and cons as they are only mine thoughts :)

Update - a question I've found which is related to this: Building big, immutable objects without using constructors having long parameter lists

解决方案

You might look at the Builder pattern advocated by Joshua Bloch, and described in Effective Java. There's a presentation with the main points at http://developers.sun.com/learning/javaoneonline/2007/pdf/TS-2689.pdf; no doubt you could dig up a better reference.

Basically, you have another class, probably an inner class, which provides methods named after the properties being set, and which return the original builder so you can chain calls. It makes for quite a readable chunk of code.

For example, let's suppose I have a simple Message with a few properties. The client code constructing this could use a builder to prepare a Message as follows:

Message message = new Message.Builder()
    .sender( new User( ... ) )
    .recipient( new User( ... ) )
    .subject( "Hello, world!" )
    .text( messageText )
    .build();

A fragment of Message.Builder might look similar to the following:

public class Builder {

    private User sender = null;
    // Other properties

    public Builder sender( User sender ) {
        this.sender = sender;
        return this;
    }
    // Methods for other properties

    public Message build() {
        Message message = new Message();
        message.setSender( sender );
        // Set the other properties
        return message;
    }

}

这篇关于具有所有类属性的构造函数或带有setter的默认构造函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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