794 Graphical User Interface Components: Part 2 Chapter (Linux web host)
794 Graphical User Interface Components: Part 2 Chapter 13 The Java API contains classes that use the Observer design pattern. Class java.util.Observable represents a subject. Class Observable provides method addObserver, which takes a java.util.Observer argument. Interface Observer allows the Observable object to notify the Observer when the Observable objects changes state. The Observer can be an instance of any class that implements interface Observer; because the Observable object invokes methods defined in interface Observer, the objects remain loosely coupled. If a developer changes the way in which a particular Observer responds to changes in the Observable object, the developer does not need to change the Observable object. The Observable object interacts with its Observers only through interface Observer, which enables the loose coupling. The optional elevator simulation case study in the Thinking About Objects sections uses the Observer design pattern to allow the ElevatorModel object (the subject) to notify the ElevatorView object (the observer) of changes in the ElevatorModel. The simulation does not use class Observable and interface Observer from the Java library rather, it uses a custom interface ElevatorModelListener that provides functionality similar to that of interface Observable. The Swing GUI components use the Observer design pattern. GUI components collaborate with their listeners to respond to user interactions. For example, an ActionListener observes state changes in a JButton (the subject) by registering to handle that JButton s events. When pressed by the user, the JButton notifies its ActionListener objects (the observers) that the JButton s state has changed (i.e., the JButton has been pressed). Strategy The Strategy design pattern is similar to the State design pattern (discussed in Section 9.24.3). We mentioned that the State design pattern contains a state object, which encapsulates the state of a context object. The Strategy design pattern contains a strategy object, which is analogous to the State design pattern s state object. The key difference between a state object and a strategy object is that the strategy object encapsulates an algorithm rather than state information. For example, java.awt.Container components implement the Strategy design pattern using LayoutManagers (discussed in Section 12.14) as strategy objects. In package java.awt, classes FlowLayout, BorderLayout and GridLayout implement interface LayoutManager. Each class uses method addLayoutComponent to add GUI components to a Container object however, each method uses a different algorithm to display these GUI components: a FlowLayout displays components in a left-to-right sequence; a BorderLayout displays components in five regions; and a GridLayout displays components in row-column format. Class Container contains a reference to a LayoutManager object (the strategy object). Because an interface reference (i.e., the reference to the LayoutManager object) can hold references to objects of classes that implement that interface (i.e., the Flow- Layout, BorderLayoutor GridLayout objects), the LayoutManager object can reference a FlowLayout, BorderLayout or GridLayout at any one time. Class Container can change this reference through method setLayout to select different layouts at run time. Class FlowLayoutDemo (Fig. 12.24) demonstrates the application of the Strategy pattern line 16 declares a new FlowLayout object and line 19 invokes the Con
Note: In case you are looking for affordable webhost to host and run your servlet application check Vision servlet hosting services