package com.orbs.ref.pattern.conservator; import java.io.Serializable; import java.awt.Point; /********************************************************************* *

* Heavyweight, unlike Flyweight, implements the interfaces to access * both the extrinsic and the intrinsic state. *

* @author * David W. Croft * @version * 1998-04-22 *********************************************************************/ public class Heavyweight implements Extrinsic, Intrinsic, Serializable ////////////////////////////////////////////////////////////////////// // Serializable just in case you want to save it. ////////////////////////////////////////////////////////////////////// { private Conservator conservator; private Flyweight flyweight; private Point location; ////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////// public Heavyweight ( Conservator conservator, String audioFileName, String imageFileName, Point location ) ////////////////////////////////////////////////////////////////////// { this.conservator = conservator; this.flyweight = ( Flyweight ) conservator.conserve ( new Flyweight ( audioFileName, imageFileName ) ); this.location = location; } ////////////////////////////////////////////////////////////////////// // Accessor methods ////////////////////////////////////////////////////////////////////// public String getAudioFileName ( ) ////////////////////////////////////////////////////////////////////// // I pass this request off to the delegate. ////////////////////////////////////////////////////////////////////// { return flyweight.getAudioFileName ( ); } public String getImageFileName ( ) ////////////////////////////////////////////////////////////////////// // I pass this request off to the delegate. ////////////////////////////////////////////////////////////////////// { return flyweight.getImageFileName ( ); } public Point getLocation ( ) ////////////////////////////////////////////////////////////////////// // This request I can handle myself. ////////////////////////////////////////////////////////////////////// { return location; } ////////////////////////////////////////////////////////////////////// // Mutator methods ////////////////////////////////////////////////////////////////////// public synchronized void setAudioFileName ( String audioFileName ) ////////////////////////////////////////////////////////////////////// // I secretly clone the shared flyweight object and modify the clone // instead. I then pass it to the conservator and reset my flyweight // delegate to the returned object. // // Synchronized for your protection. ////////////////////////////////////////////////////////////////////// { Flyweight clone = ( Flyweight ) flyweight.clone ( ); clone.setAudioFileName ( audioFileName ); this.flyweight = ( Flyweight ) conservator.conserve ( clone ); } public synchronized void setImageFileName ( String imageFileName ) ////////////////////////////////////////////////////////////////////// // I secretly clone the shared flyweight object and modify the clone // instead. I then pass it to the conservator and reset my flyweight // delegate to the returned object. // // Synchronized for your protection. ////////////////////////////////////////////////////////////////////// { Flyweight clone = ( Flyweight ) flyweight.clone ( ); clone.setImageFileName ( imageFileName ); this.flyweight = ( Flyweight ) conservator.conserve ( clone ); } public void setLocation ( Point location ) ////////////////////////////////////////////////////////////////////// // I do nothing special for this extrinsic state variable. ////////////////////////////////////////////////////////////////////// { this.location = location; } ////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////// }