Name: Please read all instructions carefully. The exam is closed book & no laptops / phones / computers shall be present nor be used. Please write your answers in the space provided. You may use the backs of the exam pages as scratch paper. Please do not use any additional scratch paper. Solutions will be graded on correctness and clarity. You can score a total of 100 points on this exam. When asked to provide a class diagram use the following semantics. 1. (5 points) Name (at least) two properties of software designs that design patterns seek to improve through their application. [2.5 points per good answer] Software design can easily result in spaghetti code. Patterns tell you how to structure classes and objects so your software design becomes easier to: 1) understandability. Software designs are a common understanding of the intended system. If it is easier to understand this design less mistakes are made. 2) maintainability. Design patterns may increase the maintainability of your code for example when you encapsulate changes such as in the strategy pattern. 3) flexibility. Application of design patterns may allow for easily extending your software design with new functionality. Answer on page 5 to 23 2. (5 points) What is one of the most important benefits that design patterns provide to communities of developers? Design patterns give you a shared vocabulary with other developers. Once you ve mastered the vocabulary you can more easily communicate with other developers and inspire those who don t know patterns to start learning them. Patterns let you communicate solutions very efficiently using their name. Patterns further let you think more abstractly about problems without getting lost into details Answer on page 28.
2. (10 points) Provide a class diagram for the Command pattern. Make sure to list the methods for each class/interface and give a short description of the responsibility of each class/interface 6 points for the correct diagram and 4 points for the descriptions. Answer on page 207 3. (5 points) To implement a singleton, how does double-checked locking improve performance? Using synchronized in getinstance() is detrimental to performance because it is always called. Double checking improves it as follows: we first check if an instance is created and if not, then we synchronize. This way we only have to synchronize once when the instance is created and not when you call getinstance(). Answer on Page 182.
4. (5 points) A simple factory is often implemented as a static method. Name one benefit of this approach. If the simple factory is implemented as a static method you don t have to instantiate the simple factory to make use of the create method. Answer on Page 115 5. (5 points) Name one benefit of using the Decorator pattern and one disadvantage. Advantage (2.5 points) : new behavior can be added at runtime without modifying existing code. Disadvantage (2.5) Decorators can result in many small objects in our design and overuse can be complex. Answer on Page 105 8. (5 points) Give an example of a set of classes that are tightly coupled and a set that is loosely coupled. High coupling as every class talks to every Loose coupling as all classes communicate other class and knows every class s interface through asingle interface and they Answer on page 53 know little about eachother
Design Problems. A number of design problems are listed below. For each design problem answer the three following questions: 1. Which? Select the most appropriate design pattern to use and you must provide the pattern s intent. 2. Why? Clearly motivate how this pattern addresses the problem 3. How? Explain how this pattern should be implemented and show an appropriate class diagram to illustrate the implementation of your pattern. (no code). Don t give a generic class diagram for the pattern but for that specific design problem. Follow the UML conventions for class diagrams. 1. (20 points). Point is a novel home security device that has various sensors. Point listens to the sounds of your home, senses what's in the air and lets you know if anything is wrong. It notifies you of windows breaking, alarms ringing, a door left open, or the presence of smoke. A Point device can be installed anywhere in your home and connects to your wireless network. You can download an app and connect to the Point device upon which you will receive messages when Point detects when anything is wrong. You want various apps for various platforms (ios, Android) to be able to get notifications. At some point other home automation devices (e.g. garage door opener, firealarm) may want to received messages as well. In case Point generates too many messages, some users may not want to receive them anymore. 1. (5 points) Which: Observer - defines a one to many dependency between objects so that when one object changes state, all of its dependents are notified and updated automatically. 2. (5 points) Why: The point device needs to have multiple apps listen to it and this pattern enables objects communicating with each other to maintain consistency and to achieve loose coupling. Objects can easily subscribe to other objects they are interested in without having to know the details of each observable. 3. (10 points) How: See class diagram.
2. (20 points). A professor is trying to open up emailed homework assignments that students have compressed in various formats (zip, tar, jar, arj, sit, rar) in OSX. Unfortunately OSX has only built in support for zip and the professor has to revert to installing various tools to uncompress each file using a command line tool, which is cumbersome. Design a universal compression tool that can deal with various compression formats. 1. (5 points) Which: Strategy - defines a family of algorithms, encapsulates each one and makes them interchangeable. 2. (5 points) Why: we need to support different types of behavior (e.g., decompression algorithms). New types of decompression algorithms may need to be added and we would like to call a single decompress method. The strategy pattern provides a unified interface (decompress) for various types of compression formats. 3. (10 points) How: See class diagram.
personal professional premium 3. (20 points). The company GreenHOST is a is a web hosting company that offers three different server plans (personal, professional, premium). To each plan, various hosting services can be added (Cloudhosting, SSL, 24x7 support, backup, Google Adwords, CloudFlare, mysql). You need to write an application that can easily compute the total monthly service fees for each plan and any combination of services. Your application must be able to easily support adding new types of hosting services when they become available. You must avoid an explosion of classes. Sample output could be: > Personal Hosting, SSL, Google Adwords, MySQL: 59.94 a month. 1. (5 points) Which: Decorator - attaches additional responsibilities to an object dynamically. Decorators provide a flexible alternative to subclassing for extending functionality 2. (5 points) Why: If we were to implement each combination of server plan+ hosting service as its own class using inheritance it would lead to an explosion of classes. The decorator pattern only requires adding one new class to add a new service. 3. (10 points) How: See class diagram.