File Directory :

In this example, the composite design pattern is used to represent data in a hierarchy. Each TextFile’s size and the total size of the Directory can be easily shown using this pattern.

In the main method of the Application class, Directory and TextFile classes are initiated. Calling the addFile method of the Directory object, the TextFile objects are added in the files variable of the Directory object. Later, by calling the getSize method of the Directory object from the main method of the Application object, the size of the total TextFile objects is calculated and displayed.

The generated java code of the file directory case study is below.

Composite.pdf