Chapter 13 Graphical User Interface Components: Part 2 (Http web server)
Chapter 13 Graphical User Interface Components: Part 2 789 should understand better how these components take advantage of design patterns and how developers integrate design patterns with Java GUI applications. 13.18.1 Creational Design Patterns Now, we continue our treatment of creational design patterns, which provide ways to instantiate objects in a system. Factory Method Suppose we are designing a system that opens an image from a specified file. Several different image formats exist, such as GIF and JPEG. We can use method createImageof class java.awt.Component to create an Image object. For example, to create a JPEG and GIF image in an object of a Component subclass such as a JPanel object, we pass the name of the image file to method createImage, which returns an Image object that stores the image data. We can create two Imageobjects, each which contains data for two images having entirely different structures. For example, a JPEG image can hold up to 16.7 million colors, whereas a GIF image can hold up to only 256. Also, a GIF image can contain transparent pixels that are not rendered on screen, whereas a JPEG image cannot contain transparent pixels. Class Image is an abstract class that represents an image we can display on screen. Using the parameter passed by the programmer, method createImage determines the specific Imagesubclass from which to instantiate the Imageobject. We can design systems to allow the user to specify which image to create, and method createImage will determine the subclass from which to instantiate the Image. If the parameter passed to method createImagereferences a JPEG file, method createImage instantiates and returns an object of an Imagesubclass suitable for JPEG images. If the parameter references a GIF file, createImage instantiates and returns an object of an Image subclass suitable for GIF images. Method createImageis an example of the Factory Method design pattern. The sole purpose of this factory method is to create objects by allowing the system to determine which class to instantiate at run time. We can design a system that allows a user to specify what type of image to create at run time. Class Component might not be able to determine which Image subclass to instantiate until the user specifies the image to load. For more information on method createImage, visit www.java.sun.com/j2se/1.3/docs/api/java/awt/Compo nent.html#createImage(java.awt.image.ImageProducer) 13.18.2 Structural Design Patterns We now discuss three more structural design patterns. The Adapter design pattern helps objects with incompatible interfaces collaborate with one another. The Bridge design pattern helps designers enhance platform independence in their systems. The Composite design pattern provides a way for designers to organize and manipulate objects. Adapter The Adapter design pattern provides an object with a new interface that adapts to another object s interface, allowing both objects to collaborate with one another. The adapter in this
Looking for affordable and reliable webhost to host and run your business application? Then look no more and go to servlet web hosting services.