package com.orbs.ref.pattern.conservator; import java.io.Serializable; /********************************************************************* *
* For a Flyweight to be used with a Conservator, it absolutely must * override its equals() method to return true if the values are the * same and it must be Cloneable. *
* The Flyweight, unlike the Heavyweight, implements the interface * to access the intrinsic state but not extrinsic state. *
* @author * David W. Croft * @version * 1998-04-19 *********************************************************************/ public class Flyweight implements Intrinsic, Cloneable, Serializable ////////////////////////////////////////////////////////////////////// // Serializable just in case you want to save it. ////////////////////////////////////////////////////////////////////// { private String audioFileName; private String imageFileName; ////////////////////////////////////////////////////////////////////// // Constructor method ////////////////////////////////////////////////////////////////////// public Flyweight ( String audioFileName, String imageFileName ) ////////////////////////////////////////////////////////////////////// { this.audioFileName = audioFileName; this.imageFileName = imageFileName; } ////////////////////////////////////////////////////////////////////// // Accessor and mutator methods ////////////////////////////////////////////////////////////////////// public String getAudioFileName ( ) { return audioFileName; } public String getImageFileName ( ) { return imageFileName; } public void setAudioFileName ( String audioFileName ) { this.audioFileName = audioFileName; } public void setImageFileName ( String imageFileName ) { this.imageFileName = imageFileName; } ////////////////////////////////////////////////////////////////////// // Overridden methods ////////////////////////////////////////////////////////////////////// /********************************************************************* * Creates a shallow clone. Done by returning a new Flyweight with * the current instance variables passed as the arguments to the * constructor. *********************************************************************/ public synchronized Object clone ( ) ////////////////////////////////////////////////////////////////////// // Synchronized for your protection. ////////////////////////////////////////////////////////////////////// { return new Flyweight ( audioFileName, imageFileName ); } /********************************************************************* * Returns true if the argument is an instance of Flyweight * (including subclasses) and its instance values are equal. *
* If you subclass Flyweight, be sure to override this method by * calling the superclass method first and then comparing the * additional instance variables that are available in the subclass. *********************************************************************/ public synchronized boolean equals ( Object other ) ////////////////////////////////////////////////////////////////////// // Synchronized for your protection. ////////////////////////////////////////////////////////////////////// { if ( other == null ) return false; if ( other == this ) return true; if ( !( other instanceof Flyweight ) ) return false; Flyweight that = ( Flyweight ) other; if ( ( this.audioFileName == null ) && ( that.audioFileName != null ) ) return false; if ( ( this.imageFileName == null ) && ( that.imageFileName != null ) ) return false; return this.audioFileName.equals ( that.audioFileName ) && this.imageFileName.equals ( that.imageFileName ); } ////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////// }