Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 5 of 5

Thread: Flyweight Design Pattern

  1. #1
    Junior Member
    Join Date
    Sep 2013
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Flyweight Design Pattern

    I read that Java String and Image classes implements 'Flyweight Design' pattern. I know, how to test this for a String class. But, I could not find a way to check this for Image class. My idea is that I will upload an image say "dog.png" into two image objects and then will compare the two image objects ( as shown below). If both objects are equal, this indicates that Java implements 'Flyweight' pattern, otherwise vice-versa.



    Image img1= new Image("dog.png");
    Image img2= new Image ("dog.png);

    System.out.println( img1== img2) // should print 1, however it prints 0

    The issue is when I execute the "new" statement, Java creates a fresh copy of the object, and there is no "intern" method (like String Class) is available. Do you have any idea that how one may check that Java Image class uses "Flyweight" pattern


  2. #2
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    4,961
    My Mood
    Hungover
    Thanks
    133
    Thanked 553 Times in 474 Posts

    Default Re: Flyweight Design Pattern

    Why do you think you need to do this?

    The flyweight design has nothing to do with equality. Your test would also fail with Strings.

    You could create a thousand instances of a large image and compare the memory consumption to that of creating a single instance. But again, why do you think you need to do this?
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. #3
    Junior Member
    Join Date
    Sep 2013
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Flyweight Design Pattern

    No, for String it works.

    Lets run the below code.

    String str1= "flyweight";
    String str2= "flyweight";
    System.out.println(str1==str2); // the output is true

    The output indicates that Java-Compiler applied "flyweight" pattern to String objects, when ever it is possible.

    Now, your question that why I need to do this for image Class. I want to do this for some 'odd reasons'

  4. #4
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    4,961
    My Mood
    Hungover
    Thanks
    133
    Thanked 553 Times in 474 Posts

    Default Re: Flyweight Design Pattern

    You're comparing apples and oranges. You're using the new keyword with the images, but not with the Strings. Try this:

     public class Main{
    	 public static void main(String... args){
     
    		 String test1 = new String("test");
    		 String test2 = new String("test");
     
    		 System.out.println(test1 == test2);
    	 } 
    }

    Or even more similar to your image example:

     
     public class Main{
    	 public static void main(String... args){
     
    		 StringWrapper test1 = new StringWrapper("test");
    		 StringWrapper test2 = new StringWrapper("test");
     
    		 System.out.println(test1 == test2);
    	 } 
    }
     
     class StringWrapper{
     
    	 String s;
     
    	 public StringWrapper(String s){
    		 this.s = s;
    	 }
     }

    The point is, using equality to test for the flyweight design is not really valid.

    Further, there is no concrete Image class, so some subtypes of Image could use the flyweight design and others could not. It might even be platform dependent!
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  5. #5
    Super Moderator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,097
    Thanks
    169
    Thanked 779 Times in 725 Posts
    Blog Entries
    5

    Default Re: Flyweight Design Pattern

    1) Take a look at the API for the intern method of the String Class
    2) Take a look at the API for the ImageIO class, pay special attention to the methods with the name 'cache' in them

Similar Threads

  1. Design Pattern for Online booking
    By tcstcs in forum Java Theory & Questions
    Replies: 0
    Last Post: January 14th, 2013, 04:21 AM
  2. decorator design pattern
    By chalapathi in forum Java Theory & Questions
    Replies: 2
    Last Post: May 8th, 2012, 03:39 PM
  3. What is difference between Design Pattern and FrameWork ?
    By casperl90 in forum Web Frameworks
    Replies: 2
    Last Post: August 8th, 2011, 03:17 AM
  4. Regular Expression pattern - complex pattern syntax
    By SmartAndy in forum Algorithms & Recursion
    Replies: 3
    Last Post: June 7th, 2011, 04:40 AM
  5. Optimizing Singleton Design Pattern
    By tcstcs in forum Java Theory & Questions
    Replies: 4
    Last Post: April 19th, 2011, 02:13 AM