Download presentation
Presentation is loading. Please wait.
1
BILL SANDERS SANDLIGHT PRODUCTIONS BLOOMFIELD, CONNECTICUT Factory Method Design Pattern: ActionScript 3.0 Implementation
2
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/ 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/Brasil/ http://www.sandlight.com/NewEngland/ http://www.sandlight.com/NewEngland/ All ActionScript 3.0 Design Patterns http://www.as3dp.com http://www.as3dp.com http://www.sandlight.com
3
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
4
Factory Method Class Diagram
5
Factory Method File Diagram
6
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()); } Concrete Factory instance Factory interface
7
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; }
8
Concrete Factory with Lazy Instantiation package { import flash.display.Sprite; public class MakeParana extends Factory { public override function factoryMethod():Sprite { if(! product) { product=new ParanaProd(); } return product.getObject(); } Lazy instantiation
9
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; }
10
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
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.