I am learning java. I am trying to use composite design pattern. I am trying to use following logic. ( Don't laugh I know it is very basic :) )
Item -> interface
Folder -> class
File -> class
In folder class, can I create an arraylist of Item to store files information?
ArrayList<Item> info = ArrayList<Item>();
Or should I use Folder Arraylist?
ArrayList<Folder> info = ArrayList<Folder>();
I don't know if interface can store real data as there is no variable just function definitions.
Thanks for helping a newbie :)
You can do both (with some syntactic correction)
List<Item> info = new ArrayList<Item>();
With regards to this comment:
I don't know if interface can store real data as there is no variable just function definitions.
Interfaces do more than provide function definitions. Most importantly, they define a type. info
, declared as above, is a list of objects of type Item
. Those objects can most certainly store data.
As an example, consider the following:
interface Item { ... }
class Folder implements Item { ... }
Item it = new Folder();
Now, it
refers to an instance of Folder
, which is an Item
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With