AS3 :: Builder Pattern
Hello,
I have had some real fun with this pattern in AS3.
The build pattern I use has the elements;
- Client that wants a built product
- Client that controls when the product of the builder is created
- A builder that has one public method and one public property respectivly
- build()
- builtObject
The Builder implements a Template Method pattern in it's build() method. When the client calls the build() method of the Builder, the template is then run.
The builder implements IBuilder interface that holds
- bulitObject:IProjectObject
- build():void
Note :: The build() method could return the product, but I have found it is not neccessary becasue I want the build() method to stand on it's own and not be tied to a type in the interface. Thus, the build() method has void for a return type.
The buildObject implements the IProductObject interface that has one property.
- product:*
There are very few times I use the astrix but, this in my opinion is a good place for it. I always cast the product from the productObject anyway.
myBuilder.build();
var productObject:IProductObject = IProductObject(myBuilder.builtObject);
trace(productObject.product);
In the build() method you would have something like;
{
buildObject();
buildAndAddToObjectOne();
...
}
protected function buildObject():void
{
...
}
This way all the methods in the build method are protected and can be overriden in a sunbclass. This is a very powerful pattern for object models such as composite trees etc.
More maybe later
Peace, Mike