Question: Composite Design Pattern Problem: The objective of this exercise is to implement the Composite design pattern. Steps: 1 . Create an interface called AbstractFile. This
Composite Design Pattern
Problem:
The objective of this exercise is to implement the Composite design pattern.
Steps:
Create an interface called AbstractFile. This is the highest level interface of all component. Define an empty method void ls in it It is going to be used to print out the entire content in a directory.
Create a class called File to implements AbstractFile. This the leaf node or menu item in slides The File class should have its constructor, a String instance variable called name.
You need to implement the ls method by printing out the file name.
System.out.printlnFile: name;
Create a class called Directory to implements AbstractFile. This is the composite class. It should have its constructor and a String instance variable called name.
Different from File class, we need to add an ArrayList called includedFiles to save all the files and subdirectories in it Also, we need a void addAbstractFile file method to add files and directories to it Implement this add method by adding the file to the ArrayList includedFiles.
The ls method in Directory class is different from the one in File. In addition to printing out the directory name, you need to add a for loop to traverse every item in the ArrayList includedFiles, and call its ls method.
Write a client class and a main function to demonstrate how they work. Add some files and subdirectories to the root directory, and add some files to the subdirectories. Then, print all of them by calling the ls method in the root directory.
What to turn in: Similar as E
Step by Step Solution
There are 3 Steps involved in it
1 Expert Approved Answer
Step: 1 Unlock
Question Has Been Solved by an Expert!
Get step-by-step solutions from verified subject matter experts
Step: 2 Unlock
Step: 3 Unlock
