Composite Patterns in Java 2 : Composite Pattern « Design Pattern « Java






Composite Patterns in Java 2

Composite Patterns in Java 2
/*
Software Architecture Design Patterns in Java
by Partha Kuchana 

Auerbach Publications

*/



import java.util.Enumeration;
import java.util.Vector;

public class CompositeDemo {
  public static final String SEPARATOR = ", ";

  public static void main(String[] args) {
    FileSystemComponent mainFolder = new DirComponent("Year2000");
    FileSystemComponent subFolder1 = new DirComponent("Jan");

    FileSystemComponent subFolder2 = new DirComponent("Feb");

    FileSystemComponent folder1File1 = new FileComponent(
        "Jan1DataFile.txt", 1000);
    FileSystemComponent folder1File2 = new FileComponent(
        "Jan2DataFile.txt", 2000);
    FileSystemComponent folder2File1 = new FileComponent(
        "Feb1DataFile.txt", 3000);
    FileSystemComponent folder2File2 = new FileComponent(
        "Feb2DataFile.txt", 4000);

    try {
      mainFolder.addComponent(subFolder1);
      mainFolder.addComponent(subFolder2);
      subFolder1.addComponent(folder1File1);
      subFolder1.addComponent(folder1File2);
      subFolder2.addComponent(folder2File1);
      subFolder2.addComponent(folder2File2);
    } catch (CompositeException ex) {
      //
    }

    //Client refers to both composite &
    //individual components in a uniform manner
    System.out.println(" Main Folder Size= "
        + mainFolder.getComponentSize() + "kb");
    System.out.println(" Sub Folder1 Size= "
        + subFolder1.getComponentSize() + "kb");
    System.out.println(" File1 in Folder1 size= "
        + folder1File1.getComponentSize() + "kb");

  }
}

class FileComponent extends FileSystemComponent {
  private long size;

  public FileComponent(String cName, long sz) {
    super(cName);
    size = sz;
  }

  public long getComponentSize() {
    return size;
  }

} // End of class

abstract class FileSystemComponent {
  String name;

  public FileSystemComponent(String cName) {
    name = cName;
  }

  public void addComponent(FileSystemComponent component)
      throws CompositeException {
    throw new CompositeException("Invalid Operation. Not Supported");
  }

  public FileSystemComponent getComponent(int componentNum)
      throws CompositeException {
    throw new CompositeException("Invalid Operation. Not Supported");
  }

  public abstract long getComponentSize();

} // End of class FileSystemComponent

class CompositeException extends Exception {

  public CompositeException(String msg) {
    super(msg);
  }
} // End of class

class DirComponent extends FileSystemComponent {
  Vector dirContents = new Vector();

  //individual files/sub folders collection

  public DirComponent(String cName) {
    super(cName);
  }

  public void addComponent(FileSystemComponent fc) throws CompositeException {
    dirContents.add(fc);
  }

  public FileSystemComponent getComponent(int location)
      throws CompositeException {
    return (FileSystemComponent) dirContents.elementAt(location);
  }

  public long getComponentSize() {
    long sizeOfAllFiles = 0;
    Enumeration e = dirContents.elements();
    while (e.hasMoreElements()) {
      FileSystemComponent component = (FileSystemComponent) e
          .nextElement();
      sizeOfAllFiles = sizeOfAllFiles + (component.getComponentSize());
    }
    return sizeOfAllFiles;
  }

} // End of class


           
       








Related examples in the same category

1.Composite pattern in JavaComposite pattern in Java
2.Composite Pattern 2