Created
April 8, 2010 04:46
-
-
Save troygilbert/359780 to your computer and use it in GitHub Desktop.
Invalidate Pattern example
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package | |
{ | |
import flash.display.Shape; | |
import flash.events.Event; | |
public class InvalidationPattern | |
{ | |
protected const USE_DEFERRED_VALIDATION:Boolean = true; | |
protected var tickerShape:Shape; | |
protected var needsValidation:Boolean; | |
protected var _property:int; | |
/** Constructor. **/ | |
public function InvalidationPattern() | |
{ | |
// shapes always get enter frame events, no stage required | |
tickerShape = new Shape(); | |
tickerShape.addEventListener(Event.ENTER_FRAME, onEnterFrame); | |
} | |
/** An example property. **/ | |
public function get property():int { return _value; } | |
public function set property(value:int):void | |
{ | |
if (value != _property) | |
{ | |
_property = value; | |
invalidate(); | |
} | |
} | |
/** Invalidates object. **/ | |
public function invalidate():void | |
{ | |
needsValidation = true; | |
if (USE_DEFERRED_VALIDATION) return; // don't do anything yet | |
validate(); | |
} | |
/** Validates an object. **/ | |
public function validate():void | |
{ | |
// if any of your dependent properties are not set, | |
// just exit this method now and it'll be called later | |
// if they're all ready, process the properties and | |
// update the state of the object | |
needsValidation = false; | |
} | |
/** Enter frame handler. **/ | |
protected function onEnterFrame(event:Event):void | |
{ | |
if (needsValidation) validate(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment