Archive for the 'Template Method Pattern' Category

Hollywood Principle: Don’t Call Us; We’ll Call You—ActionScript 3.0 Template Design Pattern goes Hollywood!

bucketrulehollyThe problem in understanding the Hollywood Principle is that it is too often glossed over as a type of inversion of control. In most respects, the Hollywood principle is really about the direction of calls. Where it gets a little confusing is when you assume the natural order of programming is from bottom to the top, with the bottom making calls to the higher levels. At one time, much programming was “bottom-up” in this sense, but that’s been quite a while. Therefore, when talking about inversion, you may be wondering, inversion from what? In fact, the late John Vlissides (one of the Gang of Four) starts off a discussion of the Hollywood Principle, noting,

The Template Method pattern leads to an inversion of control known as the “Hollywood Principle,….” (1996)

As we know, Dependency Inversion is better understood as Abstraction Dependency—both higher and lower level components come to depend on abstractions. If we think depend on abstractions we have no need to even consider inversion. Doing so only confuses the issue. So, if we examine the Hollywood Principle, let’s do so in terms of what it does in its own right—what is its focus?

The Hollywood Principle holds that higher level components should call lower level components, but lower level components should not call higher level components.

So, if we take that focus of the Hollywood Principle we can better discuss its unique features. Is it related to the other principles of OOP? Of course it is, but let’s just focus on the point it makes.
Continue reading ‘Hollywood Principle: Don’t Call Us; We’ll Call You—ActionScript 3.0 Template Design Pattern goes Hollywood!’

Flexibility Pays Off with Template Method

This is a testimonial of sorts for the capacity to easily change an application created with design patterns. Recently Adobe put up a version of a player that streams H.264 formats–these are files like Apple’s .mov and MP4, among others. After fumbling around for a while creating the files–one video .mov using iMovie and a M4a audio using Garage Band, I went to test them on a progressive download app I had handy that was set up to play audio using the Sound class and video with NetConnection(null) and NetStream. The only problem was that the streaming audio (m4a) file required a NetStream instance and would not work with the Sound class.

Because the classes for handling audio were set up around the Sound class, I thought this would be a major chore, but all I had to do was to make changes in one class. After the changes were made, it worked fine. The best part is that the structure of the Template Method did not interfere with any of the other elements in the application. So while the application may be fairly complex for the simple task of playing video and sound, when things change, as they always do, making the changes in the application were simple.

The application can be found at: http://www.sandlight.com/player9/. Keep in mind that no FLV files are used but that a genuine MOV file being played in Flash using progressive download and the sound is from a M4a file, the same file format as the ones used for iTunes–all sitting fat and happy and working. You’ll need to go to Adobe Labs and download the Flash 9 player (Beta) to see this at work.