tags:

views:

174

answers:

4

I have a class "MySet"

 class MySet
 {
   .......     
 }

This class will declare a reference to another type

(i.e)

class MySubSet
{
  ....
}

The purpose of the type "MySubset" is to supply "subset id" and a collection of integers to the type "MySet".

Which one of the followings is the correct implementation

(1)

  class MySet
    {
        int mySetID;
        MySubSet subset = new MySubSet();
        public int MySetID
        {
            get { return mySetID; }
            set { mySetID = value; }
        }
        public MySubSet MySubSet
        {
            get { return subset; }
            set { subset = value; }
        }
    }


   class MySubSet
    {
        int subsetID;
        List<int> subset = new List<int>();

        public List<int> SubSet
        {
            get { return subset; }
            set { subset = value; }
        }

        public int SubSetID
        {
            get { return subsetID; }
            set { subsetID = value; }
        }
    }

(2)

   class MySet
   {
      int mySetID;
       AnotherSubSet subset = new AnotherSubSet();
       public int MySetID
       {
          get { return mySetID; }
          set { mySetID = value; }
       }

       public AnotherSubSet MySubSet
       {
          get { return subset; }
          set { subset = value; }
       }
}




class AnotherSubSet : List<int>
    {
        int subsetID;
        List<int> lst = new List<int>();

        public int SubSetID
        {
            get { return subsetID; }
            set { subsetID = value; }
        }
    }

If both are worst design consideration help me to implement the one that i could follow.

+2  A: 

MySet doesn't look like a collection to me. It's just a class.

I'd rename it to ´MyEntity´or something like that.

List<MyEntity> mySet = new List<MyEntity>();
Natrium
+2  A: 

From all the information you've provided, I would do this:

 public class MyEntity
 {
       public int ID { get; set; } //  shortcut
       public List<int> Numbers = new List<int> { get; set; } // shortcut
 }

Sorry, I don't have /Net3.0 to hand so can't check the constructor of the list with the shortcut get/set but its the theory that counts...

ck
where's the ID of the subset?
Natrium
@Natrium - it could be easily added if necessary, but looks to be pointless as its currently a 1-to-1 relationship
ck
that could be a quick and dirty approach, since the subsetID belongs to the subset and not to MyEntity
Natrium
+2  A: 

The first version is better (as improved upon by ck) - use composition instead of inheritance. You are advised not to add properties to collections, which is effectively what you're doing in version 2. Collections should contain their items only. Someone else may be able to expand on the reasons for this, as I am not an expert, but it does cause serialization problems.

darasd
+1 for "use composition instead of inheritance"
Natrium
+1  A: 

Number 2 is better, use inheritence not composition for this pattern, - because it ifundementally is a collection. It does not contain a collection. Inheritance gives you all the functionality of the base class without the need to write pass-through functions. If you want to add a new item to the collection, using composition, you either have to add a pass through method for the Add() method to class MySubSet:

class MySubSet    
{        
    int subsetID;        
    List<int> subset = new List<int>();        
    public List<int> SubSet 
    {            
        get { return subset; }             
        set { subset = value; }        
    }        
    public void Add(int i) { subset.Add(i); }  // pass through to subset.Add()
 }

or you have to use the following non-intuitive and confusing syntax...

MySet.MySubSet.SubSet.Add(67);

with inheritence, all you need is

MySet.MySubSet.Add(67);
Charles Bretana