Pattern Exams Sample Questions

Below are questions related to the learning outcomes for the "classic" patterns that were presented and discussed in class. Any of these are potential exam questions.

  1. Explain the statement "The command pattern supports separation of execution and invocation in both time and space."
  2. Argue both for and against including the functions to handle the Composite's children in the Component interface. What are the implications for implementation of the pattern and on the intention of the pattern?
  3. Describe a class structure you could use to represent a four-function (add, subtract, multiple, divide) math expression with parenthetical expressions using the Composite pattern. Provide the class structure diagram. How do you "execute" the expression? For the expression a = 3.0 * b + 2 * ( 6 + c ) / ( d + 4.0 ) show an object structure representation using your class structure assuming standard precedence rules.
  4. A broad definition of entropy is "the degree of disorder or uncertainty in a system". Why will the entropy of a design tend to increase over time? Describe how a Facade can be put to good use in a system with high entropy.
  5. Why may the use of a Facade make the building, i.e. compilation, of a system run quicker.
  6. One method of classifying iterators does so on two dimensions. One which indicates the location of control of the iteration (internal to the iterator or external client control) and a second which indicates the location of the definition of the iteration (embedded as part of the collection or separate from the collection). Considering each dimension separately, what are the positive and/or negative aspects of iterators of each type?
  7. What language constructs would you use to give iterators privileged access in Java and in C++? How will your answer depend on the classifications for iterators given above?
  8. Iteration over a recursive data structure such as a binary tree can be tricky using an external iterator. What are the problems with this? How can you accomplish this using an external iterator? What makes it easier with an internal iterator?
  9. Explain how the Observer pattern effectively isolates the observed from the observer.
  10. Draw sequence diagrams for the registration and update cycles of an Observer pattern implemented using appropriate Java classes. Be sure to label the object with the Java class and its role in the Observer pattern.
  11. The nature of the relationship between the Proxy and RealSubject is different for each of the four types of proxies described in the textbook. For each proxy type: What are the characteristics of that relationship for each proxy type? What interactions occur between the two classes? What capabilities must the relationship support?
  12. The intention for the State pattern includes the statement "The object will appear to change its class." Explain this. Explain how the structure of the pattern reinforces this.
  13. What issues must be considered if state objects are to be shared by multiple instances of the same Context class?
  14. The C++ implementation of class Adapter uses multiple inheritance. Using a diagram show how this could be implemented in Java. Be specific about the Java features that you are using, and the association between participants in the pattern and classes in your answer.
  15. Using examples not found in the text, myCourses, or discussed in class, describe the distinctions between the intentions of the Proxy and Adapter patterns. A simple statement of the intentions of both patterns will not receive high marks.
  16. Consider the Builder pattern. A new part is now able to be built into your product. This new part will be handled by all new ConcreteBuilders added to the system. What must change to accommodate the new parts? How can you let the current ConcreteBuilders ignore the part without rewriting any of those ConcreteBuilders? If an older ConcreteBuilder is in use and the Director requests for a new part type not known by the old builder to be put in the product what should happen?
  17. Argue against the statement "Coordinating behavior should be defined in the objects being coordinated." How does the Mediator pattern fit into this argument?
  18. "The Mediator pattern trades-off reduction of coupling among the objects it coordinates by lowering cohesion in the Mediator itself." Argue for or against this statement, using information drawn from the description of the Mediator pattern itself.
  19. Explain how Java's layout manager is an example of the Strategy class. In this example, what Java class or interface plays the role of each of the participants in the pattern?
  20. The Strategy object may inform the Context of the results of its work by either:
    1. Returning an object of an appropriate Result class to the Context, or
    2. Use a reference to the Context object and calling Context object methods to record the results.
    What are the relative advantages and disadvantages of each of these approaches? Consider issues such as encapsulation, coupling, and cohesion in forming your answer.
  21. Let methodA() and methodB() both be methods declared in the current class or one of its super classes, and assume that methodA() calls methodB(). Is methodA() always, sometimes, or never an example of the Template Method pattern? Justify your answer in terms of the pattern as presented in the text.
  22. Explain the "Hollywood Principle" in Template Methods. Why is access claimed to be inverted? Use an example of the Template Method pattern not found in the text, myCourses, or discussed in class.
  23. Explain why the Java input and output stream hierarchy is an example of the Decorator pattern. Draw the UML class diagram for the input stream hierarchy and show the correspondences to the participants in the generic Decorator class diagram. Draw a UML object diagram showing at least three objects acting as a run-time example of Decorator.
  24. Decorator claims to provide a pay-as-you-go approach to adding features. Do you agree that you can not get this same effect using class inheritance? Why do you agree or disagree?
  25. One of the methods all classes inherit from java.lang.Object is toString(), which can be overridden to provide a suitable string representation for any object in a given class. Is toString() a Factory Method? Referring to the intention and/or structure of the pattern why or why not?
  26. All Java collection classes implement the Iterable interface which defines one method, iterator(). Concrete collection subclasses must implement this method, returning an object to iterate over the collection's contents. Is iterator() a Factory Method? Referring to the intention and/or structure of the pattern why or why not?
    How does the Visitor pattern make use of delegation? Explain how you could use inheritance and polymorphism to implement similar functionality over the object structure. What are the disadvantages to this?
  27. What is double dispatch? Describe how the Visitor pattern implementsdouble dispatch.

Bonus Patterns

  1. Why is it considered difficult to add new products to the families defined by a set of Abstract Factories?
  2. Describe the attributes of what is commonly referred to as a global variable. What are their benefits? Disadvantages? Can you have global variables in Java? If yes, how do you define them? If no, what is the closest you can get? Why are Singletons considered the equivalent of global variables?
  3. What issues must be considered for the destruction of a Singleton?
  4. What does it mean to say a Memento object is opaque to its caretaker? What implications does this have on the caretaker's use of a memento?
  5. How might the Memento and Command patterns be used in combination to provide undo functionality in an editor system?