Home > Design Pattern Tours > The ActionScript 3.0 Design Pattern Thrill Ride: Part I

The ActionScript 3.0 Design Pattern Thrill Ride: Part I

ThrillMy initial idea of having a “thrill ride” through a design pattern to help reveal its operation is turning out to be awkward in more ways than I envisioned. The initial tour is going to be through a State Design Pattern; so naturally I set it up using a State pattern. It wasn’t too long before I realized that it would be difficult to visit all of the participants without adding new states that would show visiting the Context and State interface. In other words, I needed VisitContext and VisitState state classes.

After some fumbling, I saw that much of the important work is done by the Client. It actually works like a composer, especially since the State design is a beautiful example of both composition and delegation. Each participant had some responsibility in the process that I wanted to reveal in the sequence in which it occurs. With the State design, this is especially challenging because the context changes with each state. So how does one go about making a tour of such a process?

What Varies?

Then it occurred to me that were I to spend time on setting up a tour of the State design pattern, why not create a reusable application that I could use on any pattern I wanted? Of course this brought me back to the most basic question of design pattern development—what varies? In other words what varies in a tour of a pattern? So I whipped out my Magic Table to look at the possible variations in a design pattern. Well, I knew that the patterns were going to vary, but what does that look like in the Magic Table? Here were some that stood out:

I’ll get to the decisions involved in deciding on the best design pattern for making my thrill ride through different design patterns in a bit, but first, I wanted to consider the initial animations, graphics and design. The embedded SWF shows what I have in mind. The “control panel” is made up of buttons representing the different states (The animation just goes from the Client to the Context but it gives an idea of the animation I’m considering. It only goes to one state and then this page has to be reloaded to start over.)

[swfobj src="http://www.as3dp.com/wp-content/uploads/2010/02/ClientTest4.swf" align="none" allowfullscreen="true"]

As you can see, the “view” from the “control panel” is the next participant in the design pattern. Also, an acquaintance arrow appears from the current view to the Context state in the “distance.” When you click one of the buttons on the panel, the view moves into the Context state and the State interface appears with an aggregation arrow from the current view.

Culling The Sequence

The idea is to move “into” the next participant (class or interface) and show what happens when the moves occur. So far all I have is a partial jump from the Client into the Context class, but I want to show what happens when that step is taken. Figure 1 shows a sequence each request goes through and I’m trying to work out a way to animate the whole thing using a design pattern.

<em><strong>Figure 1: </strong> Sequence of request in State design</em>

Figure 1: Sequence of request in State design

You can see a 5-step sequence with each request in a State design pattern. In the particular one we’re using in the example, we also have some helper classes in the form of a helicopter from the Library and a Mover object that moves the helicopter. The helper classes aid the in overall decoupling in the pattern.

To make an interesting trip through design patterns I’m forced to think in terms of a sequence to illustrate what is not sequential programming, but rather the process of composition and use and re-use of the objects making up design patterns. However, roller coasters and other theme park rides roll along sequentially and so I’m trying to step through the code dynamically as it is used. Once I get the basic implementation set up I can re-use it to trace through other design patterns.

Back to the Variation

Okay, we’ve got to ask once again What’s the fluctuation? (WTF?) After our examination of the steps in a request through a State design, we see a set of requests handled by the participants. That variation (or fluctuation) in a sequence looks a lot like a trip through a Chain of Responsibility pattern. The CoR sequence can be seen in Figure 2:

<em><strong>Figure 2: </strong> CoR Pattern can handle a sequence</em>

Figure 2: CoR sequence

Why not use it? As each step in the sequence is reached, the CoR makes a call to the appropriate object that shows what happens in the pattern by the appropriate participant. By treating each step as a request, we can handle virtually any sequence. That should serve us well when working up a way to take a trip through all of the different patterns.

Your Thoughts?

You can see where I am right now and where I have to go to get the ride moving. So I’d appreciate any ideas or help (especially with the artwork) that you may have. The look and feel is important for this to work well. Here’s where you can help:

  • Suggested design patterns that would work better than CoR
  • Ideas about how this should work—the animation, look and feel
  • What should be included in each step of the sequence
  • Artwork in the form of something I can use for a “dashboard” and scenes

Also, I’d like your thoughts on how helpful this will be. I know that sometimes I can trip over my own feet by being too clever. My favorite design pattern book is the Freemans’ Head First Design Patterns, but sometimes, their explanations got lost in the cleverness of the Head First framework. So, if you think this is a fool’s errand, let me know.

Share

No related posts.

Categories: Design Pattern Tours
  1. Curro
    March 2, 2010 at 3:40 pm | #1

    Hi Bill,
    I don’t quite see what you trying to do. If, as you say, you want to show how a request is handled by participants, then you only need a button:Next. Looking at what varies, it seems that what varies is when and how a request is fulfilled which takes us to the Command pattern. Also, as there will be many steps in common you should consider the Template Method pattern. So I think a combination of the Command and the Template will be the thing. But as I said at the beginning, I don’t really know what you want to do.

    Regards.

    Curro

  2. March 3, 2010 at 4:01 am | #2

    Hi Curro,

    You’re right. A simple set of “next” frames would do the trick, but I’ve go to select a design pattern given the topic of this blog. The idea of an animated “ride” might also be envisioned as a “tour” or even an “adventure game” where each step opens a new door or scene of the pattern while it is executed.

    What you say about the Command and Template Method patterns is true, but they may not be as easy or as flexible as the Chain of Responsibility pattern for going through a sequence of steps. Each participant in all design patterns has a unique responsibility, and the idea of a “tour” through the pattern showing what happens at each step may help reveal what’s going on. Also, it seemed like fun.

    However, as I said, I could be wrong. If you want to create an example using the Command and Template Method patterns, I’d really like to see it.

    Kindest regards,
    Bill

  3. Curro
    March 5, 2010 at 3:09 am | #3

    Hi Bill,
    it’s you who is right!
    As I started to consider how to create an example I realized that the Command pattern won’t help much, except for triggering other patterns.

    Sorry for my thoughtless comment. I have developed an enthusiasm for the Command and the Template patterns combination after translating chapters 18-19 from Agile Software Development (Robert c. Martin) into AS3.

    I am not acquainted with the CoR pattern yet but I guess it’ll do the job.

    Regards and thanks for your patience.

    Curro

  4. March 5, 2010 at 3:28 am | #4

    Hi Curro,

    Remember, I had to fumble around with the State pattern before settling on the Chain of Responsibility pattern. So, considering the Command and Template Method patterns makes perfect sense to me. Besides, I usually think of the CoR as a way to filter through a list of requests such as a Help Desk or some other user query. However, it seems to fit in this sequence as well and would be applicable to just about any sequence that the other design patterns might have.

    Kindest regards,
    Bill

  1. February 28, 2010 at 5:18 pm | #1

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>