Friday, March 14, 2008

Lists and Patterns

For this lab I started by constructing a simple doubly-linked list. Since we did this in Lab, it wasn't particularly hard to recreate on my own. I then tried to get a printVisitor working because while I had gotten it to work in lab, I wasn't really happy with my result and felt that it was slightly hacked together. I had better luck getting the visitor to work on my own and then embarked on getting the decorator pattern to wrap the list class with decorations for Node and Emptylist. This proved to be slightly more difficult than I had initially anticipated because of the fact that the wrappers of the Decorator pattern make it difficult to use the base wrapped classes methods because we change the class of the object to that of the wrapper. I eventually got this working, but don't really think that the decorator is a very good pattern to use with lists. I do think that it was a valid and worthwhile exercise getting it to work though, because in my project I mostly scrapped the decorator after the Lab 7 clean-your-room extravaganza.

Monday, March 3, 2008

GUI Recipe (from 3/3 lab)

1.) Create window(s)
JFrame

2.) Choose a layout

3.) Create content
Extend JPanel

4.) Add content to windows
add panels/components into frame

Friday, February 29, 2008

Visitor plus one

In this weeks assignment I added the visitor pattern and the singleton pattern to my project.

I have one Antivirus visitor that has two states (scanning and disinfecting) and visits computers and a Name visitor that visits with viruses and assigns them random names generated from several arrays and Random.nextInt().

To implement the visitor pattern, you first need to decide what objects will be the visitees and provide setters and getters for any state changes that need to be performed by the visitor. Then you have to add an acceptVisitor(Visitor v) method to each visitee class that tells the visitor v to visit it. You should create a visitor interface that has methods for visiting each class of visitee and implement this in each of your visitors. In each visitor, you must implement the methods in the visitor interface and then can perform actions on the visitee. 

Antivirus seemed like a natural place for the visitor pattern because it allowed me to simplify my project considerably. Previously I had an instance of antivirus that was 'owned' by each computer, but the visitor allowed me to pull out all that code and replace it with the acceptVisitor methods.

I used the singleton pattern in my project to ensure that only one instance my Antvirus visitor and Network class would be created because there was no need for multiple instances of them. The singleton is a very short pattern that creates an instance of the class if none exists and then returns that instance, or if it already exists, just returns it.

Wednesday, February 20, 2008

Four Patterns in Concert

For assignment 6, I decided to model a class of 4 students and a lecturing professor. The students observe the professor, who has 2 states: OnTopicState and OnTangentState.

When the professor is on topic, the students use the strategy pattern to set their listen() behavior to AttentiveStrategy and uses a System.out.println to print what the student is doing (taking notes, listening, or distracted).

When the professor is on a tangent, the students have their listen() behavior set to SpaceOutStrategy which can make the student print out that they are sleeping, doodling, browsing the internet, daydreaming, or paying attention and amused.

The students are decorated with Freshman, Sophomore, Junior, and Senior, which have info() methods that return a String with the students name and class.

A standard class period in my program is 50 minutes long and the professor will pick a random amount of time between 1 and 25 minutes to either be on topic or on a tangent before the state is switched. When the class timer reaches 0, the class is dismissed and the students leave.

I decided that my console output was pretty boring when I finished implementing the patterns, so I added String arrays so that the teacher would lecture on different topics and go on tangent about different things similar to how I made the students have several options for their listen() behaviors.

I had a lot of fun with this project and feel like I can now go back to my more complicated project with a better idea of how to compose patterns and not let all the details obscure the implementation. 

Friday, February 15, 2008

State Pattern Ideas

I haven't implemented my state pattern yet, but I'm planning on retooling the way that my Computer class stores whether or not it is infected. I'm going to add states for being infected, clean, or hosed by a virus. The other place I will add the state pattern will be my Virus classes. I would like my viruses to have states for Seeking computers to infect, infecting a computer, and hiding from antivirus.

State Pattern Recipe

You'll first need to determine the object(s) you'd like to apply the state pattern to and what states and transitions will be needed. Use a state diagram like the one in Head First Design Patterns if you want to visualize this properly.

1.) Create a State interface
2.) Write methods for the state transitions
3.) Create a class that implements State and add the state methods as specified in the interface
4.) in each state change method, you can use if/else statements to check the current state and offer different responses based on what state the object is currently in
5.) Create instance variables of type State for all your states and for the current state, which should be assigned to the state that the object should start in
6.) create a setter method that lets you set the state and a getter method so that you can get the current state

You now have a sort of rudimentary AI that can do different behaviors based on the current state.

Friday, February 8, 2008

Decorator Pattern Recipe

1.) Figure out which decorations/wrappers you'll need
2.) Create an abstract Decorator class that extends the superclass of the objects you wish to decorate
3.) Create wrapper classes that extend the decorator class. The constructor should take a reference to the object the class is to wrap so that you can use the methods in all layers of the decorated classes
4.) Define any methods from the abstract class
5.) To wrap an object up in a decorator, all you have to do it assign it to your decorator and pass it into the constructor.