SlideServe
Menu
Préférences
© SlideServe · v2026

Java File Manager with Composite Pattern Implementation

everly avatar
12 diapositives · 237 Views · Jul 18, 2014

Learn to build a Java file manager using the Composite Pattern. Explore directories and files, print names, sizes, create and delete files. Stay true to the basic nature. Understand composite pattern classes, abstract superclass, and code implementation with necessary classes.

Java File Manager with Composite Pattern Implementation - slide 1
01 / 12Java File Manager with Composite Pattern Implementation
1 / 17

Télécharger la présentation

Java File Manager with Composite Pattern Implementation
Une image / un lien ci-dessous est fourni (tel quel) pour télécharger la présentation

Politique de téléchargement : le contenu du site vous est fourni TEL QUEL pour votre information et votre usage personnel ; il ne peut être vendu, concédé sous licence ni partagé sur d’autres sites sans l’accord de son auteur.Le contenu vous est fourni TEL QUEL, pour votre information et votre usage personnel uniquement. Téléchargez la présentation en cliquant sur ce lien. Lors du téléchargement, si vous ne parvenez pas à télécharger une présentation, l’éditeur a peut-être supprimé le fichier de son serveur.Pendant le téléchargement, si vous ne parvenez pas à obtenir une présentation, le fichier a peut-être été supprimé par l’éditeur.

F I N

Plus de contenu associé

Transcription de la présentation
  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