📄 flyweight.java
字号:
package com.orbs.ref.pattern.conservator;
import java.io.Serializable;
/*********************************************************************
* <P>
* 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.
* <P>
* The Flyweight, unlike the Heavyweight, implements the interface
* to access the intrinsic state but not extrinsic state.
* <P>
* @author
* <A HREF="http://www.alumni.caltech.edu/~croft">David W. Croft</A>
* @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.
* <P>
* 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 );
}
//////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -