10.12.2012 Views

ActionScript 3.0 Design Patterns.pdf - VideoTutorials-bg.com

ActionScript 3.0 Design Patterns.pdf - VideoTutorials-bg.com

ActionScript 3.0 Design Patterns.pdf - VideoTutorials-bg.com

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Using Interfaces and Abstract Classes in <strong>ActionScript</strong> <strong>3.0</strong><br />

Inheritance can also be linked to two other structures; interfaces and abstract classes.<br />

However, the connection between the interface structure (a statement in Action-<br />

Script <strong>3.0</strong>) or the abstract class and inheritance is a bit different from the one with<br />

the class structure we’ve examined thus far.<br />

Interface constructs<br />

First of all, in this context, interface refers to an <strong>ActionScript</strong> <strong>3.0</strong> statement and not<br />

the object interface discussed in the “Encapsulation and <strong>Design</strong> <strong>Patterns</strong>” section.<br />

While a class can be said to be an abstraction of an object, an interface is an<br />

abstraction of methods. They are widely used in design patterns. Beginning in<br />

Chapter 5 with the adapter pattern, you will see interfaces at work in several of the<br />

other design patterns.<br />

To begin to see what an interface does, a simple example illustrates the use of one.<br />

However, once you start seeing how they’re used in design patterns, you will better<br />

see their utility. Example 1-17 shows how a typical interface is created. The application<br />

is made up of Example 1-17 to Example 1-20.<br />

The first thing we’ll do is to make our interface. As you can see in Example 1-17, the<br />

single function is quite simple and devoid of content. It does have a name, parameter,<br />

and return type, but note that the function is only a single line.<br />

Example 1-17. BandFace.as<br />

package<br />

{<br />

//Interface<br />

public interface BandFace<br />

{<br />

function playInstrument(strum:String):void;<br />

}<br />

}<br />

Each implementation of the interface must have exactly the same structure in all of<br />

the methods in the interface, and if anything is not the same, you’ll get an error message.<br />

As long as the signature for the methods is the same, everything should work<br />

fine. Example 1-18 is the first implementation of the of the BandFace interface.<br />

Example 1-18. Guitar.as<br />

package<br />

{<br />

public class Guitar implements BandFace<br />

{<br />

public function Guitar( ) {}<br />

28 | Chapter 1: Object-Oriented Programming, <strong>Design</strong> <strong>Patterns</strong>, and <strong>ActionScript</strong> <strong>3.0</strong>

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!