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