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.

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.

Sunday, September 23, 2007

The Composite Pattern

At this point in time, my group senior project is starting to take shape, our requirements document is almost finished and then we will begin to work on our design architecture. Until we start to work on the design of our project, I can only study design patterns that have a potential for being used. I thought chapter two in Design Patterns: Elements of Reusable Object-Oriented Software was helpful because it walked through a case study which brought up problems with designing and described how certain design patterns can be used to solve these problems. One design that I read about that I already see a use for is the composite pattern. Although graphics for my senior project are not part of my responsibilities, I will still discuss with my group members the benefits, structure, and implementation of using the composite pattern. Since our senior project is going to be a computer game, we will have several menus throughout the game which will be a composite of different several leafs and even other composites. This will allow us to treat individual objects and compositions of objects uniformly.

Wednesday, September 12, 2007

What Will I Be Working On This Semester?

So far, I have read the introduction to the book, Design Patterns: Elements of Reusable Object-Oriented Software, and got a good idea of what this book is all about. I also had a discussion with Dondi on how I will use the information learned from this class for my CMSI 401: Senior Project class. The textbook I am reading is basically a catalog of different types of design patterns and describes the intent, motivation, applicability, structure, consequences, and implementation of each of the design patterns. Dondi gave me a list of certain design patterns which he feels may be useful in my senior project. Hopefully, by reading through these design patterns and becoming familiar with them, I will be able to recognize the need for them, as well as actually implement them in my senior project.

Thursday, September 6, 2007

Just Getting Started

When I was registering for Fall 2007 classes, I didn't know which CMSI electives to take, but I knew I was interested in object oriented type concepts. Object technologies was the perfect class for this. According to the course description, I will be learning "Software design and implementation using object oriented and component technologies." This course should also help me in my CMSI 401 and 402 classes this school year since senior project labs are basically two software pieces which both use object oriented technologies. So far, I have not done any work for the class (besides write this blog entry) due to slow processing speeds at the amazon.com textbook store. As soon as I receive my books for this class, I will begin reading the books, updating this blog, discussing the books with Dondi, and coming up with a programming project.