Would you define a collection type and, if would, how?
If you frequently use in your program a collection which path would you prefer:
Type
List<T>
everywhere.Define class:
class TT:List<T> { }
Define class:
class TT { private List<T> _tt; // ... }
I think it's not very important, but after las开发者_如何学运维t project I started to think about it very often.
You should only create your own collection class if it will add additional functionality.
When you do create your own collection classes, you should inherit from the System.Collections.ObjectModel.Collection<T>
class, not List<T>
.
Inheriting this class allows you to override InsertItem
and RemoveItem
and run custom logic when the collection is modified. (This is not possible when inheriting List<T>
)
You can use any option you specified depending on what you need:
(1) if you need to use list and type of the collection won't be changing.
(2) if you want to extend your collection with additional functionality then your should use inheritance.
(3) if you need to create additional level of the abstraction. In the this case your collection would be only implementation detail, e.g. you will be able to change list on the set in the future.
If I just need a list, I use 1. If I want to add additional methods and limitations, I will define 2.
Don't subclass List<>
-- that's almost invariably a symptom of incorrect abstraction. Let a List be a list, nothing more. I don't have any idea why you'd want to use your 3rd option.
Like everything, there are tradeoffs. The benefit of using a wrap class (encapsulating) your list is to militarize access to it. It means a lot of work:
- You will have to add specific methods to add and remove from the list
- You will have to add the iterator methods and might end up modifying syntax when iterating.
All these costs must serve a purpose. You'd do it if you want to intercept insertions and removals from the list, or if you want to create your own methods to filter the list and such. Maybe you need a immutable list. Make sure you have enough reasons to go through the trouble of creating the wrapper class.
Finally, as SLaks has put, it's better to inherit Collection<T>
than to wrap around a list. I don't know the real differences, but I'd with this alternative.
精彩评论