DZone Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

Snippets has posted 5883 posts at DZone. View Full User Profile

ActionScript - Lazy Loading

06.01.2009
| 4431 views |
  • submit to reddit
        // when a class refers to another instance retrieved from a database (not contains it), that other instance 
// often has a unique ID. If so, you can use Lazy Loading to defer retrieving the other instance as 
// long as possible. If the referred instance is never used or only its ID is used, you can avoid
// loading it altogether.

private var _otherObjectId : uint = 0;
private var _otherObject : OtherObject = null;
public function get otherObjectId () : uint
{
    if (_otherObject != null)
        return _otherObject.id;
    else
        return _otherObjectId;
}

public function set otherObjectId(value : uint) : void
{
// use the accessor, not the variable
    if (this.otherObjectId != value)
    {
        _otherObjectId = value;
        _otherObject = null;
    }
}

public function get otherObject() : OtherObject
{
    if ((_otherObject == null) && (_otherObjectId != 0))
        _otherObject = goGetTheOtherObject(otherObjectId);

    return _otherObject;
}

public function set otherObject(value : OtherObject) : void
{
// use the variable, not the accessor
    if(_otherObject != value)
    {
        _otherObject = value;
        _otherObjectId = 0;
    }
}