Slides - Canisius College Computer Science

advertisement
Computer Science 313 – Advanced Programming Topics
LECTURE 32:
IMPLEMENTING THE
COMPOSITE PATTERN
 Writing a Java-based file manager
 Starts at drive, but drill-down into directories
 Considers only directories & files (for now)
 Named required for all directory entries
 Print out the names of directories & files
 Print out files sizes as we traverse directories
 Allow user to create & delete files
 Remain true to our basic nature
 Writing a Java-based file manager
 Starts at drive, but drill-down into directories
 Considers only directories & files (for now)
 Named required for all directory entries
 Print out the names of directories & files
 Print out files sizes as we traverse directories
 Allow user to create & delete files
 Remain true to our basic nature
Composite Pattern Classes
 Create abstract superclass for the pattern
 This class normally used by client code
 Common methods & fields defined in superclass
 Should also declare any abstract methods needed
 Other classes subclass of abstract superclass
 Decorator-like parallel hierarchies not required
 Subinterfaces not required & almost never used
 Could create hierarchies as needed
 But consider composition vs. inheritence
Composite Code Inside
public abstract class OSEntry {
public String name;
public abstract String toString();
public void createFile(String fileName)
throws UnsupportedOperationException {
throw new UnsupportedOperationException();
}
public void createDir(String dirName)
throws UnsupportedOperationException {
throw new UnsupportedOperationException();
}
}
Directories Contain…?
 Currently, each directory has lots of files
 Will need a field to store these references
 Could use an array or some type of List
 Question is: what type should List hold?
 List could simply store references to File
 Unable to hold new types of entries we create
 Class is forever closed to any type of extension
 Containers must refer to abstract superclass
Woot! Pimped-Out Recursion!
public class Directory extends OSEntry {
public List<OSEntry> contents;
public String toString() {
String retVal = getName();
for (OSEntry entry : contents) {
}
return retVal;
}
Container Code
public class Directory extends OSEntry {
public List<OSEntry> contents;
public void createFile(String fName) {
String newFile = new File(getName(), fName);
contents.add(newFile);
}
public void createDir(String dName) {
String dir = new Directory(getName(), dName);
contents.add(dir);
}
File Methods…
 Files should not be able to create files & dirs
 Methods declared previously, so that is good
 Even better, already throw documented exception
 Useful trick when option may/may not be possible
 Should be careful about too many exceptions
 Should be able to get size of a file
 Most classes should not provide this method
 Instead, we will not declare this in the superclass
 Clients must have a File before calling method
File Code
public class File extends OSEntry {
public java.util.File fileInOS;
public String toString() {
return getName();
}
public long getSize() {
return fileInOS.getLength();
}
}
Client Code
OSEntry entry;
System.out.println(entry.toString());
if (entry instanceof File) {
System.out.println(“Size is :” +
((File)entry).getSize());
} else {
System.out.println(“No size, bozo!”);
}
For Next Class
 Lab available on the web
 Lab will be due 2 weeks from Friday
 No class this Friday
 Some percentage of class with me in Hartford, CT
 Use time wisely – work on lab, study for the test #2
 Test #2 in class on Monday
 Can include any and all material since last test
 Patterns & optimizations fair game to ask about
 Open-note, open-book, open-template, but closed slide
Download