Question

I am working in C# some exercises and I don't understand the big picture. If these should implement in Java I wodn't have problems. But I am new in C# so I mixed up things and I don't know how te structure should look like. I read some explanasions like: http://www.codeproject.com/Articles/15360/Implementing-IDisposable-and-the-Dispose-Pattern-P and http://msdn.microsoft.com/en-us/library/fs2xkftw.aspx

I have first class where I make LinkedList and itst type is node, so: LinkedList linkedlist = new LinkedList than I make some nodes in puti it in linkedlist by some methods also here is method print, which prints elements from linkedlist.

My "object" class is Node, where I have constructors, properties and so getters and setters. I have allso here virtula method print and I don't know what shoudl I put in this method, becuse this node class is just abstract class and here I must implement IDisposable methods:

 public void Dispose()  {}
 protected virtual void Dispose(bool disposing) {}

Here I don't undrsetend why should I use IDispose? In all examples I saw it has been used for access to some file and working with pictures. In some examples I saw:

 public interface IDisposable
 {
   void Dispose();
 }

and I don't know if I must put this in some new class, shuld I put it in the same class or even not implement it.

And than I make subclasses NodeLong and Nodestring which uses superclass node whre I put some new properties and setters and getters. I have also used print method. I also don't understand genercis.

Linearcollection class:

public class LinearCollection{
     void Main(string[] args){
         LinkedList<Node> linkedlist = new LinkedList<Node>
          //calling methods for adding some elements, deleting, getting some elements
     }
     //implemented methods

     public void Print(){
        foreach( NodeElement item in linkedList){
              // what to do??
        }
     }

My Nodeelemts class:

 public abstract class Node:{

     private bool _disposed;
     //constructors
     //properties, set, get ex:
     private Object data;
     public Object _data{
        get{
            rethurn this._data;
        }
        set{
            this._data=value;
        }
     }

     public void Dispose()
    {
        Dispose(true);
        GC.SuppressFinalize(this);
    }

    protected virtual void Dispose(bool disposing){
        if(!_disposed){
            if(disposing){
                //disposed managed resources
            }
            //
        }
        _disposed = true;
        //
    }

     virtual public void Print(){
        //what to implemetn?
     }
 }
 //stil the same file
 public class NodeString : Node{
    //prop
    public string typeOfElement{ get set ...}
   //new constructor where is involved new property
   public overrride void Print()
   {
        Console.WriteLine(_data.ToString());
        base.Print();
    }
 }

 public class NodeLong : Node{
    //prop
    public long typeOfElement{ get set ...}
   //new constructor where is involved new property
   public overrride void Print()
   {
        Console.WriteLine(_data.ToString());
        base.Print();
    }
 }

If I make NodeLang: IClonable than I cant make NodeLang:Node.

My instructions says that I must use IClonable methods in both subclasses, I don't know how should I use it? Also use IDisposable in class Node, how? I hope I am understandible?

So the finale question is how it should look like my class, subclass, IDisposable and ICloneable?

Was it helpful?

Solution

There are times when it could make sense for a class which implements IDisposable to have a cloning method, in which case whatever code invokes the cloning method the object would be responsible for ensuring that Dispose will get called the new copy (either by calling Dispose itself, or handing off the responsibility to some other code). There are also occasions when it makes sense to have a a container class with a Dispose method that calls Dispose on each member stored therein, though in most such situations the purpose of the container class would center around such disposal and there would be no reason for it to support any kind of cloning.

Unless the Node class will be exposed to the outside world, I don't see much purpose for having it implement IDisposable. Even if the class which holds the linked list is a disposal aggregator (something which accepts objects whose useful lifetime will be shorter than its own and then cleans them up when its own Dispose method is called) and every node but the last will not only hold something that requires cleanup, but will also know of another node. Having a node call Dispose on the next node would result in unnecessary recursion, causing a crash if there are too many nodes. An alternative would be to have the Node class implement a Node DisposeAndReturnNext() method which would cause the Node to dispose of all resources it knows about and then return a reference to the next node. A caller could then use something like:

while(nodeToDispose != null)
  nodeToDispose = nodeToDispose.DisposeAndReturnNext();

Here it would be clear that the DisposeAndReturnNext method would not be expected to dispose the next node (and its descendants), even though it would know that they need disposal. While it would be possible for Node to define a Dispose method which includes a loop like the above, having the first list item's disposal handled a public Dispose method and having every other item's disposal handled by a loop like the above seems rather icky. Better to have one loop, in the enclosing class, which handles disposal for everyone.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top