×

Loading...

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

本文发表在 rolia.net 枫下论坛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
Sign in and Reply Report

Replies, comments and Discussions: