More Flexible Code - Programming to an Interface

Wednesday, August 22nd, 2007

Keep your code flexible - program to interfaces. What does this mean? Well there are a few things that we need to discuss before we can really answer that question. For this discussion you'll need a decent understanding of Object Oriented programming…but, hey if you don't have that it still may make some sense anyhow and you might even get something that you don't understand explained for you as we go along.

First things first, Polymorphism - The ability for one class to stand in for another, kind of. By "kind of" I mean that by programming to an interface, a variable or property's value can be assigned to an object that conforms to a "formal interface", more on that in a second. This means that if you crate a property or variable that is typed to an interface - myPet:IAnimal; - (where IAnimal is the interface) that item can be any number of classes that follow the "interface" defined by IAnimal .

Example:

Actionscript:
  1. public var myPet:IAnimal = new Dog();
  2. public var yourPet:IAnimal = new Fish();
  3. public var hirPet:IAnimal = new Camel();
  4. public var herPet:IAnimal = new Giraffe ();

Each of the classes, Dog, Fish, Camel, Giraffe must "implement" the IAnimal interface.

Okay, so what is a formal interface you say?
First off, a formal interface is a definition, some might say a contract, but I think that is confusing, of what a class's publically accessible methods should be and what they should look like. ActionScript 3 provides a construct to create these interfaces that is very similar to classes. Basically you create the public methods, their parameters and their return types, but nothing that has to do with what happens inside the method.

Example:

Actionscript:
  1. package
  2. {
  3. public interface IAnimal
  4. {
  5. function create(p_name:String):void
  6. function feed(p_food:Object):void
  7. function sleep():void
  8. }
  9. }

Notice that there is nothing that tells you what you need to do in the methods, or anything about properties or private methods. That is determined by the needs of the class that implements the interface. This is a good point to take hold of and knock into your head:

A class has an implementation (what the class does ) and an interface (how the outside world interacts with it).

This is what allows your code to be flexible. If you program to interfaces, you can crate objects that are much more flexible and powerful by have the ability to "stand in" for any property or variable.

You can also create an "interface" through inheritance by using something called an "abstract" class. An abstract class isn't intended for instantiation, so it is up to you to manage that. But abstract classes do allow for a default implementation (what the class does) as well as allowing for the type of interface implementation mentioned above because sub-classes inherit the interface of their parent classes.

So, keep your code flexible, program to interfaces!

Advanced ActionScript 3 with Design Patterns

Thursday, May 10th, 2007
5/5

I was very excited to receive the book Advanced ActionScript 3 with Design Patterns I had heard great things about the book and was not disappointed as I read through it.

I just happen to be stuck in jury duty for a full day and as I waited for the city and county of Denver to decide if they required my services I was able to read through most in the book in a morning. The really nice thing about this book is the lack of fluff. It is very direct, very to-the-point and very useful.
There were quite a few lessons that I learned (one would be typing a variable to an Interface...tre useful!) through out the book. Not to mention to the descriptions of many of the more popular design patterns around. I'd say Joey Lott and Danny Patterson have done a fine job presenting ActionScript 3 as applied to development with a focus on design patterns.
The only complaint that I have heard, is that that it can become a little repetitive. If you are very familiar with ActionScript syntax, you may want to skim many of the chapters for items that catch your eye and look to be good tips or new to you.
Rated 5/5 on May 10 2007
Vote on John’s reviews at LouderVoice
LouderVoice review tags:
Find entries :

Want to subscribe?

  Subscribe in a reader or,
Subscribe via email:

About me

I'm a senior developer at RealEyes Media, Adobe Certified Instructor and Adobe Certified Professional. Here you'll get my ideas and experience Flex, Flash, ColdFusion and related technologies as well as some generally off the wall stuff.