Thursday, November 1, 2007

Example of the Adapter Pattern

The adapter pattern was another pattern that I felt I've seen before, the first thing that came to mind were event listeners. I then searched through the Java 1.5 API and found a class that works as an adapter, WindowAdapter. The point of the adapter pattern is to convert the interface of a class into another interface that the client expects. In the case of WindowAdapter, WindowAdapter would be the adapter with an interface the client expects. Within WindowAdapter, there are various methods such as windowClosed and windowOpened, these are the methods that make calls to the Adaptee. So the client makes a call where the adapter then calls the method of the adaptee. This way you can use an existing class when its interface doesn't match the class that is needed.

Example of the Factory Method Pattern

After reading about the factory method pattern, nothing that I have done in the past really jumped out at me so I decided to look through the Java 1.5 API and found an example of a class that follows the factory method pattern. This class wasn't to hard to find since the name has "Factory" in it, ObjectFactory. The actual method in the ObjectFactory class that follows the factory method pattern is getObjectInstance(Object obj, Name name, Context nameCtx, Hashtable environment), which returns an object. The point of the factory method pattern is to create objects without specifying the exact class of object that will be created. This method is used to allow subclasses to choose which type of object to create.

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.