Object Sharing

  1. The Navigator, Internet Explorer, and HotJava browsers run all Java applets inside of a common runtime environment. This means that all static data is shared (including classes themselves).

    We can take advantage of this to allow sharing of data between instances of an applet. Usually this sharing takes the form of an object cache for objects of a given type.

  2. An object cache can be implemented as static Hashtable member of a class. Access is through a synchronized static method, which creates the Hashtable if it has not yet been created, and creates requested objects if they are not already in the Hashtable:
    Hashtable documentCache;
    
    synchronized DVIDocument getDocument( URL documentURL ) {
        if( documentCache == NULL )
            documentCache = new Hashtable( );
        
        DVIDocument result = documentCache.get( documentURL );
       
        if( result == NULL ) {
            result = new DVIDocument( documentURL );
            documentCache.put( documentURL, result );
        }
    
        return result;
    }

  3. The Hashtable should not be initialized where it is defined, since the static initialization method is apparently not synchronized by default. This can cause repeated re-initialization of static objects.

  4. The fast synchronized initialization scheme should not be used for constructing the hashtable, since we need to synchronize the construction of the object being retrieved as well.

  5. This idiom is used whenever there should be a unique object of a class for each set of parameters. For example, it is used in the construction of DVIDocument objects (one per document URL), DVIFont objects (one per font name and size), ImageBlock objects (one per image URL), CharacterBlock objects (one per DVICharacter object), RuleBlock objects (one per width, height pair), CharacterView objects (one per ColorScheme, CharacterBlock, scale triple), RuleView objects (one per ColorScheme, scale, width % scale, height % scale 4-tuple), ColorScheme objects (one per foreground color, background color, link color, selected link color, selected text color 5-tuple), and ScaledColorScheme objects (one per ColorScheme, scale pair).