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

Java
1. 2D Graphics GUI
2. 3D
3. Advanced Graphics
4. Ant
5. Apache Common
6. Chart
7. Collections Data Structure
8. Database SQL JDBC
9. Design Pattern
10. Development Class
11. Email
12. Event
13. File Input Output
14. Game
15. Hibernate
16. J2EE
17. J2ME
18. JDK 6
19. JSP
20. JSTL
21. Language Basics
22. Network Protocol
23. PDF RTF
24. Regular Expressions
25. Security
26. Servlets
27. Spring
28. Swing Components
29. Swing JFC
30. SWT JFace Eclipse
31. Threads
32. Tiny Application
33. Velocity
34. Web Services SOA
35. XML
Microsoft Office Word 2007 Tutorial
Java Tutorial
Java Source Code / Java Documentation
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
C# / C Sharp
C# / CSharp Tutorial
ASP.Net
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
PHP
Python
SQL Server / T-SQL
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Java » Design Pattern » Composite PatternScreenshots 
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 fcthrows CompositeException {
    dirContents.add(fc);
  }

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

  public long getComponentSize() {
    long sizeOfAllFiles = 0;
    Enumeration e = dirContents.elements();
    while (e.hasMoreElements()) {
      FileSystemComponent component = (FileSystemComponente
          .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
w___w___w___.__j_ava___2_s_.c___om__ | Contact Us
Copyright 2003 - 08 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.