ActionScript 3.0 Design Patterns

(Chris Devlin) #1
Inheritance | 33

Why use interfaces and abstract classes?


To understand why to use interfaces and abstract classes, we need to consider the


whole purpose of design patterns. It’s theability to reuse object-oriented software.


We’ve been using fairly simple examples to help clarify the concepts. However, typi-


cal software is usually far more complex, and the algorithms more sophisticated. Once


you complete a project, you’re likely to have to make a change. The larger and more


comple xthe project, the more difficult it is to reuse the assets you’ve developed, main-


tain the interconnections and generally make any kind of change without unraveling


the whole thing or introducing code that may make future change impossible.


To illustrate what this means, consider the application in Examples 1-21 to 1-23 in the


previous section. Multiply the complexity of the classAbstractClassby a factor of 10.


Do the same to the number of subclasses. Now you’re dealing with some serious com-


plexity. Next, consider that you need to maintain the functionality of the class,


Subclass, and yet change the abstract method in theAbstractClassfor a new function-


ality. Also, you have to maintain all of the interfaces and connections you’ve built.


Because you used an abstract class, you can create a new subclass that overrides the


abstract function and uses it in a different way. To see how this all works, we’ll make


a new subclass of theAbstractClassand change the abstract method. We’re not


changing anything else in the entire application, so we don’t have to worry about


everything working together because we can separate our new subclass and method


and only implement them where needed. Other subclasses of theAbstractClassare


unaffected. Examples 1-24 and 1-25 show the two new classes created to make the


change.


Note that instead of having a singletracestatement, Example 1-24 uses three. This


modification simulates a more comple xchange. However, E xample 1-25, which


implements the application, is identical to Example 1-23.


Example 1-24. SubclassChange.as


package
{
//A New Subclass of Abstract class with a change
public class SubclassChange extends AbstractClass
{
override function abstractMethod( ):void
{
trace("This is the new abstractMethod!!")
trace("Made just one little important change.");
trace("But this still works just fine!");
}
}
}

Free download pdf