72 | Chapter 2: Factory Method Pattern
Note that thedoStuff( )method in theCreatorclass is declared aspublicbecause we
need to allow clients to get to it from outside the package. In contrast, the
factoryMethod( )is declared asprotected. Theprotectedattribute makes the method
visible only within the same class or derived classes. Finally, we can take a look at the
document class calledMain, which is the client described in the class diagram shown
in Figure 2-2 that uses the creator to access products.
Clients
The document classMaindoes not belong to a named package. Therefore, it must
import the packages that contain the classes it uses. Theexamplepackage needs to be
imported, as it contains the creator classes. The document classMainis the client
described in the high-level model of the factory method pattern shown in Figure 2-1.
In Example 2-7, the document class calls a static method calledrun( )in the static
Test class to run some tests.
trace("Creating product 1");
return new Product1( ); // returns concrete product
}
}
}
Example 2-6. CreatorB.as
package example
{
public class CreatorB extends Creator
{
override protected function factoryMethod( ):IProduct
{
trace("Creating product 2");
return new Product2( ); // returns concrete product
}
}
}
Example 2-7. Main.as
package
{
import flash.display.Sprite;
import example.*;
/**
- Main Class
- @ purpose: Document class for movie
*/
public class Main extends Sprite
{
Example 2-5. CreatorA.as (continued)