Adobe FLEX 2-PROGRAMMING ACTIONSCRIPT 3.0 Manual page 155

Programming actionscript 3.0
Table of Contents

Advertisement

The
method also uses the
describe()
the RegularPolygon superclass' version of the
EquilateralTriangle.describe()
about the type of shape. Then it gets the results of the
by calling
super.describe()
The Square class won't be described in detail here, but it is similar to the EquilateralTriangle
class, providing a constructor and its own implementations of the
methods.
describe()
Polymorphism and the factory method
A set of classes that make good use of interfaces and inheritance can be used in many
interesting ways. For example, all of the shape classes described so far either implement the
IGeometricShape interface or extend a superclass that does. So if you define a variable to be an
instance of IGeometricShape, you don't have to know whether it is actually an instance of the
Circle or the Square class in order to call its
The following code shows how this works:
var myShape:IGeometricShape = new Circle(100);
trace(myShape.describe());
When
myShape.describe()
even though the variable is defined as an instance of the IGeometricShape interface, Circle is
its underlying class.
This example shows the principle of polymorphism in action: the exact same method call
results in different code being executed, depending on the class of the object whose method is
being invoked.
The GeometricShapes application applies this kind of interface-based polymorphism using a
simplified version of a design pattern known as the factory method. The term factory method
refers to a function that returns an object whose underlying data type or contents can differ
depending on the context.
The GeometricShapeFactory class shown here defines a factory method named
:
createShape()
package com.example.programmingas3.geometricshapes
{
public class GeometricShapeFactory
{
public static var currentShape:IGeometricShape;
public static function createShape(shapeName:String,
statement, but in a different way—to invoke
super()
describe()
method first sets the
, and it appends that result to the
describe()
is called, it executes the method
len:Number):IGeometricShape
method. The
string variable to a statement
desc
RegularPolygon.describe()
string.
desc
getArea()
method.
Circle.describe()
Example: GeometricShapes
method
and
because
155

Advertisement

Table of Contents
loading

This manual is also suitable for:

Flex

Table of Contents