carfield.com.hk CharacterFlyweight.java 2004-03-24T16:00:00Z 2004-03-24T16:00:00Z <br/><TEXTAREA name="code" class="java" rows="16" cols="100">package examples.flyweight.java; /* -*- Mode: Java; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- * * This file is part of the design patterns project at UBC * * The contents of this file are subject to the Mozilla Public License * Version 1.1 (the &quot;License&quot;); you may not use this file except in * compliance with the License. You may obtain a copy of the License at * either http://www.mozilla.org/MPL/ or http://aspectj.org/MPL/. * * Software distributed under the License is distributed on an &quot;AS IS&quot; basis, * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License * for the specific language governing rights and limitations under the * License. * * The Original Code is ca.ubc.cs.spl.patterns. * * Contributor(s): */ /** * Implements a flyweight storing a single regular character * * @author Jan Hannemann * @author Gregor Kiczales * @version 1.0, 05/13/02 * * @see PrintableFlyweight * @see PrintableFlyweightFactory */ public class CharacterFlyweight implements PrintableFlyweight { /** * The character this flyweight represents */ char c; /** * Creates a new flyweight and sets it to represent a particular character * * @param c the character to represent */ public CharacterFlyweight(char c) { this.c = c; } /** * Defines the method signature for flyweights &lt;code&gt;print()&lt;/code&gt; * method. Implements the necessary method from the PrintableFlyweight * interface. * * @param c the character to print */ public void print(boolean uppercase) { System.out.print(uppercase ? Character.toUpperCase(c) : c); } }</TEXTAREA><br><br/><script type="text/javascript"><!--google_ad_client = "pub-9426659565807829";google_ad_slot = "9359905831";google_ad_width = 728;google_ad_height = 15;//--></script><script type="text/javascript" src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script> 2004-03-24T16:00:00Z FlyweightImplementation.java 2004-03-24T16:00:00Z 2004-03-24T16:00:00Z <br/><TEXTAREA name="code" class="java" rows="16" cols="100">package examples.flyweight.aspectj; /* -*- Mode: Java; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- * * This file is part of the design patterns project at UBC * * The contents of this file are subject to the Mozilla Public License * Version 1.1 (the &quot;License&quot;); you may not use this file except in * compliance with the License. You may obtain a copy of the License at * either http://www.mozilla.org/MPL/ or http://aspectj.org/MPL/. * * Software distributed under the License is distributed on an &quot;AS IS&quot; basis, * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License * for the specific language governing rights and limitations under the * License. * * The Original Code is ca.ubc.cs.spl.patterns. * * Contributor(s): */ import ca.ubc.cs.spl.pattern.library.FlyweightProtocol; /** * Implements a concrete instance of the flyweight pattern. * * * @author Jan Hannemann * @author Gregor Kiczales * @version 1.0, 05/13/02 * */ public aspect FlyweightImplementation extends FlyweightProtocol { /** * Assigns the &lt;i&gt;Flyweight&lt;/i&gt; role to CharacterFlyweight. */ declare parents: CharacterFlyweight implements Flyweight; /** * Assigns the &lt;i&gt;Flyweight&lt;/i&gt; role to WhitespaceFlyweight. */ declare parents: WhitespaceFlyweight implements Flyweight; /** * Actually creates the flyweight for a key. This method is called by * &lt;code&gt;getFlyweight(Object)&lt;/code&gt; if the flyweight does not already * exist. * * @param key the key identifying the particular flyweight * @returns the flyweight representing the key */ protected Flyweight createFlyweight(Object key) { char c = ((Character) key).charValue(); Flyweight flyweight = null; if (Character.isWhitespace(c)) { flyweight = new WhitespaceFlyweight(c); } else { flyweight = new CharacterFlyweight(c); } return flyweight; } /** * Provides a custom interface to access the flyweights. Refers to the * general &lt;code&gt;getFlyweight(Object)&lt;/code&gt; method defined on the * abstract aspect. * * @param c the character identifying the particular flyweight * @returns the flyweight representing the character */ public PrintableFlyweight getPrintableFlyweight(char c) { Character ch = new Character(c); return (PrintableFlyweight) getFlyweight(ch); } }</TEXTAREA><br><br/><script type="text/javascript"><!--google_ad_client = "pub-9426659565807829";google_ad_slot = "9359905831";google_ad_width = 728;google_ad_height = 15;//--></script><script type="text/javascript" src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script> 2004-03-24T16:00:00Z Main.java 2004-03-24T16:00:00Z 2004-03-24T16:00:00Z <br/><TEXTAREA name="code" class="java" rows="16" cols="100">package examples.flyweight.java; /* -*- Mode: Java; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- * * This file is part of the design patterns project at UBC * * The contents of this file are subject to the Mozilla Public License * Version 1.1 (the &quot;License&quot;); you may not use this file except in * compliance with the License. You may obtain a copy of the License at * either http://www.mozilla.org/MPL/ or http://aspectj.org/MPL/. * * Software distributed under the License is distributed on an &quot;AS IS&quot; basis, * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License * for the specific language governing rights and limitations under the * License. * * The Original Code is ca.ubc.cs.spl.patterns. * * Contributor(s): */ /** * Implements the driver for the flyweight design pattern example.&lt;p&gt; * * Intent: &lt;i&gt;Use sharing to support large numbers of fine-grained objects * efficiently.&lt;/i&gt;&lt;p&gt; * * Participating flyweight classes are &lt;code&gt;characterFlyweight&lt;/code&gt;, and * &lt;code&gt;WhitespaceFlyweight&lt;/code&gt;. Both implement the &lt;code&gt; * PrintableFlyweight&lt;/code&gt; interface. Flyweights are generated via the * &lt;code&gt;PrintableFlyweightFactory&lt;/code&gt;. &lt;P&gt; * * These are the pattern roles: &lt;UL&gt; * * &lt;LI&gt; CharacterFlyweightFactory: FlyweightFactory * &lt;LI&gt; PritableFlyweight: Flyweight * &lt;LI&gt; CharacterFlyweight: ConcreteFlyweight * &lt;LI&gt; WhitespaceFlyweight: ConcreteFlyweight * &lt;LI&gt; Main: Client * &lt;/UL&gt; * * Intrinsic state: The character to print, * Extrinsic state: Whether the char is upper case or lower case * * This example creates a sentence out of &lt;code&gt;PrintableFlyweight&lt;/i&gt;s * (characters and whitespaces). * * &lt;p&gt;&lt;i&gt;This is the Java version.&lt;/i&gt;&lt;p&gt; * * This implementation is analoguous to the books description. * For this version, we only use two classes for the flyweights, * CharacterFlyweight and WhitespaceFlyweight. Plus, this implementation * uses a better factory that keeps a hashtable of created Flyweights * and employs creation of demand. * * @author Jan Hannemann * @author Gregor Kiczales * @version 1.0, 05/13/02 * * @see PrintableFlyweight * @see PrintableFlyweightFactory */ public class Main { /** * Implements the driver for the flyweight design pattern example.&lt;p&gt; * * Intent: &lt;i&gt;Use sharing to support large numbers of fine-grained objects * efficiently.&lt;/i&gt;&lt;p&gt; * * This example creates a sentence out of &lt;code&gt;PrintableFlyweight&lt;/i&gt;s * (characters and whitespaces). */ public static void main(String[] args) { System.out.println(&quot;This is a test for the java Flyweight pattern &quot; + &quot;implementation.&quot;); System.out.println(&quot;The client will use char flyweights to print the &quot; + &quot;phrase&quot;); System.out.println(&quot;\&quot;This Is A Test\&quot;.\n&quot;); System.out.println(&quot;Testing Pattern: Flyweight - STARTING\n&quot;); PrintableFlyweightFactory pff = new PrintableFlyweightFactory(); PrintableFlyweight T = pff.getPrintableFlyweight('t'); PrintableFlyweight H = pff.getPrintableFlyweight('h'); PrintableFlyweight I = pff.getPrintableFlyweight('i'); PrintableFlyweight S = pff.getPrintableFlyweight('s'); PrintableFlyweight A = pff.getPrintableFlyweight('a'); PrintableFlyweight E = pff.getPrintableFlyweight('e'); PrintableFlyweight Empty = pff.getPrintableFlyweight(' '); // Printing: &quot;This Is A Test&quot; T.print(true); H.print(false); I.print(false); S.print(false); Empty.print(true); I.print(true); S.print(false); Empty.print(true); A.print(true); Empty.print(true); T.print(true); E.print(false); S.print(false); T.print(false); System.out.println(); System.out.println(&quot;\nTesting Pattern: State - FINISHED&quot;); } } </TEXTAREA><br><br/><script type="text/javascript"><!--google_ad_client = "pub-9426659565807829";google_ad_slot = "9359905831";google_ad_width = 728;google_ad_height = 15;//--></script><script type="text/javascript" src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script> 2004-03-24T16:00:00Z PrintableFlyweight.java 2004-03-24T16:00:00Z 2004-03-24T16:00:00Z <br/><TEXTAREA name="code" class="java" rows="16" cols="100">package examples.flyweight.java; /* -*- Mode: Java; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- * * This file is part of the design patterns project at UBC * * The contents of this file are subject to the Mozilla Public License * Version 1.1 (the &quot;License&quot;); you may not use this file except in * compliance with the License. You may obtain a copy of the License at * either http://www.mozilla.org/MPL/ or http://aspectj.org/MPL/. * * Software distributed under the License is distributed on an &quot;AS IS&quot; basis, * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License * for the specific language governing rights and limitations under the * License. * * The Original Code is ca.ubc.cs.spl.patterns. * * Contributor(s): */ /** * Defines the &lt;i&gt;Flyweight&lt;/i&gt; interface. Here, the flyweights are characters * that offer a single method: &lt;code&gt;print(boolean)&lt;/code&gt;. * * @author Jan Hannemann * @author Gregor Kiczales * @version 1.0, 05/13/02 * * @see PrintableFlyweight * @see PrintableFlyweightFactory */ public interface PrintableFlyweight { /** * Defines the method signature for flyweights &lt;code&gt;print()&lt;/code&gt; method * * @param uppercase whether the character is to be printed as uppercase */ public void print(boolean uppercase); }</TEXTAREA><br><br/><script type="text/javascript"><!--google_ad_client = "pub-9426659565807829";google_ad_slot = "9359905831";google_ad_width = 728;google_ad_height = 15;//--></script><script type="text/javascript" src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script> 2004-03-24T16:00:00Z PrintableFlyweightFactory.java 2004-03-24T16:00:00Z 2004-03-24T16:00:00Z <br/><TEXTAREA name="code" class="java" rows="16" cols="100">package examples.flyweight.java; /* -*- Mode: Java; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- * * This file is part of the design patterns project at UBC * * The contents of this file are subject to the Mozilla Public License * Version 1.1 (the &quot;License&quot;); you may not use this file except in * compliance with the License. You may obtain a copy of the License at * either http://www.mozilla.org/MPL/ or http://aspectj.org/MPL/. * * Software distributed under the License is distributed on an &quot;AS IS&quot; basis, * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License * for the specific language governing rights and limitations under the * License. * * The Original Code is ca.ubc.cs.spl.patterns. * * Contributor(s): */ import java.util.Hashtable; /** * Implements a flyweight factory that employs a creation-on-demand policy * * @author Jan Hannemann * @author Gregor Kiczales * @version 1.0, 05/13/02 * * @see PrintableFlyweight */ public class PrintableFlyweightFactory { /** * stores the existing flyweights by character they represent */ private Hashtable printables = new Hashtable(); /** * Returns the flyweight representing the argument character. * If the appropriate flyweight does not yet exist, it is created * on demand. * * @param c the character for which the the flyweight is returned * @returns the flyweight representing the character */ public PrintableFlyweight getPrintableFlyweight(char c) { Character ch = new Character(c); if (printables.containsKey(ch)) { return (PrintableFlyweight) printables.get(ch); } else { PrintableFlyweight flyweight = null; if (Character.isWhitespace(c)) { flyweight = new WhitespaceFlyweight(c); } else { flyweight = new CharacterFlyweight(c); } printables.put(ch, flyweight); return flyweight; } } } </TEXTAREA><br><br/><script type="text/javascript"><!--google_ad_client = "pub-9426659565807829";google_ad_slot = "9359905831";google_ad_width = 728;google_ad_height = 15;//--></script><script type="text/javascript" src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script> 2004-03-24T16:00:00Z WhitespaceFlyweight.java 2004-03-24T16:00:00Z 2004-03-24T16:00:00Z <br/><TEXTAREA name="code" class="java" rows="16" cols="100">package examples.flyweight.java; /* -*- Mode: Java; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- * * This file is part of the design patterns project at UBC * * The contents of this file are subject to the Mozilla Public License * Version 1.1 (the &quot;License&quot;); you may not use this file except in * compliance with the License. You may obtain a copy of the License at * either http://www.mozilla.org/MPL/ or http://aspectj.org/MPL/. * * Software distributed under the License is distributed on an &quot;AS IS&quot; basis, * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License * for the specific language governing rights and limitations under the * License. * * The Original Code is ca.ubc.cs.spl.patterns. * * Contributor(s): */ /** * Implements a flyweight storing a single whitespace character * * @author Jan Hannemann * @author Gregor Kiczales * @version 1.0, 05/13/02 * * @see PrintableFlyweight * @see PrintableFlyweightFactory */ public class WhitespaceFlyweight implements PrintableFlyweight { /** * The character this flyweight represents */ char c; /** * Creates a new flyweight and sets it to represent a particular character * * @param c the character to represent */ public WhitespaceFlyweight(char c) { this.c = c; } /** * Defines the method signature for flyweights &lt;code&gt;print()&lt;/code&gt; * method. Implements the necessary method from the PrintableFlyweight * interface. * * @param c the character to print */ public void print(boolean uppercase) { System.out.print(uppercase ? Character.toUpperCase(c) : c); } }</TEXTAREA><br><br/><script type="text/javascript"><!--google_ad_client = "pub-9426659565807829";google_ad_slot = "9359905831";google_ad_width = 728;google_ad_height = 15;//--></script><script type="text/javascript" src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script> 2004-03-24T16:00:00Z files.lst 2004-03-24T16:00:00Z 2004-03-24T16:00:00Z <br/><br/><script type="text/javascript"><!--google_ad_client = "pub-9426659565807829";google_ad_slot = "9359905831";google_ad_width = 728;google_ad_height = 15;//--></script><script type="text/javascript" src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script> 2004-03-24T16:00:00Z