A way to make my code safe? - Private and public

A class instance has a private ArrayList. The instance is responsible for storing the data stored in the array.

private ArrayList<SomeDataStructure> myPrivateArrayList; 

However, when another module requests data, this instance of class A must pass the data to the person who requests it, so there is a public function in class A:

 public ArrayList<SomeDataStructure> getMyPrivateArrayList (); 

My question is, how do I implement this function so that I can guarantee that those who receive the List array through this public function cannot change it (i.e. the read-only return value)?

Thanks in advance!

+4
source share
4 answers

I would suggest doing this instead (if you are allowed in your situation):

 private ArrayList<SomeDataStructure> myPrivateArrayList; public List<SomeDataStructure> getMyPrivateList () { return Collections.unmodifiableList(myPrivateArrayList) } 

Note that the open data structure is of type List instead of ArrayList . I think (generally speaking) the public interface of a class should not return specific types, but rather should return interfaces. It simplifies tasks like this, and also reduces the number of dependencies that one class has on the implementation of another class.

+6
source

Instead of the return type ArrayList<SomeDataStructure> use List<SomeDataStructure> . Then you can use the java.util.Collections.unmodifiableList(...) utility method to create a read-only list view:

 public List<SomeDataStructure> getMyPrivateArrayList() { return Collections.unmodifiableList(myPrivateArrayList); } 

Another option is to return a copy of the list:

 public ArrayList<SomeDataStructure> getMyPrivateArrayList() { return new ArrayList<SomeDataStructure>(myPrivateArrayList); } 

(There are other options, but these are the most common approaches.)

But keep in mind that if SomeDataStructure is mutable, then callers from any of the above objects can still mutate any of the objects in your list. (That is, they can do something like obj.getMyPrivateArrayList().get(0).setProp(null) .)

0
source

In your getMyPrivateArrayList () function, do the following:

 public List<SomeDataStructure> getMyPrivateArrayList(){ return Collections.unmodifiableList(myPrivateArrayList); } 

Collections.unmodifiableList(someList) returns a read-only list.

<h / "> In your calling class, if you try to modify the returned list, you will get a runtime error, for example.

If you follow these

 List<SomeDataStructure> readOnlyList=getMyPrivateArrayList(); readOnlyList.add(new SomeDataStructure()); 

You will get the following error:

 Exception in thread "main" java.lang.UnsupportedOperationException at java.util.Collections$UnmodifiableList.add(Collections.java:1160) at MainClass.main(MainClass.java:14) 
0
source

Do you need to get a list? Or can you just forward some access lists to the list? You can define some public functions, such as get(index) , that simply call the equivalent methods on your list and return the result. This is most likely what you want to do, because it only gives people access to the methods you choose, and you don’t need to give them the list itself or drop CPU cycles by copying the data into read-only structures.

0
source

Source: https://habr.com/ru/post/1384440/


All Articles