We help IT Professionals succeed at work.

singleton pattern

wunyu
wunyu asked
on
279 Views
Last Modified: 2010-03-31
Hi,

I have an abstract class Node and 2 classes with extend it, called DNode and EmptyNode. EmptyNode is a singleton class with a public method called isEmpty().  DNode class also has a method called isEmpty().  Both these node classes have variables next and previous.

I have another class called List, which has this in its constructor:

head = EmptyNode.getSingleton();
tail = EmptyNode.getSingleton();

both head and tail were declared to be of type Node.

I am trying to write a method called isEmpty in class List:

public boolean isEmpty { return head.next.isEmpty(); }

My compliler is complaining that it cannot execute the isEmpty() method:

List.java:19: cannot resolve symbol
symbol  : method isEmpty ()
location: class Node
                return head.next.isEmpty();

I know that you have to call EmptyNode.getSingleton.isEmpty() to call methods from the singleton class.  But I need a general method in List so that it shouldn't care if it was calling it on a regular Node or an EmptyNode.  how do I get around this required syntax for Singleton classes?  Thanks.
Comment
Watch Question

Commented:
Making two identically named methods in descendant classes is NOT the same as having that method in a base class. The method isEmpty cannot be accessed from an object of type Node - the method doesn't exist, exactly as the compiler says.

You need to put isEmpty in the base class, ie Node.
prcsn1 perfectly right you need to put isEmpty in your base class Node you can put it as abstract method.

Commented:

...

public abstract class Node {

   ...
   ...

    public abstract boolean isEmpty();

}


public class EmptyNode extends Node {
  ...
  public boolean isEmpty() { return true; } // supposed to be always empty :-))
  ...
}

public class DNode extends Node {
  ...
  public boolean isEmpty() { return false; } // supposed to be allways non-empty
  ...
}









Commented:
This one is on us!
(Get your first solution completely free - no credit card required)
UNLOCK SOLUTION

Commented:
OOPS! Typo. It should read:

 // Define a constant of type node  
  public static final Node EMPTY= new Node();

Commented:
I suggest another approach that can be useful in some cases (if you want to reuse nodes without calling too many "new") ... define the isEmpty() method in the Node class too, and override it in the descendant classes:

public class Node {
  ...
  public boolean isEmpty( return true; );
  ...
}

public class DNode {

  private int data;    // or "Object" or whatever you want
  private boolean fempty = true;  // default to an empty node

  // constructor + some data
  public DNode(int somedata)  {
    setData( somedata );
  }

  // "bare" constructor without data initialization
  // public DNode() {  }

  // a method to invalidate data carried by this node
  public void makeEmpty() {
    fempty = true;
  }
 
  // a method to assign soke data to the node
  public void setData( int somedata ) {
    data = somedata; // assign the data "carried" by this node
    fempty = false;    
  }

  // retrieve data
  public int getData() {
    if ( fempty ) throw new Exception("Trying to access data of an empty node");
    return data;
  }

  public boolean isEmpty( return fempty ); // override

}

Commented:
I think that my comment was the most relevant. Even if the poster never bothered to reply.
Unlock the solution to this question.
Join our community and discover your potential

Experts Exchange is the only place where you can interact directly with leading experts in the technology field. Become a member today and access the collective knowledge of thousands of technology experts.

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.