Why collections need to be created this way:
List list = new Linkedlist(); Why is it not desirable to write LinkedList right away?
Why collections need to be created this way:
List list = new Linkedlist(); Why is it not desirable to write LinkedList right away?
This is an important technique for writing good OOP code. The idea is that your code becomes less dependent on the specific implementations of the modules used. You can write LinkedList , ArrayList , CopyOnWriteArrayList , etc. to the same List interface.
At the same time, by announcing the list in this way, we can be sure that if it suddenly becomes necessary to change the implementation from LinkedList to ArrayList , then we will have to change only the line with the constructor call. For example, LinkedList , unlike ArrayList , in addition to the List interface implements another Queue (queue) in which there is a push method.
List list = new LinkedList(); LinkedList linkedList = new LinkedList(); ArrayList arrayList = new ArrayList(); list.add("ok"); linkedList.add("ok"); arrayList.add("ok"); list.push("error"); //ΠΎΡΠΈΠ±ΠΊΠ° ΠΏΡΠΈ ΠΊΠΎΠΌΠΏΠΈΠ»ΡΡΠΈΠΈ, Ρ ΠΈΠ½ΡΠ΅ΡΡΠ΅ΠΉΡΠ° List Π½Π΅Ρ ΡΠ°ΠΊΠΎΠ³ΠΎ ΠΌΠ΅ΡΠΎΠ΄Π°. linkedList.push("Ok"); arrayList.push("error"); //ΠΎΡΠΈΠ±ΠΊΠ° ΠΏΡΠΈ ΠΊΠΎΠΌΠΏΠΈΠ»ΡΡΠΈΠΈ, ArrayList Π½Π΅ ΠΈΠΌΠΏΠ»Π΅ΠΌΠ΅Π½ΡΠΈΡΡΠ΅Ρ Queue In the example above, even though we have an instance of LinkedList in the list , we cannot call methods specific to it. The List interface forces us to use only list methods, not queues or anything else.
Using an interface instead of an implementation is not so important when a collection (or other object) is created and used within one method, but it is important if it is somehow transferred to other modules.
Read more about this topic about SOLID .
List is an interface, and LinkedList is a class that implements this interface. interface List contains the basic methods add(), get(), remove() and so on, if you have enough of these methods you can write List<...> list = new LinkedList<..>() , if you need to use add. methods that exist in the LinkedList class should then be written as LinkedList<...> linkedList = new LinkedList<...>();
List list = new Linkedlist(); not Linkedlist list = new Linkedlist(); - Alexey ShimanskyList is an interface, as I recall. LinkedList is a class that implements this interface. LinkedList cannot be an interface, because new Linkedlist() creates an instance, and an instance of the interface cannot be created. Therefore, the statement "LinkedList is a List interface" is incorrect. - Qwertiy β¦If you want to pull up from the base some entities that will be saved in your repositories as different sheets, you will have to check the model at least every time to see what kind of sheet there is. And by initializing through the interface you simplify your life for a few seconds. This is one of the examples, and there are many more such. For example, if you changed a sheet from array to linked in a model, but in services or controllers it pulled entities into an array, then youβll have to manually change everything, but initiating this need from the interface disappears.
Because this is the ideology of Java, declare variables with interface types.
But that will change soon:
https://habrahabr.ru/post/280188/
https://habrahabr.ru/post/280075/
val list = new Linkedlist(); List type, then the variable must be declared as List - as close to the base type as possible. - IgorSource: https://ru.stackoverflow.com/questions/513092/
All Articles