| Home | Tutorials | Forums | Articles | Blogs | Movies | Library | Employment | Press | Buy templates |
|
|
#1 |
|
rather be programming
Join Date: Feb 2005
Location: City of Angels
Posts: 10,000
|
Does anyone actually "program to interfaces over implementations"? I have never seen anyone do: var clip:Object = new MovieClip();. The authors of Design Patterns for Actionscript 3 by O'Reilly to give an explanation, but it looks like the most ridiculous thing I have ever seen.
So what does this principle of GoF really mean? Below is a segment where they try to explain this: 1.8.2. Using Complex Interfaces As you saw in Example 1-36, the program typed the instances to the implementation and not the interfaces as was done in Example 1-20. This was caused by the key methods being part of classes that implemented an interface and extended a class. Because of the way in which the different display objects need to be employed, this dilemma will be a common one in using Flash and ActionScript 3.0. Fortunately, a solution is at hand. (The solution may be considered a workaround instead of the correct usage of the different structures in ActionScript 3.0. However, with it, you can create methods that require some DisplayObject structure and program to the interface instead of the implementation.) If the interface includes a method to include a DisplayObject type, it can be an integral part of the interface. Because Sprite is a subclass of the DisplayObject, its inclusion in the interface lets you type to the interface when the class you instantiate is a subclass of Sprite (or some other subclass of the DisplayObject, such as MovieClip.) To see how this works, the application made up of Example 1-39 and Example 1-40 creates a simple video player. The VidPlayer class builds the structural details of the video playing operations. To do so requires that it subclass the Sprite class. By placing a getter method with a DisplayObject type in the IVid interface, the application sets up a way that the client can program to the interface. ActionScript Code:
() function is implemented to the DisplayObject class in building the VidPlayer class. ActionScript Code:
treated like one. Any reference to displayObject is actually a method request. The trick is to add the instance to the display list, and at the same time call the method that establishes the instance as a DisplayObject. Example 1-41 does just that. ActionScript Code:
IVid. Next, showTime instantiates the VidPlayer class that has all the details for playing the video. By doing so, it inherits the Sprite class as well as the IVid interface. Then the showTime client plays the video using the playVid() method. Finally, when the showTime instance is added to the display list with the addChild statement, it is added as both the child of the VidPlayer class and the DisplayObject class by using the displayObject getter. Because the getter, displayObject, is included in the display list, you will not get the following error: 1067: Implicit coercion of a value of type IVid to an unrelated type flash. display: DisplayObject. IVid appears in the error because the instance was typed to the interface instead of the implementation. By slipping in the DisplayObject typed getter method, we avoid the error.
__________________
I'm old enough to know better and young enough to do it anyway. -- maskedman |
|
|
|
|
#2 |
|
Flash Sucks
|
Are you asking a question? Programming to interfaces is an important part of oop, is it that you don't understand the benefits? Or just don't agree with it? Or am I just way off :-)
-M |
|
|
|
|
|
|
|
#3 |
|
rather be programming
Join Date: Feb 2005
Location: City of Angels
Posts: 10,000
|
Yea, I don't understand the benefits.
Maybe I'm comparing it wrong. I see it as datatype a Sprite as Object. I am just missing the importance of it.
__________________
I'm old enough to know better and young enough to do it anyway. -- maskedman |
|
|
|
|
#4 |
|
Flash Sucks
|
There's not really any benefit to the example you are showing. An example where it's benefits can be seen is with iterators.
IIterator is an interface. It has absolutely no implementation details, therefore your code will naturally not rely on the implementation of the iteration it represents. When you are given an IIterator object you are sure of one thing, you can iterate over some collection of objects. It doesn't matter what collection, how that collection is structured, or anything else specific to that collection. This means that you can swap out how the collection is stored, and even the iterator itself, and your code will surely work (unless of course your code is broken.). That's a simple example, but the benefits are quite obvious. |
|
|
|
|
#5 |
|
rather be programming
Join Date: Feb 2005
Location: City of Angels
Posts: 10,000
|
Maybe the fact that the code example by O'Reilly is so poorly done isn't helping my understanding. Hopefully, if I continue reading throught the book, my understanding of it will clear up somewhat.
I suppose the whole thing is directly related to polymorphism?
__________________
I'm old enough to know better and young enough to do it anyway. -- maskedman |
|
|
|
|
#6 |
|
Flash Sucks
|
yuup they are very related :-D
|
|
|
|
|
#7 |
|
rather be programming
Join Date: Feb 2005
Location: City of Angels
Posts: 10,000
|
Any simple code (but realistic) that you can think of off the top of your head where this approach would be valuable?
p.s. Looking like I might be out your way here in the near future....
__________________
I'm old enough to know better and young enough to do it anyway. -- maskedman |
|
|
|
|
#8 |
|
rather be programming
Join Date: Feb 2005
Location: City of Angels
Posts: 10,000
|
after reading some OOP, programming to Interfaces (or the superclass) is essential. Polymorphism plays a big role in code reusablility.
It isn't so much that movieclips get types as objects, but that custom classes getting types to the super when they share the same interface.
__________________
I'm old enough to know better and young enough to do it anyway. -- maskedman |
|
|
|
|
#9 |
|
Joeri Sebrechts
Join Date: Apr 2005
Location: Antwerp, Belgium
Posts: 1,462
|
Interfaces are useful where ever you need two or more different lines of classes to behave the same way. For example, you could define an interface "Storable", with Serialize and Unserialize functions, that translate the object from and to string. Then, you could store any object inside a collection of Storable and serialize the whole collection to string without knowing which object you're dealing with and without having to encode unnecessary data, because each object would know how to serialize itself.
|
|
|
![]() |
| Thread Tools | |
| Display Modes | Rate This Thread |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| advice on writing effective interfaces, with eventListeners | kalvin82 | ActionScript 2.0 | 2 | 01-24-2007 07:00 AM |
| Voice Narration program for Flash Projects??? | lorenelks | Flash 8 General Questions | 0 | 05-08-2006 03:50 PM |
| view how a program is runing through a webcam | benucio | Flash Media Server | 0 | 04-14-2006 12:41 PM |
| OOP design: interfaces, why and when? | bitterclarence | Components | 7 | 05-11-2005 10:46 AM |
| Interfaces: Overwriting methods. | Covenent | ActionScript 2.0 | 0 | 05-20-2004 10:32 PM |