Results 1 to 3 of 3

Thread: Implementing an Interface and read-only problems.

  1. #1
    Join Date
    Apr 2005
    Posts
    7

    Default 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. #2
    Join Date
    Oct 2004
    Location
    Herndon, VA, US
    Posts
    648

    Default Re: Implementing an Interface and read-only problems.

    Quote 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.
    --Jing Xue

  3. #3
    Join Date
    Apr 2005
    Posts
    7

    Default Re: Implementing an Interface and read-only problems.

    Quote Originally Posted by manifoldronin
    Quote 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.

Similar Threads

  1. Replies: 1
    Last Post: Oct 27th, 2011, 06:58 AM
  2. Martin Fowler's article & big problems
    By quartz in forum Meta
    Replies: 11
    Last Post: Aug 3rd, 2006, 10:50 AM
  3. Spring code remarks
    By Alarmnummer in forum Architecture
    Replies: 18
    Last Post: Apr 7th, 2005, 07:17 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •