您可以使用构造器或构造器模式或构造器模式的变体来解决初始化步骤中字段过多的问题。
我将扩展您的示例,以证明我对这些选项有用的观点。
了解您的示例:
可以说an
Offer只是4个字段的容器类:
public class Offer { private int price; private Date dateOfOffer; private double duration; private Hotelonly hotelOnly; // etc. for as many or as few fields as you need public int getPrice() { return price; } public Date getDateOfOffer() { return dateOfOffer; } // etc.}如您的示例所示,要使用以下方法来为这些字段设置值:
public void setHotelonly(Hotelonly hotelOnly) { this.hotelonly = hotelOnly; }不幸的是,这意味着如果您需要在所有字段中都包含值的报价,则必须做您拥有的一切:
Offers offers = new Offers();Offer offer = new Offer();offer.setPrice(price);offer.setDateOfOffer(date);offer.setDuration(duration);offer.setHotelonly(hotelOnly);offers.add(offer);
现在让我们看看如何改善这一点。
选项1:构造函数!
除了默认构造函数(默认构造函数当前为
Offer())以外的其他构造函数对于初始化类中字段的值很有用。
Offer使用构造函数的版本如下所示:
public class Offer { private int price; private Date dateOfOffer; //etc. // ConSTRUCTOR public Offer(int price, Date dateOfOffer, double duration, Hotelonly hotelOnly) { this.price = price; this.dateOfOffer = dateOfOffer; //etc. } // Your getters and/or setters}现在,我们可以在一行中对其进行初始化!
Offers offers = new Offers();Offer offer = new Offer(price, date, duration, hotelOnly);offers.add(offer);
更好的是,如果您从未使用过
offer那一行:
offers.add(offer);您甚至不需要将其保存在变量中!
Offers offers = new Offers();offers.add( new Offer(price, date, duration, hotelOnly) ); // Works the same as above
选项2:构建器模式
一个生成器模式,如果你想有任何的字段默认值的选项很有用。
构建器模式解决的问题是以下凌乱的代码:
public class Offer { private int price; private Date dateOfOffer; // etc. // The original constructor. Sets all the fields to the specified values public Offer(int price, Date dateOfOffer, double duration, Hotelonly hotelOnly) { this.price = price; this.dateOfOffer = dateOfOffer; // etc. } // A constructor that uses default values for all of the fields public Offer() { // Calls the top constructor with default values this(100, new Date("10-13-2015"), 14.5, new Hotelonly()); } // A constructor that uses default values for all of the fields except price public Offer(int price) { // Calls the top constructor with default values, except price this(price, new Date("10-13-2015"), 14.5, new Hotelonly()); } // A constructor that uses default values for all of the fields except Date and Hotelonly public Offer(Date date, Hotelonly hotelOnly) { this(100, date, 14.5, hotelOnly); } // A bunch more constructors of different combinations of default and specified values}看到那会变得多么混乱吗?
Builder模式是,你把另一个类 的内部 类。
public class Offer { private int price; // etc. public Offer(int price, ...) { // Same from above } public static class OfferBuilder { private int buildPrice = 100; private Date buildDate = new Date("10-13-2015"); // etc. Initialize all these new "build" fields with default values public OfferBuilder setPrice(int price) { // Overrides the default value this.buildPrice = price; // Why this is here will become evident later return this; } public OfferBuilder setDateOfOffer(Date date) { this.buildDate = date; return this; } // etc. for each field public Offer build() { // Builds an offer with whatever values are stored return new Offer(price, date, duration, hotelOnly); } }}现在,您不必具有那么多的构造函数,但是仍然能够选择要保留默认值的值以及要初始化的值。
Offers offers = new Offers();offers.add(new OfferBuilder().setPrice(20).setHotelonly(hotelOnly).build());offers.add(new OfferBuilder().setDuration(14.5).setDate(new Date("10-14-2015")).setPrice(200).build());offers.add(new OfferBuilder().build());最后的报价只是带有所有默认值的报价。其他是我设置的默认值。
看看如何使事情变得容易吗?
选项3:构建器样式的变化
您还可以通过简单地使当前的setter返回相同的Offer对象来使用构建器模式。除了没有额外的
OfferBuilder类外,其他部分完全相同。
警告:如下面的用户WW所述,此选项破坏了JavaBeans-Java容器类的标准编程约定,例如Offer。因此,您不应将其用于专业目的,而应在自己的实践中加以限制。
public class Offer { private int price = 100; private Date date = new Date("10-13-2015"); // etc. Initialize with default values // Don't make any constructors // Have a getter for each field public int getPrice() { return price; } // Make your setters return the same object public Offer setPrice(int price) { // The same structure as in the builder class this.price = price; return this; } // etc. for each field // No need for OfferBuilder class or build() method}您的新初始化代码是
Offers offers = new Offers();offers.add(new Offer().setPrice(20).setHotelonly(hotelOnly));offers.add(new Offer().setDuration(14.5).setDate(new Date("10-14-2015")).setPrice(200));offers.add(new Offer());最后的报价只是带有所有默认值的报价。其他是我设置的默认值。
因此,尽管工作量很大,但如果要清理初始化步骤,则需要对其中每个具有字段的类使用以下选项之一。然后使用每个方法附带的初始化方法。
祝好运!是否需要进一步说明?



