100 likes | 234 Views
Factory Method Design Pattern: ActionScript 3.0 Implementation. Bill Sanders Sandlight Productions Bloomfield, Connecticut. Links and Downloads. You will find all materials in .zip folders that you might want to download and open in Flash CS5 or Flex (Flash Builder) to follow along:
E N D
Factory Method Design Pattern: ActionScript 3.0 Implementation Bill Sanders Sandlight Productions Bloomfield, Connecticut
Links and Downloads • You will find all materials in .zip folders that you might want to download and open in Flash CS5 or Flex (Flash Builder) to follow along: • Download files from here: • http://nemo.mwd.hartford.edu/~wsanders/Brasil/ • You can also test the applications on the play links • Play links: • http://www.sandlight.com/Brasil/ • http://www.sandlight.com/NewEngland/ • All ActionScript 3.0 Design Patterns • http://www.as3dp.com • http://www.sandlight.com
The Factory Method • When building a large project, you may not know all of the objects you will use in the future or how objects may change • The Factory Method was designed so that the Client class makes requests from the Factory (Creator), not knowing the details about the product being created. Client Factory Product
Request from Client to Factory (Program to interface; not implementation) private var parana:Factory; -------------- private function doParana(e:MouseEvent) { parana=new MakeParana(); addChild(parana.factoryMethod()); } Factory interface Concrete Factory instance
Abstract Factory (Creator) package { import flash.display.Sprite; import flash.errors.IllegalOperationError; // ABSTRACT Class) public class Factory { protected var product:Product; // ABSTRACT Method (must be overridden in a subclass) public function factoryMethod():Sprite { throw new IllegalOperationError(“must be overridden"); return null; } } }
Concrete Factory with Lazy Instantiation package { import flash.display.Sprite; public class MakeParanaextends Factory { public override function factoryMethod():Sprite { if(! product) { product=new ParanaProd(); } return product.getObject(); } } } Lazy instantiation
Abstract Product package { import flash.display.Sprite; import flash.errors.IllegalOperationError; // ABSTRACT Class (should be subclassed and not instantiated) class Product { protected var objNow:Sprite; public function getObject():Sprite { throw new IllegalOperationError(“must be overridden "); return null; } } }
Concrete Product package { import flash.display.Sprite; public class ParanaProd extends Product { public override function getObject():Sprite { objNow=new Parana(); objNow.x=273.55, objNow.y=276.50; return objNow; } } } Image in Sprite class