Flyweight
                  2011/4/25




2011   4   25
Flyweght
                =


2011   4   25
2011   4   25
2011   4   25
Flyweight



                            new




2011   4   25
Flyweight


                       (    )




2011   4   25
@Test
                	 public void Integer                       () throws Exception {
                	   	   Integer   integer1_first = Integer.valueOf(1);
                	   	   Integer   integer2 = Integer.valueOf(2);
                	   	   Integer   integer1_second = Integer.valueOf(1);
                	   	   Integer   integer1_third = new Integer(1);
                	   	   //
                	 	 assertTrue(integer1_first == integer1_second);
                	 	 //
                	 	 assertFalse(integer1_first == integer2);
                	 	 //
                	 	 assertFalse(integer1_first == integer1_third);

                	 }




2011   4   25
public static Integer valueOf(int i) {
                        assert IntegerCache.high >= 127;
                        if (i >= IntegerCache.low && i <= IntegerCache.high)
                            return IntegerCache.cache[i + (-
                IntegerCache.low)];
                        return new Integer(i);
                    }




2011   4   25
...

                	 @Test
                	 public void Integer                   () throws Exception {
                	 	 Integer integer127_first = 127;
                	 	 Integer integer127_second = 127;
                	 	 assertTrue(integer127_first == integer127_second);

                	 	 Integer integer128_first = 128;
                	 	 Integer integer128_second = 128;
                	 	 //
                	 	 assertFalse(integer128_first == integer128_second);
                	 }




2011   4   25
Flyweight
                  (Immutable)
                                  java.lang.Integer
                java.math.BigDecimal




2011   4   25
Enum




2011   4   25
2011   4   25
2011   4   25
•            Flywight

                    • http://www.hyuki.com/dp/
                      cat_Flyweight.html




2011   4   25

Flyweight

  • 1.
    Flyweight 2011/4/25 2011 4 25
  • 2.
    Flyweght = 2011 4 25
  • 3.
    2011 4 25
  • 4.
    2011 4 25
  • 5.
    Flyweight new 2011 4 25
  • 6.
    Flyweight ( ) 2011 4 25
  • 7.
    @Test public void Integer () throws Exception { Integer integer1_first = Integer.valueOf(1); Integer integer2 = Integer.valueOf(2); Integer integer1_second = Integer.valueOf(1); Integer integer1_third = new Integer(1); // assertTrue(integer1_first == integer1_second); // assertFalse(integer1_first == integer2); // assertFalse(integer1_first == integer1_third); } 2011 4 25
  • 8.
    public static IntegervalueOf(int i) { assert IntegerCache.high >= 127; if (i >= IntegerCache.low && i <= IntegerCache.high) return IntegerCache.cache[i + (- IntegerCache.low)]; return new Integer(i); } 2011 4 25
  • 9.
    ... @Test public void Integer () throws Exception { Integer integer127_first = 127; Integer integer127_second = 127; assertTrue(integer127_first == integer127_second); Integer integer128_first = 128; Integer integer128_second = 128; // assertFalse(integer128_first == integer128_second); } 2011 4 25
  • 10.
    Flyweight (Immutable) java.lang.Integer java.math.BigDecimal 2011 4 25
  • 11.
  • 12.
    2011 4 25
  • 13.
    2011 4 25
  • 14.
    Flywight • http://www.hyuki.com/dp/ cat_Flyweight.html 2011 4 25