Name: ______________________________ Date: ______________________________
Software Engineering Specialization
Exercises: Design Patterns
Exercise 1: An object is loosely coupled if it is dependent on only a few other objects and/or the
dependencies are abstract. In the Observer design pattern, the subject may have
hundreds of observers (dependencies). In light of this, explain why the Observer
design pattern is said to be loosely coupled.
Exercise 2: Consider the code for the Singleton design pattern given below. Modify this code so
that at most 5 instances are created and each instance can be individually referenced.
Note that the keyword static indicates a class attribute/operation.
public class Singleton {
private static Singleton instance = null;
private Singleton() { }
public static Singleton getInstance() {
if (instance == null) {
instance = new Singleton();
}
return instance;
}
}