TDDB84 DesignPatterns Exam 2005 10 15 PDF
TDDB84 DesignPatterns Exam 2005 10 15 PDF
Design Patterns
Exam
Tid: 14-18
October 15, 2005
Place: Ter1 (Terra)
Skriv namn, klass, och personnummer på samtliga lösa blad. Poängavdrag kommer att
göras om punkterna nedan inte åtföljs! Write name, class, and person number on all
solution sheets. Please also observe the following, otherwise it might lead to subtraction
of points.
1) Endast ett svar per blad. Använd endast framsidan (delfrågor kan vara på samma sida).
Write only the answer to one task on one sheet. Use only the front side of the sheets
(answers to subtasks can be on one page).
2) Sortera inlämnade svar med avseende på uppgiftsnummer i stigande ordning. Sort the
solution sheets according to the task number.
3) Svaren får vara på svenska eller engelska. Answers may be in English or Swedish.
4) Dina svar skall tydligt visa lösningsmetod. Enbart rätt svar kommer inte att ge poäng. I
det fall du är osäker på frågeställning, skriv ner din tolkning och lös uppgiften utifrån din
tolkning. Your answers should clearly show solution methods, reasons, and arguments.
Short answers should be motivated. If you are in doubt, write a remark.
5) Som hjälpmedel är boken av Gamma eller liknande INTE tillåtna. The Gamma book
and the articles are NOT allowed.
1 Singleton (3 points)
Give an example where the Singleton design pattern would be useful.
Observation:
It is totally irrelevant what the three commands do, and you do not even need to know
what they are doing.
ConcreteModel
ConcreteView view -state
-state -view : ConcreteView
+Update() state = view->GetState()
+GetState()
+SetState() return state
Tasks:
(a) First, identify the pattern in the diagram.
(b) Second, identify the participants in the diagram and indicate their responsibilities.
Tasks:
(a) What pattern is represented by the diagram?
(b) Change the class diagram to implement an interpreter over arithmetic expressions.
(c) Sketch the methods added to the diagram at section (b)
(d) Changes the class diagram obtained in section (b) and add new classes (and
methods to the old ones) to implement a visitor over the integer trees.
(e) Sketch the new methods added to the class diagram (both for the existing classes
and for the new ones) at section (d). For the visitor methods consider just printing
out the tree contents.
7 Tetris (5 points)
A Tetris field is essentially a big collection of stones, some representing empty fields, and
some representing parts of a Tetris block, either just falling or landed at the bottom of the
play ground some time ago. Whenever a block lands, it dissembles into its individual
stones, which are considered independent from then on. It makes sense, therefore, to
represent each stone by an individual object in an object-oriented Tetris application.
Doing so naively results in a large amount of objects being allocated, deleted, and moved
all the time.
Task:
What design pattern can be applied to optimize this, taking advantage of the fact that
the stones are really very similar? Motivate your answer.
Imagine you have to realize a ray-tracer application. At the core of such an application is
a data structure, which maintains and represents the scene graph or the “world". Objects
can be very simple objects, which have a geometrically defined shape and a typically
algorithmically defined material, but they can also be arbitrarily complex compositions of
other objects. The scene graph is then repeatedly traversed to check for intersections with
various optical rays.
Tasks:
(a) Design a class structure, which can represent a scene graph. Which design pattern
is applicable?
(b) It is often useful to be able to transform existing objects in a scene graph, for
example by moving, scaling or rotating them. This should be possible for simple
and complex objects alike. Enhance the class structure from the previous subtask
to enable transformation of objects. Which design pattern do you use?
(c) When transforming an object it is typical to construct the transformation from the
basic transformation (scaling, rotation, and translation) by putting them in a
sequence of transformation operations. Extend the class structure to represent
such sequences of transformations. Discuss different solutions.
(d) When tracing complex objects it is often worthwhile to first check whether the ray
will ever intersect their bounding box (i.e., the smallest box completely enclosing
the object). Enhance the class structure to add this functionality. What design
pattern could you use? Are there other ways of doing it?
Person
Employee Student
Task:
Add runtime tracing of drinking behavior. You should be able to switch tracing on or
off for any object in the hierarchy at runtime. If tracing is on, the system logs when a
person starts and when it stops drinking. Which design pattern could you use? What
does your solution look like?
Tasks:
(a) Assume, every provider is known in advance, and implements an interface
IFlightProvider, which provides operations for querying for a connection, and for
booking a flight. In this application you will need a way to enable clients to
interface to these providers and book the cheapest flight on offer for the
destination and date they are interested in. Flight providers should require (and
receive) no knowledge on other flight providers known to the system. Which
design pattern could you use?
(b) Many airlines offer on-line booking services as web services. How can you
incorporate such an airline as a flight provider in the system above?
Tasks:
(a) The design of the generic event-driven simulator is based (partly) on a design
pattern. Which design pattern? Motivate your answer. If unsure, motivate other
alternatives that you consider.
(b) The GenericSimulator is given above. Why are some methods abstract?
Consider that you have to write a simulator for a specific system, let us call it
WeatherSimulator. Draw the class hierarchy of this specific simulator.
(c) Could you use any of the Abstract Factory or Factory Method design patterns in
order to generate system-specific events? Motivate your answer.
(d) How would you implement different specific simulators using the Strategy design
pattern? Consider changing the given code. Draw the class diagram of the
improved system.