1 / 12

Lecture 32: Implementing the Composite Pattern

Computer Science 313 – Advanced Programming Topics. Lecture 32: Implementing the Composite Pattern. Pimp my File Manager. Writing a Java-based file manager Starts at drive, but drill-down into directories Considers only directories & files (for now)

everly
Download Presentation

Lecture 32: Implementing the Composite Pattern

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Computer Science 313 – Advanced Programming Topics Lecture 32:Implementing theComposite Pattern

  2. Pimp my File Manager • 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

  3. Pimp my File Manager • 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

  4. 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

  5. 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();} }

  6. 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

  7. Woot! Pimped-Out Recursion! public class Directory extends OSEntry {public List<OSEntry> contents;public String toString() { String retVal = getName(); for (OSEntry entry : contents) {retVal += entry.toString(); } return retVal;}

  8. 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);}

  9. 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

  10. File Code public class File extends OSEntry {public java.util.FilefileInOS;public String toString() { return getName();}public long getSize() { return fileInOS.getLength();} }

  11. 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!”); }

  12. 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

More Related