Wednesday, October 24, 2007

Example of the Observer Pattern

The first thing that came to my mind after reading about the observer pattern was a program that Dondi showed our class in CMSI 370 (Interaction Design) called drip. This program had multiple "meters" that used the same data, so when one meter was manipulated, all the other ones would be changed as well. This can be done with the observer pattern without tightly coupling objects. An example I found in the java core api was actually the interface called Observer. Classes that implement Observer are notified of changes that happen to objects that are observable.

Example of the Command Pattern

After first reading about the command pattern, I did not realize that I have used it many times before this. Since the command pattern deals with encapsulating actions as objects, I thought a good place to look for this pattern was a program dealing with user interaction. I pulled up some of my old homework assignments from CMSI 370 (Interaction Design) and noticed that ActionListener was what I was looking for. ActionListener is the same as the Command class that is used in the diagrams in the book, Design Patterns: Elements of Reusable Object-Oriented Software. And the actionPerformed() method is the Execute() method.

Sunday, October 14, 2007

Example of Strategy Pattern

After learning about the strategy pattern, I started to think about where I have already seen this design pattern implemented before. I then realized that Patrick Falls' senior project for CMSI 402, Project M.O.S.A.I.C., is a perfect example of the strategy pattern. Patrick's project was basically a utility that used algorithms for creating mosaic like images that start with an actual image and proceed to recreate that image in the style of a mosaic by using other images as tiles. The feature of Patrick's project that used the strategy pattern was the ability to use pluggable algorithms that a user may choose on the fly. These algorithms are encapsulated as an object that can be interchanged.

Tuesday, October 2, 2007

Example of Template Method Pattern

The template method pattern is a commonly used pattern that is very useful when you want a class or program to follow a certain guideline, but don't want it to be tied down to a specific behavior. I am currently taking a web applications class with Dr. Toal, and have discovered (with the help of Dondi) that the spring framework contains an example of the template method pattern. Within the spring framework, there are several abstract controller classes which act as templates. A specific example is the AbstractController, which serves as a superclass that provides functionality for sublcasses to use, as well as abstract template methods for subclasses to override and implement themselves.

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.

Example of Composite Pattern

Throughout my computer science career (freshman year through senior year) I have dealt with several APIs, but I had not recognized the fact that they are implemented with certain design patterns in mind. A good example of the composite pattern can be found in the swing API. JPanel for example, is a component that can contain within itself other components such as JButton, JLabel, etc... What makes JPanel follow the composite pattern is the fact that it can also contain other JPanels, which makes up a composition of components. The top level component doesn't have to know about what's contained within it's contents. Let's say a top level JPanel contains another JPanel which contains several other JComponents, the top level JPanel calls the paint() function, which calls paint() on its composition.