In order to be able to replace a specific implementation, it is generally known that
List<AnyType> myList = new ArrayList<AnyType>();
instead
ArrayList<AnyType> myList = new ArrayList<AnyType>();
This is easy to understand, so you can easily easily change the implementation from ArrayList to LinkedList or any other list.
Well ... it's all nice and nice, but since I cannot directly initiate the List, so I will need to enter
public List<AnyType> getSpecificList()
{
return new ArrayList<AnyType>();
}
which makes the previous template completely pointless. What if I now want to replace the LinkedList implementation with an ArrayList? It is required to change it to two positions.
Is it possible to have something like this (I know the syntax is completely wrong)?
public class MyClass<T>
{
Type myListImplementation = ArrayList;
List<T> myList = new myListImplementation<T>();
public List<T> getSpecificList()
{
return new myListImplementation<T>();
}
}
"ArrayList" "LinkedList", . , , " ". .
- ? ^
Atmocreations