Monthly Archive for November, 2009

PHP Chain of Responsibility Design Pattern from ActionScript 3.0

smallchainARE Design Patterns portable from one language to another? Of course they are. I’ve read some accounts that claim otherwise, but I’ve not found any design pattern that could not be used in any computer language that I’ve tried. (Maybe I’m just conversant in languages where design patterns do work, but I don’t think so.) Anyway, Chandima and I have been discussing doing a set of PHP design patterns, and I wanted one for a contact form I had done using PHP.

For this blog, though, I thought I’d use a translation from one of the design patterns we have on this blog so that you can more easily see the similarities and differences between ActionScript 3.0 and PHP in implementing the same design pattern.

Chain of Responsibility

As you may recall from our Chain Of Responsibility (CoR) post, the design pattern is used when you want to have a system that takes care of requests when you have different outcomes. In our original example, we added a helper class called Request, but otherwise we stuck with the basics of the CoR structure. Figure 1 shows the class diagram of the CoR we used in our initial post.

<em><strong>Figure 1: </strong> Chain of Responsibility Class Diagram</em>

Figure 1: Chain of Responsibility Class Diagram

In this particular design pattern the Client class is a full-fledged participant in the pattern. Because Client classes are request objects themselves, it may seem odd to have a Request class. Just think of the Request class as a helper class that encapsulates requests for the Client.
Continue reading ‘PHP Chain of Responsibility Design Pattern from ActionScript 3.0′

  • Share/Bookmark

ActionScript 3.0 Design Pattern Starter Kit

Getting Started is Design Patterns Can be Daunting

Getting Started in Design Patterns Can be Daunting

Over the years, Chandima and I have created several tools for helping ActionScript 3.0 developers create and use Design Patterns. For those who have recently decided to take the leap into coding patterns, this post provides one-stop shopping for getting started. For the old-timers, it represents a place where you can find key elements and refresh your understanding of design pattern basics.

What are Design Patterns?

So as not to create false expectations, Design Patterns will not make your programs run faster or better. They were developed to aid in creating object oriented programs that can be reused and easily changed and updated. One of the side benefits is that they aid in understanding Object Oriented Programming (OOP); so, if you leap into design patterns, you’re leaping into OOP.

They’re not easy, but don’t let that scare you. Some are easier than others, and by starting with the easy ones, the more challenging ones will come in time.

They payoffs are huge. If you’re reading this line, it means you’re not afraid of expanding your mind beyond what you thought you could do in programming. Once you’ve figured out ‘Hello World’ level programming and discovered the beauty of a good algorithm, it’s time to look at the bigger picture. So let’s get started!
Continue reading ‘ActionScript 3.0 Design Pattern Starter Kit’

  • Share/Bookmark

AS3 Design Patterns now on iPhone!

ActionScript 3.0 Design Patterns for the iPhone

ActionScript 3.0 Design Patterns for the iPhone

We were delighted to hear that our book was selected by O’Reilly to be among the first to be made available for the iPhone! If you click on the App Store icon (located just below the picture of the book’s cover), it’ll take you right to the store. Check out the tweet on our MicroBlog to see iPhone application with our book. Or just open iTunes and search for “O’Reilly Design Patterns” and there it is. Even better, it’s only $4.99, and you can have it handy wherever you and your iPhone or touch-pad iPod go. (Now if Bill can only figure out a way to get an iPhone…)

  • Share/Bookmark

Truckin’ Through ActionScript 3.0 MVC: Part V—Purpose and Use

Truckin' thru MVC

Truckin' thru MVC

The MVC remains one of the most valuable structures for understanding and using Design Patterns, and yet I believe it to be misunderstood and misused on just about every level imaginable. In the first chapter of their book, the Gang of Four spend a mere two pages describing MVC in order to help readers understand the concept of a “pattern.” Unfortunately, the language used may have been the culprit leading to widespread uses for which the MVC was never intended. In introducing the MVC, GoF note,
Looking at the design patterns inside MVC should help you see what we mean by the term “pattern.”

The phrase, the design patterns inside MVC is where the trolly may have jumped the tracks.

Continue reading ‘Truckin’ Through ActionScript 3.0 MVC: Part V—Purpose and Use’

  • Share/Bookmark

Truckin’ Through ActionScript 3.0 MVC: Part IV—Making Changes

Truckin' thru MVC

Truckin' thru MVC

I mentioned that updating and changing programs is the sine qua non of Design Patterns. Further, I promised that it wouldn’t take very long to get to Part IV of this series because all I was going to do was to update the original compass program. So that’s what I did, and here is Part IV of Truckin’ Through ActionScript 3.0 MVC.

Adding an Interface for the View

Before adding another view, I wanted to add an interface so that I could use the same Model and Controller classes. The changes are minimal in the revised View classes, and I changed the Client so that it could more easily request any view the user would like. Since you have all of the details about how the MVC works in Parts I-III, I’ll get right into the interface.

In looking at the View class in Part III, you can see that the class has only two methods. By putting them into an interface, we can program to the interface instead of the implementation. That means that as long as the same interface is used, you can use the same Model and Controller without any changes at all. As usual, interfaces are fairly short, and this one is no exception.

?View Code ACTIONSCRIPT
1
2
3
4
5
6
7
8
9
10
11
package 
{
	import flash.display.Sprite;
	import flash.events.Event;
 
	public interface IView
	{
		function createView(vessel:Sprite):Sprite;
		function update(e:Event):void;
	}
}

Before continuing, click the Play button to test both compasses that have their roots in that interface. (compi?)
play
The Model and Controller classes are identical for both compass examples. Read on to see how easy it is to add views.

Continue reading ‘Truckin’ Through ActionScript 3.0 MVC: Part IV—Making Changes’

  • Share/Bookmark