具有构造函数数量变化的子类的静态工厂方法



这更多是一个样式问题,我不确定解决问题的方法是"最清洁"。

实现细节并不重要,我想做的是以下内容:

我有一个抽象类,该课程由几个子类扩展,这些子类本质上都非常相似。我想通过仅通过超级阶级中的静态方法来使它们从客户端隐藏这些类,该方法将超类引用到子类实例(混凝土类由方法参数确定)。到目前为止,我相信这是一件很普遍的事情。

现在,我正在谈论的这些子类可以分为两组,一个组,其成员需要两个参数才能构造,另一组需要一个组,其成员需要附加参数。

所以现在我的问题是:如何让客户通过上述静态方法获得这两种类型。我是否提供两个带有不同参数列表的静态方法?我是否强迫客户在第一组不需要的第三个可选参数上传递零?有设计模式吗?(我已经考虑了有效Java的构建器模式,但据我了解,通常是在不同的上下文中使用的)。还是我修改我的继承层次结构?

任何答案都将不胜感激!

编辑:

我相信我的问题目前有点令人费解,所有这些都添加了一点代码以使其更清楚:

abstract class SuperClass{
    public static SuperClass getSuperClass(String type, int i1, int i2, int optional) {
        /*
         *'type' alone determines which concrete subclass is instanciated
         *'optional' is only needed for the construction for some of those 'types'
         *so the implementation of this method might look like this:
         */
        switch(type) {
            case "1":
                return new Subclass1(i1, i2);
                break;
            case "2":
                return new Subclass2(i1, i2, optional);
                break;
        /*
         *So the problem is this: always passing 'optional' is weird if it 
         *is then simply discarded in some cases.
         *Overloading is weird as well because I don't want to split up 
         *the switch statement or check for exceptions in every case 
         *(when optional is/is not declared for types where it 
         *shouldn't/should be)
         */
    }
}

您有两个选项:

选项1

可以使用var-args实现超级类中的static factory method

public static SuperClass newInstace(int...parameters) {
     SuperClass superClass = null;
     if(parameters.length == 2) {
          if(parameters[1]>=5) {//instantiate a subclass based on a value
             super = new SubClass1(parameters[0],parameters[1]);
          } else {
             super = new SubClass2(parameters[0],parameters[1]);
          }
     } else if(parameters.length == 3) {
           if(parameters[2]>=5) {
             super = new SubClass3(parameters[0],parameters[1],parameters[2]);
          } else {
             super = new SubClass4(parameters[0],parameters[1],parameters[2]);
          }
     } else {
          throw new IllegalArgumentException("Invalid number of parameters passed to newInstance. Expected number of parameters [min = 2, max = 3]")
     }
     return superClass;
}

选项2

另外,您可以超载newInstance方法,并且具有2个参数,另一个采用3个参数。

如下所述,这两种方法都有利弊:

  1. 当您期望在现有子类中引入新字段的频率或期望与现有领域更多的领域引入新的子类的频率极低时,方法1是一个更好的选择。
  2. 当您期望在现有子类中引入新字段的频率或您期望与现有领域更多的领域引入新的子类的频率相对较高时,方法2是一个更好的选择。将导致一个非常大的方法。

您可以通过配置字符串或配置对象(例如属性),其具有与实现需求一样多的细节。可能是1或100个论点。但是,对于呼叫者,有一个也是唯一的包装参数。

它取决于。听起来您的超类静态方法返回SuperClass实例。如果是这样,您可以根据附加参数返回不同的子类实例。

考虑一个抽象类Vehicle。现在,两个抽象类SingleDeckVehicleMultiDeckVehicle扩展了Vehicle,让前两个参数是车轮的数量和座椅数,而附加参数为车辆的甲板数。然后,甲板的数量将很重要,如果他/她想要一个SingleDeckVehicle实例,您的客户确实将通过1作为第三个参数。

但是,您还可以创建另一种静态方法。假设您已经定义了静态方法Vehicle.getVehicle(int wheelNo, int seatNo, int deckNo)。您也可以定义另一个:Vehicle.getSingleDeckVehicle(int wheelNo, int seatNo)返回Vehicle.getVehicle(wheelNo, seatNo, 1);

一个类似的例子是边界案例:http://docs.oracle.com/javase/7/docs/api/javax/swing/borderfactory.html。我希望我正确理解您的问题...

最新更新