Objective C # Reading Notes-entry 26: avoid returning references to internal class objects

Source: Internet
Author: User

You may think that the read-only attribute can only be read, and the caller cannot change the attribute value. This is not the case in all cases. Let's look at the following example:

 1       Public   Class  Mybusinessobject  2   {  3           Private List < String > Listofdata = New List < String > { "  COM1  " ,"  Com2  " , "  Com3  " , "  Com4  " , "  Com5  " , "  Com6  "  };  4  5           Public List < String > Data  6   {  7               Get { Return  Listofdata ;}  8   }  9           //  Other details omitted  10 }

We wanted the data attribute of the mybusinessojbect class to be read-only, but now we can change its internal status through the reference it returned:

 
1Mybusinessobject BO =NewMybusinessobject ();2//Access set3List <String> Stuff =Bo. Data;4//This is not the case, but it is allowed here.5Stuff. Clear ();

Obviously, this is not what we want. We create a strict interface for the class and then let the user use the object through the interface. We do not want users to access or modify the internal status of objects without knowing them. We haveFour policies can prevent this type of internal data structure from being intentionally or unintentionally modified.:

  • Value Type
  • Constant type
  • Interface
  • Wrapper)

 

1. Value Type

When customersCodeWhen a member of the value type is accessed through the attribute, a copy of the value type is actually returned, and any modification to the copy will not affect the internal status of the object.

 

2. Constant type

Constant types are also safe (see:Valid tive C # Note 20). We can safely return strings or other constant types in the type, and the customer Code cannot make any changes to them, because we can also ensure the security of the internal state of the type.

 

3. Define Interfaces

Define an interface to restrict customers' access permissions to internal data members in some functions (see:Valid tive C # note 22). When creating a class, you can create a set of interfaces to support the subset of class functions. By exposing the class function to the outside world through the interface, you can avoid any intentional or unintentional changes to internal data. The list <t> function provided by the ienumerable interface is an application of this policy.

 

4. Provide the package object

Provides a package object that exposes only the package, thus limiting access to the object.System. Collections. objectmodel. readonlycollection <t>Type is a standard read-only encapsulation of the set to keep the data in the set read-only:

 1       Public   Class  Mybusinessobject  2   {  3           Private List < String > Listofdata = New List < String > { " COM1  " , "  Com2  " , "  Com3  " , "  Com4  " , "  Com5  " , "  Com6  " };  4   5           Public List < String > Data  6   {  7               Get { Return  Listofdata ;}  8   }  9   10          Public Readonlycollection < String > Collectionofdata  11   {  12               Get { Return   New Readonlycollection < String > (Listofdata );}  13   }  14           // Other details omitted  15 }

 

Section

If the reference type is exposed to the outside world through the public excuse, the user of the object can modify the internal structure of the object by bypassing the methods and attributes we define. This violates our general intuition and may lead to common errors. With this in mind, you should modify the interface exposed by the class. If you simply return internal data, you are granted the permission to access internal members. The Customer Code can call any available methods in the member. You can use interfaces, wrapper objects, or value types to provide private data to external entities to restrict external access to such data.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.