Web domain - Chapter 13 Graphical User Interface Components: Part 2
Chapter 13 Graphical User Interface Components: Part 2 791 Composite Designers often organize components into hierarchical structures (e.g., a hierarchy of directories and files on a hard drive) each node in the structure represents a component (e.g., a file or directory). Each node can contain references to other nodes. A node is called a branch if it contains a reference to one or more nodes (e.g., a directory containing files). A node is called a leaf if it does not contain a reference to another node (e.g., a file). Occasionally, a structure contains objects from several different classes (e.g., a directory can contain files and directories). When an object called a client wants to traverse the structure, the client must determine the particular class for each node. Making this determination can be time consuming, and the structure can become hard to maintain. In the Composite design pattern, each component in a hierarchical structure implements the same interface or extends a common superclass. This polymorphism (introduced in Section 9.10) ensures that clients can traverse all elements branch or leaf uniformly in the structure. Using this pattern, a client traversing the structure does not have to determine each component type, because all components implement the same interface or extend the same superclass. Java GUI components use the Composite design pattern. Consider the Swing component class JPanel, which extends class JComponent. Class JComponent extends class java.awt.Container, which extends class java.awt.Component (Fig. 13.22). Class Container provides method add, which appends a Component object (or Componentsubclass object) to that Containerobject. Therefore, a JPanel object may be added to any object of a Componentsubclass, and any object from a Componentsubclass may be added to that JPanelobject. A JPanelobject can contain any GUI component while remaining unaware of that component s specific type. A client, such as a JPanelobject, can traverse all components uniformly in the hierarchy. For example, if the JPanel object calls method repaint of superclass Container, method repaintdisplays the JPanelobject and all components added to the JPanel object. Method repaint does not have to determine each component s type, because all components inherit from superclass Container, which contains method repaint. javax.swing.JComponent javax.swing.JPanel java.awt.Container java.awt.Component Fig. 13.22 Inheritance hierarchy for class JPanel.
Note: In case you are looking for affordable and reliable webhost to host and run your business application check Vision ftp web hosting services