Friday, July 18, 2008

Google Guice - 谷歌的果汁

Guice (pronouced 'juice') is a new dependency injection framework from Google. The biggest difference between Guice and other DI framework is the annotation. Guice does not use xml configuration. Instead, you mark your 'injectee' with the @Inject annotation. This annotation tell Guice: "Hello injector, I need your help. Try to find the dependencies for me".  The concept is very simple, and it really takes only 10 minutes to get started.  Of course, this assumes you already know what DI is, which might take a while to put it into practice.

The class dependency is organized in a Module. In the module, you bind an interface to a concrete implementation.  Just like all DI framwork, the purpose is to avoid using the new operation.  When you say Animal a = new Dog(); you hardcoded the Dog type in the source code.  If you run it the Guice way, you will create a AnimalModule,  and bind the Dog object within the AnimalModule. Next time when you want to switch the implementation, simply change the Module, not your code.

 public class AnimalModule implements Module {

public void configure(Binder binder) {

binder.bind(Animal.class).to(Dog.class);

}

}


Guice also provide support of field injection. You can initialize field values in a module, even for private fields.  Unlike Spring, which want to be a replacement of everything, Guice limit itself to be a simple layer focus on dependency injection.

If you really want to know the disadvantage of Guice compare to other pre Java 5 DI frameworks, the only thing I can think of is the jar dependency.  Once you decide to use Guice annotation, you must include guice jar file in your classpath.  Some other DI framework like Spring or Pico can retrofit into old classes without touching the code.  This is not the case for Guice.

If you want to start a new project and want to have a light DI layer quickly, Guice is really worth trying.




Thursday, July 17, 2008

The evil File/Save

Recently I discussed with my friend about desktop application design. In my opinion, most of the File/Save are bad design for a GUI-based system.

1. A good application should try to hide the technical detail such as the file system in the operating system. File concept should be avoided whenever possible. The famous example is the Quicken financial software. When you open Quicken, you see your balance book. There is no need to open Quicken, and then open again for a file. 

2. If the application designer intends to use the File Folder metaphor in real life, he should avoid  File/Save because there is no such metaphor in real life as Save a file. When you file something, it is there. 

3. A document-based design should treat document as a physical document: They exists and persists as soon as they are created.  A user should not lost the whole document because he forgot to save it.  Someone may argue that a user might regret his changes and want to revert back to original file. There are plenty of technique to overcome this issue: Use of command pattern, journaling, or simply store multiple copies. They are all better than save-it-or-lose-it solution.

4. User does not care if something is in the memory or in the storage. Developers does. To explain File/Save activity often involved in explanation of memory and storage, RAM and HDD.  These is too much information for the users.

As a matter of fact, even for developer tools, there is rarely need for File/Save. Eclipse IDE (or Visual Age) is a good example.  In object-oriented language, you always work with class, not files. When coding is done, you compile it into class, not just save it. 

So next time when we design an application menu, think first if we really need that File/Save menu. Will that cause user to lose their work? Will we involved in the embarrassing moment to explain what is RAM and what is HDD to the user?

Monday, July 14, 2008

Stress testing gone wild

Last Friday iPhone launch event became iPocalypse - phone activation server was under heavy load, so heavy it becomes denial-of-service attack.

I am very sure Apple did the full scale stress testing before the launch. However, I think this iPocalypse is more a project planning mistake than the technology issue. Why does Apple decide to launch the firmware 2.0 update on the exact same day of selling iPhone 3G?  I really appreciate Apple's intention of taking care of existing customers like me.  I feel loved. But to be honest, we already paid one year ago and enjoyed the iPhone for a year.  If we are not in the 3G line, that means we don't have to upgrade to 3G.  Why not just let the old customer wait for one or two days? 

In reality, a stress-tested system gone wild when one of the execution path holding the resource.  When this happens, rebooting server will only solve the problem for a few seconds.  When users see "retry later" message, they will hit retry button more, not less. The longer user waits, the faster they hit the retry button.  This is human nature of dealing with frustration. Apparently you can change the error message to "Do not retry. It's not gonna work", but a more practical solution is to sit with your network admin or static web server admin to figure out a strategy to regulate the traffic.  If you have a controlled user (such as the iPhone case), it would be even better to organize the users.  

Anyway, I picked the right time to active my iPhone (early morning Sunday Eastern time) and I live happily with my iPhone ever after.


Saturday, July 12, 2008

iPhone OS 2.0 - Apple Remote


iPhone 2.0 update is released.  My first download application from App Store is the Apple Remote. It is an amazing replacement of my tiny white 3-button remote that come with the Mac, and this remote has even less buttons: zero! Your iPhone becomes the ultimate remote control for your iTunes.  It also supports Apple TV. Now I am tempted to by an Apple TV.

There is also one handy trick from iPhone Atlas for taking screenshot in this version.  Press both home button and sleep/wake button simultaneously, you will see a blink on the screen, and then you will see a screenshot in the Photos. 

The new iPhone 2.0 update also allow you to write Chinese characters using finger. In general it is a very impressive update. I feel like having a new phone again.


Saturday, July 05, 2008

『長』不忍睹的金曲獎

十九屆金曲獎巨星雲集,但是還是太慢了。舞台上的幾個Golden Melody Award大字醜到不行。林宥嘉因為聽不到耳機裡的監聽聲音唱天天想你走音,不知道為什麼安排家家唱 I will always love you,是要紀念惠妮休斯頓嗎? 喜劇的天王高凌風跟康康在回音監聽不良的舞台顯得冷場,來賓頒獎人的致詞長又沒有人制止。 最大的得獎人在溫州開演唱會。不過後半段開始變快。張惠妹依然精彩,羅大佑李宗盛也是很有看頭,但是羅大佑鋼琴的音量竟然沒有開。這是哪一家舞台工程公司做的啊,怎麼會狀況這麼多?林宥嘉康康高凌風羅大佑這些表演者都是身經百戰的專業人員,卻被一個不紮實的舞台工程給打敗了。

得獎人立刻表演是蠻好的傳統。評審也很有眼光,選了一些我聽得懂的得獎人,不像以前總有幾個獎明顯是配合政府愛台灣。

聽說東風作這個典禮是賠本的。辛苦了。若不是有這幾位天王天后撐著,只一場只能算是同樂會或是廟會吧。新聞局是不是可以增加一點預算呢? 流行音樂是台灣文化最後一個強項,不要這樣省吧。