Add another example of composite pattern
parent
ae55ab6079
commit
f4adbe6bca
@ -0,0 +1,80 @@
|
||||
package composite.file.directories;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
public class Directory extends File {
|
||||
|
||||
private final List<File> 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 +
|
||||
"}";
|
||||
}
|
||||
}
|
@ -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(); }
|
||||
|
||||
}
|
@ -0,0 +1,27 @@
|
||||
package composite.file.directories;
|
||||
|
||||
import java.util.Iterator;
|
||||
import java.util.List;
|
||||
|
||||
public class FileIterator implements Iterator<File> {
|
||||
|
||||
private final List<File> directory;
|
||||
private int currentIndex = 0;
|
||||
|
||||
public FileIterator(List<File> 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;
|
||||
}
|
||||
}
|
@ -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();
|
||||
|
||||
}
|
||||
}
|
@ -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 + '\'' +
|
||||
"} ";
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue