User Guide
156 Object-Oriented Programming in ActionScript
{
switch (shapeName)
{
case "Triangle":
return new EquilateralTriangle(len);
case "Square":
return new Square(len);
case "Circle":
return new Circle(len);
}
return null;
}
public static function describeShape(shapeType:String,
shapeSize:Number):String
{
GeometricShapeFactory.currentShape =
GeometricShapeFactory.createShape(shapeType, shapeSize);
return GeometricShapeFactory.currentShape.describe();
}
}
}
The createShape() factory method lets the shape subclass constructors define the details of
the instances that they create, while returning the new objects as IGeometricShape instances
so that they can be handled by the application in a more general way.
The
describeShape() method in the preceding example shows how an application can use
the factory method to get a generic reference to a more specific object. The application can get
the description for a newly created Circle object like this:
GeometricShapeFactory.describeShape(“Circle”, 100);
The describeShape() method then calls the createShape() factory method with the same
parameters, storing the new Circle object in a static variable named
currentShape, which
was typed as an IGeometricShape object. Next, the
describe() method is called on the
currentShape object, and that call is automatically resolved to execute the
Circle.describe() method, returning a detailed description of the circle.
Enhancing the example application
The real power of interfaces and inheritance becomes apparent when you enhance or change
your application.