Tuesday and Thursday
12:30 PM - 1:45 PM
What's New (Home)
Homework 5: Design Patterns
In class, we covered a variety of design patterns. In this assignment, you get a chance to implement three of them: Decorator, Observer, and State. You may use any Object-Oriented programming language to implement the patterns: C++, Java, Smalltalk, etc. Note: only implement the minimal amount of code you need to demonstrate the design pattern (e.g. these should be relatively short programs).
- Write a program that demonstrates the advantages of the State design pattern. The program should create an object that has at least three states. This object should have one operation that returns different output depending on its current state. This object should internally update the state after each call to this operation. Your program should instantiate the object, and call the state-dependent operation three times. As a result, your program should produce output that shows that the object is indeed changing state, since the same operation will produce different output depending on the current state.
- Write a program that demonstrates the advantages of the Observer pattern. Do not reuse the classes that implement the Observer pattern in the Java class library, I want you to implement the pattern yourself. Your program should create an object that has state that needs to be observed. It should attach at least two observers to this object and it should then change the state of the observed object twice. Each time the observers should indicate that they received the state change notification and retrieved the new state.
- Write a program that demonstrates the advantages of the Decorator pattern. Create an object with two operations that produce output when they are called. Create a decorator for that object that adds two additional operations that produce output when they are called. (For simplicity, you should have the operations write to standard out, e.g. do not try to implement a gui program that makes use of decorators graphically.) Instantiate an undecorated object and have your program call both operations. Then instantiate a decorator, use it to "decorate" the original object, then call all four operations provided by the decorator. The output of the original two operations should be decorated in some way. That is, it should be obvious that the original output of the undecorated object is being manipluated in some way by the decorator.
This homework assignment is worth 60 points, 20 points for each design pattern. Please turn in the source code and the output produced by running your program. If you submit electronically, combine your source code files and output into a single archive.
This homework can be turned in via hard copy, fax, or submitted electronically. Please remember that acceptable formats for electronic submission are ASCII, postscript, and PDF. Send ASCII submissions in the body of an e-mail message (not as an attachment). Send postscript/PDF submissions as an attachment to an e-mail message. If you send postscript, be sure to embed any special fonts that you may use directly into the postscript font. (Most printer drivers provide an option to allow you to embed fonts.) Make sure that your e-mail message is clearly marked (as discussed in lecture 1) and that this same information appears in the attached document.
Send questions to <firstname.lastname@example.org>. Answers to common questions will be discussed in class and/or posted to the class website.