Initialize a collection within an object?

2019-03-13 19:31发布

If an object has a property that is a collection, should the object create the collection object or make a consumer check for null? I know the consumer should not assume, just wondering if most people create the collection object if it is never added to.

标签: c# .net oop
10条回答
老娘就宠你
2楼-- · 2019-03-13 20:05

I prefer to create the collection upon instantiation, provide add/remove methods in the containing object, and if needed, provide addAll/removeAll methods for adding and removing entire external collections. I never allow the collection object to be accessed for get or set by the caller - I may, however, provide a getter that returns a copy (poss. immutable) of the collection.

I have also had copy-on-write implementations which provide an immutably wrapped view, since any mutation will create a new underlying collection.

查看更多
祖国的老花朵
3楼-- · 2019-03-13 20:11

I create an empty collection. Sure, it has a slight memory cost, but not a large enough one to be concerned about it.

查看更多
Anthone
4楼-- · 2019-03-13 20:12

You can also use the "Lazy initailizer" pattern where the collection is not initialized until (and unless) someone accesses the property getter for it... This avoids the overhead of creating it in those cases where the parent object is instantiated for some other purpose that does not require the collection...

   public class Division
    {
        private int divId;
        public int DivisionId { get; set; }

        private Collection<Employee> emps;
        public Collection<Employee> Employees
        { get {return emps?? (emps = new Collection<Employee>(DivisionId));}} 
    }

EDIT: This implementation pattern is, in general, not thread safe... emps could be read by two different threads as null before first thread finishes modifying it. In this case, it probably does not matter as DivisionId is immutable and although both threads would get different collections, they would both be valid. Whne the second thread fihishes, therefore, emps would be a valid collection. The 'probably' is because it might be possible for the first thread to start using emps before the second thread resets it. That would not be thread-safe. Another slightly more complex implementation from Jon SKeet is thread-safe (see This article on SIngletons for his example/discussion on how to fix this.

查看更多
爱情/是我丢掉的垃圾
5楼-- · 2019-03-13 20:12

While what I say here is not universal by any means, I think collections are mostly suited as private properties which should be accessed by public methods on the object itself. Therefore, the object is responsible to create and destroy them as needed. This way, it won't be possible to set it to null from external code.

查看更多
三岁会撩人
6楼-- · 2019-03-13 20:12

I've tried both ways and I am usually happy when I find I have already instanciated it. Alternatively you could hide the the implementation from the consumer behind a set of methods that can handle the check for you.

查看更多
一夜七次
7楼-- · 2019-03-13 20:15

If the collection is an intrinsic part of the object, then the object should generally create it (and not allow the user to set it to a different collection instance).

查看更多
登录 后发表回答