Announcement Announcement Module
Collapse
No announcement yet.
Implementing an Interface and read-only problems. Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Implementing an Interface and read-only problems.

    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:
    Code:
    public interface Hierarchy {
        public HierarchyNode getRootNode();
        public PathFactory getPathFactory();
    }
    
    public interface HierarchyNode<V> extends Comparable &#123;
        
        public V getValue&#40;&#41;;
        public Hierarchy getHierarchy&#40;&#41;;
        public Collection<HierarchyNode<V>> getChildNodes&#40;&#41;;
        public int getChildCount&#40;&#41;;
        public HierarchyNode<V> getChildNode&#40;V value&#41;;
        public HierarchyNode<V> findNode&#40;HierarchyPath path&#41; throws UnattachedNodeException;
        public HierarchyPathElement getPathElement&#40;&#41;;
    &#125;
    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.


    Here is the implementation, which is a basic getter/setter implemation. This implementation basically sets up the Hierarchy in memory before it is used.

    Code:
    public class DefaultHierarchy implements Hierarchy &#123;
        
        protected DefaultHierarchyNode rootNode;
        protected DefaultPathFactory pathFactory;
    
        public DefaultHierarchy&#40;&#41; &#123;
            this.pathFactory =  new DefaultPathFactory&#40;&#41;;
            this.pathFactory.setHierarchy&#40;this&#41;;
        &#125;
        public void setRootNode&#40;DefaultHierarchyNode rootNode&#41; &#123;
            this.rootNode = rootNode;
        &#125;
        public DefaultHierarchyNode getRootNode&#40;&#41; &#123;
            return this.rootNode;
        &#125;
        public DefaultPathFactory getPathFactory&#40;&#41; &#123;
            return this.pathFactory;
        &#125;
    &#125;
    
    
    public class DefaultHierarchyNode<V> implements HierarchyNode<V> &#123;
     ...
    &#125;
    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.

    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:
    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>
    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.

    What am I doing wrong :?:

  • #2
    Re: Implementing an Interface and read-only problems.

    Originally posted by mrxtravis
    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.
    From what you posted, I'm not sure how you got past compilation. getRootNode() must return the same type as the interface it implements.

    Comment


    • #3
      Re: Implementing an Interface and read-only problems.

      Originally posted by manifoldronin
      Originally posted by mrxtravis
      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.
      From what you posted, I'm not sure how you got past compilation. getRootNode() must return the same type as the interface it implements.

      DefaultHierarchyNode implements the HierarchyNode interface and DefaultHierarchyNode is what the implementation is returning. Therefor it is returning the same type as the interface.

      Comment

      Working...
      X