First of all, I would like to know if I am doing some OO work using 'best practices' (or even correct at all).
Here are two interfaces which belong to a framework I have written:
Notice that there are no setter methods. I think that the way the Node and Hierarchy objects are populated is independant of how the objects should be used.Code:public interface Hierarchy { public HierarchyNode getRootNode(); public PathFactory getPathFactory(); } public interface HierarchyNode<V> extends Comparable { public V getValue(); public Hierarchy getHierarchy(); public Collection<HierarchyNode<V>> getChildNodes(); public int getChildCount(); public HierarchyNode<V> getChildNode(V value); public HierarchyNode<V> findNode(HierarchyPath path) throws UnattachedNodeException; public HierarchyPathElement getPathElement(); }
Here is the implementation, which is a basic getter/setter implemation. This implementation basically sets up the Hierarchy in memory before it is used.
The important thing to note is that the getRootNode() returns a DefaultHierarchyNode instead of a HierarchyNode. I think this is correct because classes working with the default implementation should not have to typecase from the interface to the implementation.Code:public class DefaultHierarchy implements Hierarchy { protected DefaultHierarchyNode rootNode; protected DefaultPathFactory pathFactory; public DefaultHierarchy() { this.pathFactory = new DefaultPathFactory(); this.pathFactory.setHierarchy(this); } public void setRootNode(DefaultHierarchyNode rootNode) { this.rootNode = rootNode; } public DefaultHierarchyNode getRootNode() { return this.rootNode; } public DefaultPathFactory getPathFactory() { return this.pathFactory; } } public class DefaultHierarchyNode<V> implements HierarchyNode<V> { ... }
Do people agree with me on this? If not, tell me why. Otherwise preceed to the Spring question.
Spring Question:
I used the following spring configuration:
and I get a NotWritablePropertyException for the 'rootNode' property of BaseHierarchy. This seems weird because I am not specifying the interface anywhere. If I remove "implements Hierarchy" line of code from the BaseHierarchy class, then I do not receive the Exception.Code:<bean id="hierarchy" class="gov.noaa.gdsg.hier.base.BaseHierarchy"> <property name="rootNode"><ref bean="theRootNode" /></property> </bean> <bean id="theRootNode" class="gov.noaa.gdsg.hier.base.BaseHierarchyNode"> <property name="value"><value>Media</value></property> </bean>
What am I doing wrong :?:


Reply With Quote