Writing the simplest code (Part2)


Turns out there was an even simpler way to write a concrete implementation of ICollection:



namespace Testing.Collections


{


    public class EmptyCollection<A> : ICollection<A>


    {


        public EmptyCollection() {


        }


 


        #region ICollection<A> Members


 


        public bool Add(A element) {


            return false;


        }


 


        public bool Empty {


            get {


                return true;


            }


        }


 


        #endregion


    }


}


I thought of this implementation after the conversation I had with Julien Covreur (concerning Weak/StrongReferences) regarding the Replace Type Code with Subclasses refactoring.  I’m not a big fan of either state or mutability of objects and i prefer to have both fall out of the system naturally through the use of inheritance.


Note: because there is no state and no mutability, this object is a prime candidate for the singleton pattern.  That, of course, can come at a later time.