From f4adbe6bca0b1e88a6cb6641fae18a8fe60aafd1 Mon Sep 17 00:00:00 2001 From: Hammy Date: Sat, 13 Feb 2021 20:12:08 +0000 Subject: [PATCH] Add another example of composite pattern --- .../composite/file/directories/Directory.java | 80 +++++++++++++++++++ .../src/composite/file/directories/File.java | 15 ++++ .../file/directories/FileIterator.java | 27 +++++++ .../src/composite/file/directories/Main.java | 28 +++++++ .../composite/file/directories/TxtFile.java | 48 +++++++++++ 5 files changed, 198 insertions(+) create mode 100644 Design Patterns/src/composite/file/directories/Directory.java create mode 100644 Design Patterns/src/composite/file/directories/File.java create mode 100644 Design Patterns/src/composite/file/directories/FileIterator.java create mode 100644 Design Patterns/src/composite/file/directories/Main.java create mode 100644 Design Patterns/src/composite/file/directories/TxtFile.java diff --git a/Design Patterns/src/composite/file/directories/Directory.java b/Design Patterns/src/composite/file/directories/Directory.java new file mode 100644 index 0000000..860bff0 --- /dev/null +++ b/Design Patterns/src/composite/file/directories/Directory.java @@ -0,0 +1,80 @@ +package composite.file.directories; + +import java.util.ArrayList; +import java.util.List; + +public class Directory extends File { + + private final List files = new ArrayList<>(); + private final String name; + private final String dateModified; + private final Long size; + + Directory(String name, String dateModified, Long size) { + this.name = name; + this.dateModified = dateModified; + this.size = size; + } + + FileIterator createIterator() { + return new FileIterator(files); + } + + @Override + String getName() { + return name; + } + + @Override + String getDateModified() { + return dateModified; + } + + @Override + Long getSize() { + return size; + } + + @Override + void add(File file) { + files.add(file); + } + + @Override + void remove(File file) { + files.remove(file); + } + + @Override + File getFile(int fileIndex) { + return files.get(fileIndex); + } + + @Override + void displayFileInfo() { + System.out.println( + "\n\nFolder Name: " + getName() + + "\nDate Modified: " + getDateModified() + + "\nSize: " + getSize() + + "\nContents:" + ); + + FileIterator fileIterator = createIterator(); + while (fileIterator.hasNext()) { + File file = fileIterator.next(); + file.displayFileInfo(); + } + } + + @Override + public String toString() { + return "\n\nFolder Name: " + name + + "\nDate Modified: " + dateModified + + "\nSize: " + size + + "\nContents{" + files + + ", name='" + name + '\'' + + ", dateModified='" + dateModified + '\'' + + ", size=" + size + + "}"; + } +} diff --git a/Design Patterns/src/composite/file/directories/File.java b/Design Patterns/src/composite/file/directories/File.java new file mode 100644 index 0000000..8ae91bc --- /dev/null +++ b/Design Patterns/src/composite/file/directories/File.java @@ -0,0 +1,15 @@ +package composite.file.directories; + +public abstract class File { + + void add(File file) { throw new UnsupportedOperationException(); } + void remove(File file) { throw new UnsupportedOperationException(); } + File getFile(int fileIndex) { throw new UnsupportedOperationException(); } + void displayFileInfo() { throw new UnsupportedOperationException(); } + + String getName() { throw new UnsupportedOperationException(); } + String getDateModified() { throw new UnsupportedOperationException(); } + Long getSize() { throw new UnsupportedOperationException(); } + String getExtension() { throw new UnsupportedOperationException(); } + +} diff --git a/Design Patterns/src/composite/file/directories/FileIterator.java b/Design Patterns/src/composite/file/directories/FileIterator.java new file mode 100644 index 0000000..0252209 --- /dev/null +++ b/Design Patterns/src/composite/file/directories/FileIterator.java @@ -0,0 +1,27 @@ +package composite.file.directories; + +import java.util.Iterator; +import java.util.List; + +public class FileIterator implements Iterator { + + private final List directory; + private int currentIndex = 0; + + public FileIterator(List directory) { + this.directory = directory; + } + + @Override + public boolean hasNext() { + return currentIndex < directory.size(); + } + + @Override + public File next() { + if (this.hasNext()) { + return directory.get(currentIndex++); + } + return null; + } +} diff --git a/Design Patterns/src/composite/file/directories/Main.java b/Design Patterns/src/composite/file/directories/Main.java new file mode 100644 index 0000000..8a633e5 --- /dev/null +++ b/Design Patterns/src/composite/file/directories/Main.java @@ -0,0 +1,28 @@ +package composite.file.directories; + +public class Main { + public static void main(String[] args) { + + File rootDirectory = new Directory("Root", "27/02/2021", 100L); + + rootDirectory.add(new TxtFile("Winston", "26/05/2019", 5L)); + rootDirectory.add(new TxtFile("Readme", "06/02/2019", 202L)); + + File goudhamFolder = new Directory("Goudham", "12/03/2018", 150L); + + goudhamFolder.add(new TxtFile("University", "07/08/2002", 90L)); + goudhamFolder.add(new TxtFile("Overwatch", "02/12/2010", 678L)); + + rootDirectory.add(goudhamFolder); + + File matthewFolder = new Directory("Matthew", "05/10/2019", 570L); + + matthewFolder.add(new TxtFile("Grades", "10/03/2000", 700L)); + matthewFolder.add(new TxtFile("Star Wars", "18/08/2016", 20L)); + + rootDirectory.add(matthewFolder); + + rootDirectory.displayFileInfo(); + + } +} diff --git a/Design Patterns/src/composite/file/directories/TxtFile.java b/Design Patterns/src/composite/file/directories/TxtFile.java new file mode 100644 index 0000000..87e6aae --- /dev/null +++ b/Design Patterns/src/composite/file/directories/TxtFile.java @@ -0,0 +1,48 @@ +package composite.file.directories; + +public class TxtFile extends File { + + private final String name; + private final String dateModified; + private final Long size; + private final String extension = ".txt"; + + public TxtFile(String name, String dateModified, Long size) { + this.name = name; + this.dateModified = dateModified; + this.size = size; + } + + @Override + public String getName() { + return name; + } + + @Override + public String getDateModified() { + return dateModified; + } + + @Override + public Long getSize() { + return size; + } + + @Override + public String getExtension() { + return extension; + } + + @Override + public void displayFileInfo(){ System.out.println(this); } + + @Override + public String toString() { + return "TxtFile{" + + "name='" + name + '\'' + + ", dateModified='" + dateModified + '\'' + + ", size=" + size + + ", extension='" + extension + '\'' + + "} "; + } +}