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

Home
Java
1.2D Graphics GUI
2.3D
3.Advanced Graphics
4.Ant
5.Apache Common
6.Chart
7.Class
8.Collections Data Structure
9.Data Type
10.Database SQL JDBC
11.Design Pattern
12.Development Class
13.EJB3
14.Email
15.Event
16.File Input Output
17.Game
18.Generics
19.GWT
20.Hibernate
21.I18N
22.J2EE
23.J2ME
24.JavaFX
25.JDK 6
26.JDK 7
27.JNDI LDAP
28.JPA
29.JSP
30.JSTL
31.Language Basics
32.Network Protocol
33.PDF RTF
34.Reflection
35.Regular Expressions
36.Scripting
37.Security
38.Servlets
39.Spring
40.Swing Components
41.Swing JFC
42.SWT JFace Eclipse
43.Threads
44.Tiny Application
45.Velocity
46.Web Services SOA
47.XML
Java » Design Pattern » Composite Pattern 




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
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.