Strong-Type Collection

来源:互联网 发布:与人工智能有关的股票 编辑:程序博客网 时间:2024/05/16 14:07

在泛型类出现之前:

 

实现ICollection、IEnumerable、IList接口的目的是要创建对象与.NET Framework之间兼容互通的管道,但这样的做法却会引发另一个问题,那就是这些接口都使用object作为Collection中的元素类型,而.NET Framework所有的对象皆是源自object,这代表着用户可以放入所有对象于Collection对象中。往好的方面着眼,这是最具扩展性的设计方式,但往坏的方面着眼时,这种设计过于松散,容易令用户产生混淆与误用。那该如何兼顾两者呢?答案是Strong-Type Collection,下面是MyList集合的Strong-Type版本。

public class MyList: IList{   priavte ArrayList list;   public MyList(){     list = new ArrayList();   }         #region IList Members   public bool IsReadOnly   {     get{       return false;     }   }   object IList.this[int index]   {     get{       return list[index];     }     set{       list[index] =value;     }   }   public string this[index]   {     get{       return (string)list[index];     }     set{       list[index] =value;     }   }      public void RemoveAt(int index)   {     list.Remove(index);   }   public IList.Insert(int index,object value)   {     list.Insert(index,value);   }   //strong-type Insert   public void Insert(int index,string value)   {     ((IList)this).Insert(index,value);   }   void IList.Remove(object value)   {     list.Remove(value);   }   //strong-type remove   public void Remove(string value)   {     ((IList)this).Remove(value);   }   bool IList.Contains(object value)   {     return list.Contains(value);   }   //strong-type contains   public bool Contains(string value)   {     return ((IList)this).Contains(value);   }   int IList.IndexOf(object value)   {     return list.IndexOf(value);   }   //strong-value contains   public int IndexOf(string value)   {     return ((IList)this).IndexOf(value);   }   int IList.Add(object value)   {     return list.Add(value);   }   //strong-type Add.   public int Add(string value)   {     return ((IList)this).Add(value);   }   public void Clear()   {     list.Clear();   }   public bool IsFixedSize   {     get{       return false;     }   }   #endregion   #region ICollection Members   public bool IsSynchronized;   {     get{       return list.IsSynchronized;     }   }   public int Count{     get{       return list.Count;     }   }   public void CopyTo(Array array,int index)   {     list.CopyTo(array,index);   }   public object SyncRoot   {     get     {       return list.SyncRoot;     }   }   #endregion   #region IEnumberable Members   public IEnumberator GetEnumberator()   {      return list.GetEnumerator();   }   #endregion }


 MyList将实现IList接口的函数与属性声明为IList接口的明确实现,一旦声明成明确实现,用户就无法在不转型成IList接口情况下调用这些函数,这可以有效防止误用的情况发生。只是没有这些函数的话,用户就无法操作此Collection了,因此MyList提供了另一组同名称的函数与属性给用户,并且以string为操作类型,这样就不会发生误用的情况了。是的!这样一来MyList的实现变得复杂了不少,但在鱼与熊掌要兼得得愿望下,付出点代价是难免的。

 

CollectionBase

针对设计人员对Strong-type Collection的需求,.NET Framework中提供了一个名为CollectionBase的基础类,其实此类的实现如同MyList类一样,提供了IList接口的基本实现,设计人员可以继承此类来加上额外的同名函数。

public class MyList2:CollectionBase{  public void Insert(string s)  {    List.Add(s);  }     public void Remove(string s)  {    List.Remove(s);  }  public void Add(string s)  {    List.Add(s);  }  public bool Contains(string s)  {     return List.Contains(s);  }  public int IndexOf(string s)  {    return List.IndexOf(s);  }}


 

原创粉丝点击