ActionScript 3.0 Design Patterns

(Chris Devlin) #1

286 | Chapter 8: Observer Pattern


of an abstract class or an interface instead of a class. What does that really mean,


though?


The easiest way to understand this concept is to think of the methods in either an


abstract class or interface asinterfaces. An interface is a set of functions an object


implements. Eric and Elisabeth Freeman (Head First Design Patterns) suggest that we


think of the properties in either an abstract class or interface assupertypes. It turns


out that the actual practice of programming to an interface has many different


approaches, but to see the basic concept, consider the following set of scripts,


Example 8-1 through Example 8-6. Example 8-1 through Example 8-4 set up an


interface, and Example 8-5 and Example 8-6 implement it. Be sure to save the files all


in the same folder.


Example 8-1. SpaceWarrior.as


//SpaceWarrior.as
package
{
interface SpaceWarrior
{
function useWeapon( ):void;
}
}


Example 8-2. Alien.as


//Alien.as
package
{
public class Alien implements SpaceWarrior
{
function Alien( )
{
//Constructor
}
public function useWeapon( ):void
{
trace("Zaaaapp!!!");
}
}
}


Example 8-3. Earthling.as


//Earthling.as
package
{
public class Earthling implements SpaceWarrior
{
function Earthling( )
{
//Constructor

Free download pdf