Skip to main content

Chronicles of the Trove: Fixing by Deprefixing

Posted by evanx on June 17, 2007 at 7:42 AM PDT

I have one very simple rule for design, namely to create as many classes as possible! Let's name this "Evan's Rule of Simplicity by Multiplicity."

So I just noticed a simple cue to refactor out a class. Consider the following GUI construction.

public class MainView {
    JTabbedPane mainTabbedPane = new JTabbedPane();
    ...
    DefaultMutableTreeNode categoryRootTreeNode = new DefaultMutableTreeNode(new CheckBoxModel("All", false));
    DefaultTreeModel categoryTreeModel = new DefaultTreeModel(categoryRootTreeNode);
    JTree categoryTree  = new JTree(categoryTreeModel);   
    ...   
    public MainView() {
        categoryTree.setEditable(true);   
        ...
    }
    ...
}

You see we have a few related components categoryTree, categoryTreeModel and categoryRootTreeNode. They all start with the same prefix ie. category, so as to distinguish them from other trees.

So the clue is when seeing such prefixes, refactor them out to a class so as to "de-prefix" them, as follows.

public class CategoryTreeView {    
    DefaultMutableTreeNode rootTreeNode = new DefaultMutableTreeNode(new CheckBoxModel("All", false));
    DefaultTreeModel treeModel = new DefaultTreeModel(rootTreeNode);
    JTree tree  = new JTree(treeModel);
   
    Map treeNodeMap = new HashMap();
    Map feedTreeNodeMap = new HashMap();
   
    TreeCellRenderer renderer = new CheckBoxTreeCellRenderer();
    CheckBoxTreeCellEditor editor = new CheckBoxTreeCellEditor();
    ...
    public CategoryTreeView() {
        tree.setEditable(true);
        ...
    }       
    ...
}

So let's name this "Evan's Rule of Fixing by Deprefixing."

A further step might be refactoring this to extend something eg. JTree or DefaultTreeModel, rather than being a composition.

We hook up that class, to a now simpler MainView.

public class MainView {    
    JTabbedPane mainTabbedPane = new JTabbedPane();
    ...
    CategoryTreeView categoryTreeView = new CategoryTreeView();
    ...       
    public MainView() {
        ...
    }
    ...
}

So our increasingly complex system progressively devolves to a collection of increasingly simple classes. It's nice!

PS. Here is (another) sneak preview of this feed reader toy that's been distracting me of late, and the actual reason i wrote this blog entry, ie. as an excuse for this postscript, as will be the case with all entries in this series - I know, gutting.

PPS. That's from The Office when David Brent says, "Well, there's good news and bad news. The bad news is that Neil will be taking over both branches, and some of you will lose your jobs. Those of you who are kept on will have to relocate to Swindon, if you wanna stay. I know, gutting. On a more positive note, the good news is, I've been promoted, so... every cloud... You're still thinking about the bad news aren't you?"

Launch   (FeedTrove, 1M/350k, unsandboxed, Java6)

There seems to be the odd GUI feeze-up but i don't know why!? I'm doing every GUI-ish thing on the EDT as far as i can see. Personally i suspect JEditorPane! ;) Anyway, one can dispose the frame into the system tray, and pop it up again.

Related Topics >>