Inner Classes For Tree

Just a trick:) Not static inner classes give the opportunity elegantly implement Tree structure.

Example (only immutable code is shown):

 interface INode
 { 
  INode getParentNode();
  Collection getChildrenNodes();
  Data getSomeNodeData();
 } 

class DefaultNode implements INode { // members, initialization and mutable code has been omited private class InnerNode extends DefaultNode { public INode getParentNode() { return DefaultNode.this;// outer class } }

public INode getParentNode() { return null;// or INode.ROOT if you use NULL object pattern... } public Collection getChildrenNodes() { return childrenNodes;// returns the Collection of InnerNodes } public Data getSomeNodeData() { return data; } }

-- KirillStepanosov


EditText of this page (last edited October 5, 2001) or FindPage with title or text search