Chapter 8. Composite Pattern


In This Chapter

Understanding the Element Interface 103

Understanding Leaf Elements 106

Understanding Composite Elements 107

Building a File System Example 108

Summary 112

The Composite pattern enables you to elegantly deal with recursive or hierarchical data structures. There are many examples of hierarchical data structures, making the Composite pattern very useful. A common example of such a data structure is one that you encounter every time you use a computer: the file system. The file system consists of directories and files. Every directory potentially has contents. The contents of a directory might be files, but they also might be directories. In this way, the file system of a computer is organized in a recursive structure. If you want to represent such a data structure programmatically, you can use the Composite pattern.

The Composite pattern has the following elements:

  • Element interface: An interface for all participating elements.

  • Leaf: A class representing terminating elements in the data structure. In the file system example, files are leaf elements because they don't have child elements. Leaf classes must implement the element interface.

  • Composite: A class for the collections in the data structure. In the file system example, directories are composite elements. Composite classes must implement the element interface.




Advanced ActionScript 3 with Design Patterns
Advanced ActionScript 3 with Design Patterns
ISBN: 0321426568
EAN: 2147483647
Year: 2004
Pages: 132

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net