CS510 Design Patterns, Winter 00, Final, March 13

Name: ______________________



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.



1.
What is the name of the pattern intended to prevent concurrent access to data?

    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; }
    }


2.
The Singleton Pattern relies on two key Java constructs or features to ensure that: (1) at most only one object of a class is ever created by clients, and (2) a reference to the unique instance of the singleton class is always accessible to clients.

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; }
    }


3.
A command of a Command Pattern, say SetCurrentClock, is intended to set the current time within an application program.

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;
        }
    }


4.
The Factory Method Pattern is used to construct families of related objects. This pattern relies on a key property of Object Oriented Programming Languages and Java in particular.

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.


5.
The Visitor Pattern relies on both overloading and overriding of methods. Briefly define these concepts and use a minimal abstract example to clarify your statements or definitions. I am not asking for a description or an example of an actual visitor.

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.


6.
For each question check zero or more choices according to the directions.

(a)
A not-yet-loaded Java class is loaded when (check the most accurate choice):
       
A constructor is invoked.
       
A static member is referenced.
       
Both of the above choices.
  X  
All of the above choices and in additional situations.

(b)
The equals method in class java.lang.Object (check all the correct choices):
       
Takes two arguments to compare.
  X  
Can be overridden in subclasses.
  X  
Tests whether two references refer to the same object.
       
Tests whether two references refer to objects with the same value.

(c)
Enumeration in package java.util identifies (check all the correct choices):
  X  
An interface.
       
An abstract class.
       
A (concrete) class.
       
None of the above choices.

(d)
The Prototype Pattern creates new objects by (check the most accurate choice):
       
Shallow copying.
       
Deep copying.
  X  
An appropriate combination of both the above choices.
       
Invoking either method clone or a ``copy constructor.''

(e)
The ``extrinsic state'' of a Flyweight object is (check all the correct choices):
       
Information shared by all the Flyweight objects.
       
Information shared by some Flyweight objects.
       
Information shared by no Flyweight objects.
  X  
Information stored by clients of Flyweight objects.