I was told to design the code according to the following code example:
Class Dogs{ List<Dog> listOfDogs = new ArrayList<Dog>(); // Setters, getters... } Class Dog{ // Attributes and methods }
The question arises: why is it better to implement the class Dogsas a container for ArrayList objects Dog, and not just refer to a simple ArrayList. Is encapsulation considered?
Dogs
Dog
Basically, creating a class Dogsprovides you with an extra layer of indirection.
, (.. ) Dogs, . , , , , , , ( Dogs - , ), .
. , "", , API Dogs. , Dogs . List.
List
" API" Visitor. Dogs visit(), . "", Dog, Dogs. .
visit()
Dogs, Dogs , , . , releaseTheHounds() callEveryoneForDinner() ..
releaseTheHounds()
callEveryoneForDinner()
, , , List listOfDogs, ArrayList. , :
listOfDogs
ArrayList
List listOfDogs = new ArrayList<Dog>();
:
ArrayList listOfDogs = new ArrayList<Dog>();
, . "" ArrayList List. (, TreeList), , listOfDogs.
TreeList
, : :
List<Dog> listOfDogs = new ArrayList<Dog>();
, List Dog .
, - , , , . .
, Dog, , , . , , / . , Dogs. , , , .. , .
Class Dogs { List listofDogs = new ArrayList<Dog>(); public void AddDog(Dog newDog) { //check if valid } public List<Dog> getDogs() { //return readonly dogs collection } }
- mvc,
, . , ArrayList , , , desc.
, , , , - " ". - , , . , , .
, Dog - PooledObject, , PooledObject, .
, , . , . , , , , : push, remove, , .., Dog Object; .
, Dog, .
Source: https://habr.com/ru/post/1540374/More articles:Spring - "context: property-placeholder" - приоритет собственности - javaВ чем разница между объявлением статической константы и константы в неназванное пространство имен? - c++What is the easiest way to trim the values of a string []? - stringwrite.table with different number formatting for each column - formattinghome directory not created with custom resource added with chef - linuxCan I use pa_monad to provide an η extension? - polymorphismjQuery every error: Uncaught TypeError: cannot use the "in" operator to search for "18" in the div [data-role = page] - javascriptUIVIew / CALayer animation callback - iosAndroid adb shell ping - this version of ping should not run with privileges. Aborting - androidA faster way to create a nullified array in Scala - arraysAll Articles