196 | Chapter 5: Adapter Pattern
The Adapter Class
Before we implement theIListDisplayinterface, a decision regarding which type of
adapter, class or object, needs to be made. The existing interface of theTextField
class is quite extensive and contains several methods that would be directly useful in
the new context. For example, theTextFieldinterface defines many properties such
asbackgroundColorandborderthat enable formatting the text field display object. In
addition, it also defines methods such assetTextFormat( )that allows formatting the
text in the field using aTextFormatobject. If theListDisplayFieldadapter is imple-
mented as a class adapter, we can benefit from less code and implementation effi-
ciency. However, if we implement ListDisplayField as a class adapter in
ActionScript 3.0 using inheritance, all the public methods and properties in
TextFieldwill be exposed. We will need to carefully override some of the methods
and properties in theTextFieldclass that allow direct manipulation of text in the
field. The adapter implementation is shown in Example 5-14.
Example 5-14. ListDisplayField.as
1 package
2 {
3 import flash.text.*;
4
5 // Adapter
6 public class ListDisplayField extends TextField implements IListDisplay
7 {
8
9 private var aList:Array;
10
11 public function ListDisplayField( )
12 {
13 super( ); // Call the TextField constructor
14 this.clear( );
15 }
16
17 public function addItem(s:String):void
18 {
19 this.aList.push(s);
20 this.update( );
21 }
22
23 public function deleteItemAt(i:uint):Boolean
24 {
25 if ((i > 0) && (i <= aList.length)) {
26 aList.splice(i-1, 1);
27 this.update( );
28 return true;
29 } else {
30 return false;
31 }
32 }
33