heavyweight.java

来自「Conservator extends on the concept of a 」· Java 代码 · 共 115 行

JAVA
115
字号
     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 + =
减小字号Ctrl + -
显示快捷键?