Jabber's toy code--002---Beep, beep, beep!

jabber (jabber)
本文发表在 rolia.net/zh 相约加拿大网上社区枫下论坛
Here is a way to have your machine beep.
//other lines
public static void main(String args[]) {
Toolkit kit = Toolkit.getDefaultToolkit();
kit.beep();
}

Really dummy code.One day in March, I put the above beeping code into an infinite while-loop, lauched my JVM, and then went out for lunch with some friends. After I came back, my co-worker sitting in the same office asked me: " What's wrong with your computer? My ear....".

Actually, there is something we can learn from the above dummy code. The design and implementation of Toolkit in JVM follow the Abstract Factory Pattern. Actually, Abstract Factory is also called Toolki Pattern, or Kit Pattern.

Here are some points.
JVM needs a Toolkit to do something like beeping, but toolkit uses system resources such as speakers. Hence, there are different Toolkits on different native machines, ----Unix, Windows, Mac, just name a few.
For this reason, Toolkit is an abstract class. Pay attention to the word "abstract". There are different extensions of this abstract class for JVMs on Windows, Unix, Linux. We application programmers are not interested in how to implement these things. So JDK library provides a method called Toolkit#getDefaultToolkit() for getting a reference to the Toolkit. We are forbidden to use constructors of subclasses. In doing so, there is a unified interface---Toolkit---on different operating systems.
更多精彩文章及讨论,请光临枫下论坛. 网址: rolia.net/zh
(#108091@0)
2001-6-21 -05:00

回到话题: Jabber's toy code--002---Beep, beep, beep!

回到论坛: HOME枫下论坛枫下论坛主坛工作学习IT杂谈

URL:   
http://www.rolia.net/zh/post.php?f=0&p=108091