Created
April 24, 2022 10:40
-
-
Save devrath/dbc676adf08012a8394bd121be8096fa to your computer and use it in GitHub Desktop.
This snippet represents the composite design pattern
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| public class CategoryDramaMoviesFolder implements FolderDetailsComponent { | |
| @Override | |
| public void printFolderDetails() { | |
| System.out.println("Folder Details: Category drama movies"); | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| public class GamesFolder implements FolderDetailsComponent { | |
| @Override | |
| public void printFolderDetails() { | |
| System.out.println("Folder Details: Game's"); | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| public class JurassicParkMovie implements FolderDetailsComponent { | |
| @Override | |
| public void printFolderDetails() { | |
| System.out.println("File Details: Jurassic Park"); | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| public class MovieDemo { | |
| public static void main(String[] args){ | |
| // Parent | |
| ParentFolder parentFolder = new ParentFolder(); | |
| // First level children | |
| SoftwaresFolder softFolder = new SoftwaresFolder(); | |
| MoviesFolder moviesFolder = new MoviesFolder(); | |
| GamesFolder gamesFolder = new GamesFolder(); | |
| // Second level children | |
| JurassicParkMovie jurassicParkMovieFile = new JurassicParkMovie(); | |
| CategoryDramaMoviesFolder categoryDramaMoviesFolder = new CategoryDramaMoviesFolder(); | |
| // Add second level children | |
| moviesFolder.addItems(jurassicParkMovieFile); | |
| moviesFolder.addItems(categoryDramaMoviesFolder); | |
| // Add first level children | |
| parentFolder.add(softFolder); | |
| parentFolder.add(moviesFolder); | |
| parentFolder.add(gamesFolder); | |
| // Initiate print | |
| parentFolder.printFolderDetails(); | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| public class MoviesFolder implements FolderDetailsComponent { | |
| private ArrayList<FolderDetailsComponent> comp = new ArrayList<>(); | |
| public void addItems(FolderDetailsComponent component) { | |
| comp.add(component); | |
| } | |
| @Override | |
| public void printFolderDetails() { | |
| System.out.println("Folder Details: Movie's"); | |
| for(FolderDetailsComponent component : comp){ | |
| component.printFolderDetails(); | |
| } | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| public class ParentFolder implements FolderDetailsComponent { | |
| ArrayList<FolderDetailsComponent> comp = new ArrayList<>(); | |
| public void add(FolderDetailsComponent component){ | |
| comp.add(component); | |
| } | |
| @Override | |
| public void printFolderDetails() { | |
| System.out.println("Folder Details: Parent Folder"); | |
| for (FolderDetailsComponent component: comp) { | |
| component.printFolderDetails(); | |
| } | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| public class SoftwaresFolder implements FolderDetailsComponent { | |
| @Override | |
| public void printFolderDetails() { | |
| System.out.println("Folder Details: Software's"); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment