none
Encapsulation and Getter methods RRS feed

  • Question

  • Hi,

    I have a question with regard to encapsulation.  As I know, encapsulation enables to hide the implementation details using private/protected data members and provides public methods and properties to operate on the data.  The idea here is to prevent the direct modification of the data members by the class consumers.

    But I have a concern with the property getters or other public methods which return private/protected data members.  For ex: if I have class like this

    public class Inventory
    
    {
       private List<Guitar> guitars = new List<Guitar>();
    
       public void AddGuitar(string serialnumber, string price)
       {
            Guitar guitar = new Guitar(serialnumber, price);
            guitars.Add(guitar);
       }
    
       public List<Guitar> GetGuitars()
       {
            return guitars;
       } 
    }

    Now if the Inventory class consumer calls GetGuitars, he is going to get the list of guitars being maintained in the Inventory class.   Now the consumer can modify the list, like delete/add/modify the items.  For me it looks like we are not encapsulating.  I think that I should be returning a copy of the Guitar list items in the GetGuitars().   What do you think?.

    Is my understanding of the encapsulation right?.

     


    Ravi

    Saturday, September 29, 2012 3:10 PM

Answers

  • If you run the Code Analysis (or FxCop) on that code I believe it will warn you that is a bad idea. You should return a copy of the internal list, not a reference to it. Now in reality would you want that performance hit to avoid the obvious leak...it's a choice you make. You could also use a ReadOnlyCollection.


    http://pauliom.wordpress.com

    • Marked as answer by RaviAustin Saturday, October 20, 2012 4:22 PM
    Friday, October 12, 2012 9:02 AM

All replies

  • Encapsulation is a way to hide the behavior and attributes from outside clients. Inventory can have a private method which is executing some business logic to construct guitar list. Outside client doesn't have access to this business logic. 

    In your case GetGuitars is made public to clients so that they can consume it. Thus whatever is public becomes visible to client. In your case Guitar is public object, and you decided to share it with your clients.

    Other way you can use is have a marker interface IGuitar. Share this interface with the clients. Interface will have GetMethods on private property. 

    Friday, October 5, 2012 11:06 AM
  • If you run the Code Analysis (or FxCop) on that code I believe it will warn you that is a bad idea. You should return a copy of the internal list, not a reference to it. Now in reality would you want that performance hit to avoid the obvious leak...it's a choice you make. You could also use a ReadOnlyCollection.


    http://pauliom.wordpress.com

    • Marked as answer by RaviAustin Saturday, October 20, 2012 4:22 PM
    Friday, October 12, 2012 9:02 AM
  • pkr2000, thanks for the answer.  I am happy my understanding coincides with what you answered.  I also like your suggestion.

    Thanks


    Ravi

    Saturday, October 20, 2012 4:23 PM