Com S 362 --- Object-Oriented Analysis and Design EXERCISE: INHERITANCE 2 (File $Date: 2003/09/25 22:32:53 $) The purpose of this exercise is for you to learn about some design patterns that use inheritance and polymorphism. As with all exercises, this is to be done individually, not in teams. And it is due the day this topic is planned to be discussed in class, unless specified otherwise (see the syllabus at: http://www.cs.iastate.edu/~cs362/syllabus.shtml). As with all exercises, you have two choices for doing the work. You can either: - complete it as specified or - write down questions or problems that you had in trying to complete it. If you write down questions or problems you have, these should be detailed enough so that we can tell that you have read the materials and thought about them. (Don't just write: "I couldn't get it to work; say what you tried and what you didn't understand.) During the class where this exercise is discussed, you should try to get help with these by explaining what you did and what your problems or confusions are. Don't be shy; there will be other people with the same problem, and everyone can learn by discussing these issues. 1. [Polymorphism] Read sections 16.3-16.4 and section 22.1 of Craig Larman's book Applying UML and Patterns (second edition, Prentice Hall PTR, Upper Saddle River, NJ, 2002). (a) What problem does the polymorphism pattern solve? (b) Describe the solution that polymorphism offers to this problem. (c) Where could we use (or did we use) the polymorphism design pattern in the Calculator example presented in class? (d) Give an example (in words) of how one could use polymorphism in a program that managed vehicles for a rental agency. 2. [Template Method Design pattern] Read Chapter 1 (for background) and pp. 325-330 of Erich Gamma, Richard Helm, Ralph Johnson, and John Vlissides's book Design Patterns: Elements of Reusable Object-Oriented Software (Addison-Wesley, Reading, Mass., 1995). (If you can't find this, read section 34.12 of Craig Larman's book Applying UML and Patterns (second edition, Prentice Hall PTR, Upper Saddle River, NJ, 2002). (a) When should one use the template method design pattern? (b) How does it differ from the polymorphism pattern? (c) Is there a place where we could use (or did use) the template method design pattern in the Calculator example? 3. [Singleton design pattern] Read Chapter pp. 127-134 of Erich Gamma, Richard Helm, Ralph Johnson, and John Vlissides's book Design Patterns: Elements of Reusable Object-Oriented Software (Addison-Wesley, Reading, Mass., 1995). Also read section 23.4 of Craig Larman's book Applying UML and Patterns (second edition, Prentice Hall PTR, Upper Saddle River, NJ, 2002. (a) What advantages does the singleton design pattern have over using static fields and methods? (b) Change the code of the Calculator example (availble from the course syllabus web page or from /home/course/cs362/public/lectures/inheritance-polymorphism/Calculator/ on the Com S department machines, to use the singleton pattern instead of using static fields and methods in the calc.Registers class. Make sure all the rest of the code for the calculator compiles and passes all the tests. Print the code for the class calc.Registers class and any other code that needed to be changed. (c) Are there any other classes in the Calculator example that could benefit from using the Singleton pattern? Explain. WHAT TO HAND IN You should have at the beginning of class, written answers to the above questions (or written out questions and problems you encountered for each part). Make sure your name is on these. Attach the printouts, if any, requested above. ADDITIONAL READINGS If you have time, read about the "Template Method" pattern in the Design Patterns book (also on reserve). If you are new to Java you might also want to read the interesting (to you) parts of the "new to Java" section on-line at http://developer.java.sun.com/developer/onlineTraining/new2java/ (If you read that link, I would advise starting with the "step by step programming" step, which is step 3.)