Have fixed parameters in Java constructor - possible?

て烟熏妆下的殇ゞ 提交于 2020-01-02 10:22:31

问题


I've defined an object in Java - as far as the Java is concerned, they're the same thing, but as far as the data which populates them is concerned, they can be one of three types (wildly named 1,2,3 with 0 for the "root").
What I'd really like to be able to do is have four constructors defined, as they need slightly different params for each type. I could do it with strategic nulls, but it seems like the wrong thing to do. What I'd love to have is something like:

public MenuNode(int type = 1, param1, param2, param3) {
    doStuffHere();
} 
public MenuNode(int type = 2, paramX, paramY) {
    doStuffHere();
}

and then call something along the lines of:

switch (toQueue.itemType) {

    when ITEM_TYPE_STATIC {
        MenuNode mn1 = new MenuNode(ITEM_TYPE_STATIC, param1, param2, param3);
    }

    when ITEM_TYPE_DYNAMIC {
        MenuNode mn2 = new MenuNode(ITEM_TYPE_DYNAMIC, paramX, paramY);
    }

}

etc etc.

I hope this makes some sort of sense - it's a bit out there, and Googling only comes up with references to public static void etc. If someone with a bit more experience/know-how with Java than me can take a look, eternal love and gratitude will be forthcoming.


回答1:


Another approach is to avoid using a constructor here, and create a static factory method which calls a private constructor:

class MenuNode {
    private MenuNode() {
        // Does nothing important
    }
    public static MenuNode createStatic(param1, param2, param3) {
         MenuNode result = new MenuNode();
         result.setItemType(ITEM_TYPE_STATIC);
         result.setParam1(param1);
         result.setParam2(param2);
         result.setParam3(param3);
         result.doStuffHere();
         return result;
    }
    public static MenuNode createDynamic(paramX, paramY) {
         MenuNode result = new MenuNode();
         result.setItemType(ITEM_TYPE_DYNAMIC);
         result.setParamX(paramX);
         result.setParamY(paramY);
         result.doStuffHere();
         return result;
    }



回答2:


If I understood your scenario right, you may want to thing about refactoring this approach.

Make MenuNode a base class, and inherit your subtypes from it (MenuNodeType1, MenuNodeType2 etc.). That way you'll get control over the single types but you can still throw them all into one collection.




回答3:


If I understand you correctly, you want the constraint that the value of the first argument to each constructor must match the constructor being called.

Simple:

public MenuNode(int type, param1, param2, param3) {
    if (type != 1) throw new IllegalArgumentException ("type must be 1")
    doStuffHere();
} 
public MenuNode(int type, paramX, paramY) {
    if (type != 2) throw new IllegalArgumentException ("type must be 2")
    doStuffHere();
}

If you have two or more types that have the same list of parameters associated with them, the validation would look more like this;

    if (type != 1 && type != 3) throw new IllegalArgumentException ("type must be 1 or 3");



回答4:


Consider using factory methods to construct your MenuNodes:

public MenuNode createTypeOneMenuNode(Object param1, Object param2, Object param3) {
    ...
}

public MenuNode createTypeTwoMenuNode(Object paramx, Object paramy) {
    ...
}



回答5:


You are probably looking for method overloading

I don't see why you need to add the type to the call -> if you call it with X parameters, that you know what type it should be., right?




回答6:


Another solution if your constructors become hard to distinquish is to use static factory methods because these can have descriptive names. But as Bobby mentioned this seems to be more a case for refactoring to multiple types.



来源:https://stackoverflow.com/questions/5634575/have-fixed-parameters-in-java-constructor-possible

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!