Web hosting domain names - Chapter 9 Object-Oriented Programming 525 Mediator (not

Chapter 9 Object-Oriented Programming 525 Mediator (not discussed) Memento (Section 9.24) Observer (Section 13.18) State (Section 9.24) Strategy (Section 13.18) Template Method (Section 13.18) Visitor (not discussed) Memento Consider a painting program. This type of program allows a user to create graphics. Occasionally the user may position a graphic improperly in the drawing area. Painting programs offer an undo feature that allows the user to unwind such an error. Specifically, the program restores the drawing area s state to that before the user placed the graphic. More sophisticated painting programs offer a history, which stores several states in a list, so the user can restore the program to any state in the history.The Memento design pattern allows an object to save its state, so that if necessary the object can be restored to its former state. The Memento design pattern requires three types of objects. The originator object occupies some state the set of attribute values at a specific time in program execution. In our painting-program example, the drawing area acts as the originator, because it contains attribute information describing its state when the program first executes, the area contains no elements. The memento object stores a copy of all attributes associated with the originator s state i.e., the memento saves the drawing area s state. The memento is stored as the first item in the history list, which acts as the caretaker object the object that contains references to all memento objects associated with the originator. Now, suppose the user draws a circle in the drawing area. The area contains different information describing its state a circle object centered at specified x-y coordinates. The drawing area then uses another memento to store this information. This memento is stored as the second item in the history list. The history list displays all mementos on screen, so the user can select which state to restore. Suppose the user wishes to remove the circle if the user selects the first memento from the list, the drawing area uses the first memento to restore itself to a blank area. State In certain designs, we must convey an object s state information or represent the various states that an object can occupy. Our optional elevator simulation case study in the Thinking About Objects sections uses the State design pattern. Our simulation includes an elevator that moves between floors in a two-story building. A person walks across a floor and rides the elevator to the other floor. Originally, we used an integer value to represent on which floor the person is walking. However, we encountered a problem when we tried to answer the question on what floor is the person when riding the elevator? Actually, the person is located on neither floor rather the person is located inside the elevator. We also realized that the elevator and the floors are locations that the person can occupy in our simulation. We created an abstract superclass called Location to represent a location. Subclasses Elevator and Floor inherit from superclass Location. Class Person contains a reference to a Locationobject, which represents the current location eleva Copyright 1992 2002 by Deitel & Associates, Inc. All Rights Reserved. 7/7/01

Leave a Reply