1 / 16

Lecture 32: Implementing Composite Pattern

Computer Science 313 – Advanced Programming Topics. Lecture 32: Implementing 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) Named required for all directory entries

huong
Download Presentation

Lecture 32: Implementing 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:ImplementingComposite 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 which preferred: compositionor 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?

  7. 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? • References to Listcouldbe to File • But new types impossible to be stored in the List • With approach, class closed to extension forever

  8. 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? • References to Listcouldbe to File • But new types impossible to be stored in the List • With approach, class closed to extension forever • Containers mustrefer to abstract superclass

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

  10. Directory Code public class Directory extends OSEntry{public List<OSEntry> contents;public String toString() { String retVal = getName(); for (OSEntry entry : contents) {retVal += entry.toString(); } return retVal;}

  11. Directory Code Pimped Out public class Directory extends OSEntry{public List<OSEntry> contents;public String toString() { String retVal = getName(); for (OSEntry entry : contents) {retVal += entry.toString(); } return retVal;}

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

  13. 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 File to call method

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

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

  16. For Next Class • Lab probably should be done now • Will have mini-lecture during lab introducing map-reduce • For Monday’s lecture the readings on web • How do we optimize loops?What can be done? • Are there ways to structure code to help optimizer? • Test #2 in class week from Monday • Can include any and all material since last test • Patterns & optimizations fair game to ask about

More Related