/** * This obeys the Singleton design pattern. * Only one instance of Registry can be generated. * It is used by DateServer to keep track of the number of connections made. * The point is that each time the server requests an instance of * a Registry, it gets the same single instance. So that instance's * field for keeping track of the connection count keeps getting incremented. * The synchronization step is from Metsker's Java Workbook book. *
Singleton Design Pattern example for COM1204 Summer 2003. * * @author Bob Futrelle * @version 0.1, 5 July 2003 * */ public class Registry { private static Registry registry = null; // (the default in any case) private static final Object classlock = Registry.class; private int connectionCount; private Registry(){}; public void addToCount() { connectionCount++; } public void report() { System.out.println("Count now " + connectionCount); } public static Registry getRegistry() { synchronized(classlock) { if (registry == null) { registry = new Registry(); } return registry; } } // getRegistry } // class Registry