开发者

Design: Use class or instance references?

Recently in a project I saw another developer call custom nodes from a JTree using the class name as a reference to then trigger the display of a Panel in the central area of a JFrame (constraint: one and only one Panel per node).

I can understand the use of class names in the tree as there's not much displaying going on. However, I'd tend to use an instance in the tree itself that could also animate a JPanel. What is the most widespread practise and in what cases would one approach be more appropriate than another?

Edit: Code added at request. It seems I misremembered things as there was a bit of decoupling.

The enum represents a sort of hardcoded registry for JPanels which are displayed when a manipulation is carried out by a user. This is what I mean by using actual class names.

public enum RightEnum {
    BLAH1(BLAH1.class, "BLAH1", "This is a custom JPanel"),
    BLAH2(BLAH2.class, "BLAH2", 开发者_JAVA百科"This is another custom JPanel")
    // etc

    private Class<? extends AbstractWidget> widgetClass;
    private String key; 
    private String label;


    RightEnum(Class<? extends AbstractWidget> widgetClass, String key, String label) {
        this.widgetClass = widgetClass;
        this.key = key;
        this.label = label;
    }

    public String getLabel() {
        return label;
    }

    public String getKey() {
        return key;
    }

    public Class<? extends AbstractWidget> getWidgetClass() {
        return widgetClass;
    }

    public static RightEnum getEnum(String key) {
        if(key == null) {
            return null;
        }
        for(RightEnum right : RightEnum.values()) {
            if(right.key.equals(key)) {
                return right;
            }
        }
        return null;
    }
}

Method from containing Swing class which recreates a custom JPanel from the enum.

private static AbstractWidget createWidget(Right right) throws Exception {
    RightEnum rightEnum = RightEnum.getEnum(right.getKey());
    Class<? extends AbstractWidget> widgetClass = rightEnum.getWidgetClass();
    AbstractWidget widget = (AbstractWidget) widgetClass.newInstance();
    widget.setRight(right);
    return widget;
}

Method inside a MouseAdapter/KeyListener which fetches some sort of model object:

private void doOpenView() throws Exception {
        NavigationTreeNode navNode = (NavigationTreeNode) CustomFrame.navigationMenu.getLastSelectedPathComponent();
        if (navNode.isLeaf()) {
            if(navNode.getObject() instanceof Right) {
                Right right = (Right) navNode.getObject();
                try {
                    CustomFrame.widgetContainer.createAndAddWidget(right);                  
                } catch (Exception ex) {
                    throw ex;
                }
            }
        }
    }

The actual JTree has nothing much interesting and simply recreates nodes from model classes.


It's still a little hard to follow, but I believe your approach is probably better. If the class instance stored can also do the displaying, that's a great method to use.

Whenever you add a layer of "Meta"--using reflection--you really should have a great reason to do so because it encrypts your code a little. The best use in general for reflection is probably loading objects from non-java text or xml files, and even then it helps to isolate the reflection parts inside a single factory method and return instantiated java objects that don't use any further reflection.

Be careful, however, of the words elegant and crappy. Often what people call elegant I'd call "More Cryptic", and crappy is usually also "More Cryptic".

Your goal should always be "More Readable", even if your code is a little longer or a little less fun to write. Actually I'd say "Fully Factored" is most important, followed closely by "More Readable" (which implies some level of brevity, too much code can be as hard to read as overly "elegant" code).

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜