This is an individual, closed-book exam. There are 6 problems. You have 75 minutes to complete this exam.
If a problem asks for an example or a fragment of code, please, answer with the simplest example or fragment of code you can think of.
If a problem asks for a Yes/No or similar answer, please make sure that your solution provides this answer.
If you feel that you need to make additional assumptions for the solution of a problem, please state the assumptions you make.
Single Threaded Execution
Sketch the code/structure of this pattern.
class HoldDatum {
private Datum datum;
public synchronized void set (Datum d) { datum = d; }
public synchronized Datum get () { return datum; }
}
Name and briefly explain these features.
PRIVATE CONSTRUCTOR: a private constructor that ensures that
the singleton class cannot be instantiated by clients.
STATIC getInstance METHOD: a static method that returns
a reference to the unique instance of the singleton class.
Sketch the code/structure of this pattern.
class Singleton {
private Singleton () {}
private static Singleton instance = new Singleton ();
public static Singleton getInstance () { return instance; }
}
Discuss the key elements of a class that implements the SetCurrentClock command and sketch its code. Hint: you can assume the existence of any time manipulation method that you need. Use an expressive enough name for any such method and/or describe with a comment or separately what the method does.
Class SetCurrentClock extends some base class, say Command.
Class SetCurrentClock has methods doit and undoit.
Class SetCurrentClock stores internally the difference
between the application clock time before being set by the
method doit and the application clock time after being set.
This allows the undoit method to later restore the clock time
as if the doit command had not been executed.
Sketch the code/structure of the SetCurrentClock class.
class SetCurrentClock extends Command {
private Time delta;
public void doit (Time toBeSet) {
delta = time difference between current time and time to be set;
// advance (or delay) current time by delta
add delta to current time;
}
public void undoit () {
// undo what method doit did
subtract delta to current time;
}
}
Describe this property and how it enables the pattern to construct different objects. Hint: In your answer, I am looking for a clear, complete and concise explanation. I do not want to read code.
The Factory Method Pattern consists of a base abstract class B
and specialized subclasses. Class B defines a method
referred to as the "factory method" which is not overridden
in subclasses. Class B also declares abstract methods, m1, m2, ...
which are defined in subclasses. The factory method calls
m1, m2, ... When the factory method is executed for an object
of a subclass S of B, the calls to m1, m2, ... are executed
by the methods of S that override m1, m2, ... The differences
between m1, m2, ... in different subclasses of B are responsible
for the differences in the objects constructed by these subclasses.
What are the conventional names and signatures of the overloaded and overridden methods in the Visitor Pattern.
Two methods with the same name (identifier) are OVERLOADED
when they have the same scope. The methods must have a
different signature. The ambiguity is resolved
at compile-time, by looking at the DECLARATION of the
overloaded method arguments. E.g.,
class S extends B { ... }
void f (B b) { ...} // 1
void f (S s) { ... } // 2
B b = new S ();
f (x); // calls 2
A method f declared in a class S that extends B OVERRIDES
a method with the same name AND SIGNATURE and RETURN type
declared in B. The abmiguity is resolved at run-time.
The method executed by a call to f depends on the actual
type of the object to which f belongs to. E.g.,
class B { void f () { ... } }
class S extends B { void f () { ... } }
B b = new S ();
b.f () // execute f defined in S
In the Visitor Pattern, method accept in an Element class is
overidden, and method visit in a concrete Visitor is overloaded.