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)
With regards to this comment:
Interfaces do more than provide function definitions. Most importantly, they define a type.
info, declared as above, is a list of objects of typeItem. Those objects can most certainly store data.As an example, consider the following:
Now,
itrefers to an instance ofFolder, which is anItem.