More Flexible Code – Programming to an Interface

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:
[as]
public var myPet:IAnimal = new Dog();
public var yourPet:IAnimal = new Fish();
public var hirPet:IAnimal = new Camel();
public var herPet:IAnimal = new Giraffe ();

[/as]

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:
[as]
package
{
public interface IAnimal
{
function create(p_name:String):void
function feed(p_food:Object):void
function sleep():void
}
}
[/as]
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!

Leave a Reply

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