ActionScript 3.0 Saturated Bridge Design Pattern 1: Intention

Independent Variation by Decoupling Abstractions and Implementations
The concept that you can decouple an abstraction from its implementation sounds flat out bizarre. Whether talking about an interface or an abstract class, how in the world is it possible to implement a concrete class that is not affected by changes in the interface? If the abstraction varies, the concrete implementation is either going to change or crash. If you use an interface, you’ll have some kind of abstract method set up that is implemented by a class using the interface and its signature. If I change the interface even minutely, something is going to change in the implementation or it will crash. Suppose I change the return type in one of the methods from a Number to a String, what’s going to happen when I run the program? It will throw an error.
For example, try the following:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 | //Client package { import flash.display.Sprite; public class Client extends Sprite { private var doFirst:IFace; public function Client() { doFirst=new Imp1(); trace(doFirst.doThing()); } } } //Interface package { public interface IFace { function doThing():Number; } } //First (correct) Implementation package { public class Imp1 implements IFace { public function doThing():Number { return 55; } } } |
That works just as expected. No big deal, the interface implementation is correctly formed. Now, make the following variation and try it:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 | //Modified Client to call new implementation package { import flash.display.Sprite; public class Client extends Sprite { private var doFirst:IFace; public function Client() { doFirst=new Imp2(); trace(doFirst.doThing()); } } } //Modified implementation package { public class Imp2 implements IFace { public function doThing():String { return "Hello World!"; } } } |
The only thing that was changed was the return data type from Number to String. Here’s the Crash results:
Line 3 1144: Interface method doThing in namespace IFace is implemented with an incompatible signature in class Imp2.
So how can you have independent variation between an abstraction and its implementation?
The Bridge to the Moon
When thinking about an image for this series on the Bridge design pattern, I considered a number of bridge images. However, the Bridge intention is to,
Decouple an abstraction from its implementation so that the two can vary independently.
The idea of a bridge to the moon popped up because the earth and the moon have different orbits (and a few other things) but share much in common and exert an influence on one another. The idea of a bridge to the moon is one way to think about decoupling and connections at the same time. An even better way is to take a look at the Bridge class diagram in Figure 1:

Figure 1: Bridge Class Diagram
Now, when we think about decoupling an abstraction from its implementation we can see that the Gang of Four made both the abstraction and implementation abstract! The implementation in the sense that we use extend from an abstract class or implements from an interface has a whole new meaning.
When an aggregate relationship exists between two classes (or interfaces) they create something akin to a concurrent object with different implementations possible. In this case the aggregate relationship is between two interfaces (or abstract classes), and you can begin to understand independent variation. So we need to look more closely at what exactly varies.
Read more…
Gentle Readers: Chandima and I try different things, and this is the first time (I think) that we’ve taken an online Adobe Connect presentation and the accompanying PowerPoint slides and translated them into an article (post.) If you were able to attend the online talk, you have all of the slides along with written commentary and you can take your time to review them. Of course if you are unable to attend because it’s 3:30 am in Mumbai, this provides an asynchronous means of participating. Let us know what you think. Is it effective? Ineffective? Makes no difference? Your comments are valuable and invited. 







Abstract What?





Bill Sanders
Recent Comments