Add all 23 GoF design pattern implementations (2026-06-13)
This commit is contained in:
27
02-structural/composite/File.java
Normal file
27
02-structural/composite/File.java
Normal file
@@ -0,0 +1,27 @@
|
||||
package composite;
|
||||
|
||||
/**
|
||||
* Leaf — a single file. It has no children.
|
||||
* getSize() returns its own size. print() shows its name.
|
||||
*
|
||||
* Notice: the File has no knowledge of directories or nesting.
|
||||
* It just knows its own name and size.
|
||||
*/
|
||||
public class File implements FileSystemItem {
|
||||
|
||||
private final String name;
|
||||
private final long size;
|
||||
|
||||
public File(String name, long sizeBytes) {
|
||||
this.name = name;
|
||||
this.size = sizeBytes;
|
||||
}
|
||||
|
||||
@Override public String getName() { return name; }
|
||||
@Override public long getSize() { return size; }
|
||||
|
||||
@Override
|
||||
public void print(String indent) {
|
||||
System.out.printf("%s[FILE] %s (%,d bytes)%n", indent, name, size);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user