
This is the Title of the Book, eMatter Edition
Copyright © 2007 O’Reilly & Associates, Inc. All rights reserved.
236
|
Chapter 8: Interfaces
As with the Point class, code that wishes to store a Rectangle instance simply invokes
serialize( ) on that instance. Through composition, the invocation is forwarded to the
Serializer instance stored by the Rectangle class. Here is an example of its use:
var r:Rectangle = new Rectangle(this, 1);
r.setSize(10, 20);
trace(r.serialize( )); // Displays: width=10|height=20
If a class would rather implement its own custom serialize( ) method instead of using
the generic one provided by Serializer, then the class should simply implement the
Serializable interface directly, providing the serialize( ) method definition and body
itself.
Separating the Serializable datatype’s interface from its implementation allows any
class to flexibly choose from among the following options when providing an imple-
mentation for the serialize( ) method:
• Extend Serializer
• Use Serializer via composition
• Provide its own serialize( ) method directly
If the class does not already extend another class, it can extend Serializer (this option
means the least work). If the class already extends another class, it can still use
Serializer via composition (this option is the most flexible). Finally, if the class needs
its own special serialization routine, it can implement Serializable directly ...