Monday, October 1, 2007

Example of Abstract Factory Pattern

The abstract factory pattern is another pattern that I have worked with in the past, although at the time I did not know that I was in fact using it. In my interaction design class, I recreated several "Preferences" interfaces for common programs using the swing API. The abstract factory pattern is implemented with "look and feel". When programming a graphical interface using JComponents, I do not need to specify what type of JComponent needs to be created because this is already built into the abstract factory (look and feel) which is specified at one point in the program. This enables similar objects to be created without specifying their concrete class. For example, this is useful if I wanted to create a graphical interface for some sort of "Preferences" menu which contains many JButtons, JLabels, etc., and I wanted it to be used on several platforms but using that platform's native look and feel. Rather than repetitive code such as "new MacButton()" and "new WindowsButton()", all I would need to do is "if (OS=="Mac"){changeLookAndFeel("Mac")}". This would shorten code tremendously and create a lot less confusion and chances to make errors.

No comments: