Web site layout - 790 Graphical User Interface Components: Part 2 Chapter
790 Graphical User Interface Components: Part 2 Chapter 13 pattern is similar to an adapter for a plug on an electrical device electrical sockets in Europe are different from those in the United States, so an adapter is needed to plug an American device into a European electrical socket and vice versa. Java provides several classes that use the Adapter design pattern. Objects of these classes act as adapters between objects that generate certain events and those objects that handle these events. For example, a MouseAdapter, which we explained in Section 12.12, adapts an object that generates MouseEvents to an object that handles MouseEvents. Bridge Suppose we are designing class Button for both the Windows and Macintosh operating systems. Class Button contains specific button information such as an ActionListener and a String label. We design classes Win32Button and MacButton to extend class Button. Class Win32Button contains look-and-feel information on how to display a Button on the Windows operating system and class MacButton contains look-and-feel information on how to display a Button on the Macintosh operating system. Two problems arise from this approach. First, if we create new Button subclasses, we must create corresponding Win32Button and MacButton subclasses. For example, if we create class ImageButton (a Button with an overlapping Image) that extends class Button, we must create additional subclasses Win32ImageButton and MacImageButton. In fact, we must create Button subclasses for every operating system we wish to support, which increases development time. The second problem is that when a new operating system enters the market, we must create additional Button subclasses specific to that operating system. The Bridge design pattern avoids these problems by separating an abstraction (e.g., a Button) and its implementations (e.g., Win32Button, MacButton, etc.) into different class hierarchies. For example, the Java AWT classes use the Bridge design pattern to enable designers to create AWT Button subclasses without needing to create corresponding subclasses for each operating system. Each AWT Button maintains a reference to a ButtonPeer, which is the superclass for platform-specific implementations, such as Win32ButtonPeer, MacButtonPeer, etc. When a programmer creates a Button object, class Button calls factory method createButton of class Toolkit to create the platform-specific ButtonPeer object. The Button object stores a reference to its ButtonPeer this reference is the bridge in the Bridge design pattern. When the programmer invokes methods on the Button object, the Button object invokes the appropriate method on its ButtonPeer to fulfill the request. If a designer creates a Button subclass called ImageButton, the designer does not need to create a corresponding Win32ImageButton or MacImageButton with platform-specific image-drawing capabilities. An ImageButton is a Button. Therefore, when an ImageButton needs to display its image, the ImageButton uses its ButtonPeer s Graphics object to render the image on each platform. This design pattern enables designers to create new cross-platform GUI components using a bridge to hide platform-specific details. Portability Tip 13.2 Designers often use the Bridge design pattern to enhance the platform independence of their systems. This design pattern enables designers to create new cross-platform components using a bridge to hide platform-specific details.
Note: If you are looking for cheap and reliable webhost to host and run your mysql application check Vision professional web hosting services