Thursday, July 9, 2015

Effective Java--1)Create/Destroy Object: static factory method

Static factory method

Static factory is another way to create a class instance other than constructor. [1] shows an example:
 public static Boolean valueOf(boolean b){  
   return b? Boolean.TRUE:Boolean.FALSE;  
 }  

Static factory is a special kind of static method. Static factory returns a class instance.
Pros:
  • Static factory has name. Constructor doesn't have name, sometimes may call the wrong constructor.
  • Do not require to create a new object each time they are invoked.
    • Like Flyweight technique, greatly improve performance if equivalent objects are requested often, especially when they are expensive to create.
  • Can return any subtype of their return type.
    • Like EnumSet returns a single long if no more than 65 elements; otherwise backs a long array.
  • Static factory reduces the verbosity of creating parameterized type instance.
    • Constructor flavor:
       public static Boolean valueOf(boolean b){  
         return b? Boolean.TRUE:Boolean.FALSE;  
       }  
      
    • Static factory method flavor:
      
      Map<String,List<String>> m = HashMap.newInstance();
      public static <K,V> HashMap<K,V> newInstance(){ return new HashMap<K,V>;}
      
Cons:

  • Class without public/protected constructor cannot be subclassed.
  • Not really distinguishable from other static method.

Tuesday, May 5, 2015

On the way to my dream,tick-tock,tick-tock~~

For such a long time, I have struggled to fit in the "best" life. I tried the best to adjust external changes. I don't always complain about that. But, it is easy to fall in sadness. I don't want to live in that way any more.

I want to live my own life not belong to anyone else. I love the movie "Sydney White". I love the way they treat life and face themselves. They could speak loudly that "I'm a dork". Why cannot I face my flaws and then find my lengths. I should do that, right now.

I want to become an excellent software engineering. I want to have my puppy.

I will find my dream work and dream company. Not just from the top ranking.

I have acnes and fight with them. I dream of being a dog walker and comic drawer.

I love Hulk. Pretty much. "I'm always angry~~". Lalala~~~ :)

Feel the light in my heart and listen to my self. Thanks ooh. Thanks Home. I love comic. There's no one else could help me but myself.