PDA

View Full Version : Trying to understand this code...



riveroaks
November 21st, 2008, 05:49 PM
I'm working on creating a liquid Flash layout and I found one tutorial that seems pretty helpful at http://www.adobe.com/devnet/flash/articles/liquid_gui_04.html

But can someone help me understand part of this code? My question about the code is down below the code

inside BodyUI Class


....
public static var _instance:BodyUI;
public var bg:Sprite;
private var _parent:Sprite;
public function BodyUI (p:Sprite)
{
_parent = p;
bg = new BodyBG ();
addChild (bg);
}
public static function getInstance (p:Sprite):BodyUI
{
if (_instance == null)
_instance = new BodyUI (p);
return _instance;
}....


then inside LiquidGUI_v3 Class

public function LiquidGUI_v3 ()
{
// Tell the player not to scale assets
stage.scaleMode = StageScaleMode.NO_SCALE;
// Tell the player to put coords 0,0 to the top left corner
stage.align = StageAlign.TOP_LEFT;
// Listen for resizing events
stage.addEventListener(Event.RESIZE, onResize);
// Create and add the Header UI
_header = HeaderUI.getInstance (this);
addChild (_header);
// Create and add the Body UI
_body = BodyUI.getInstance (this);
addChild (_body);
_body.y = _header.height;
// Create and add the Footer UI
_footer = FooterUI.getInstance (this);
addChild (_footer);
// Size everything after creation to ensure the app is drawn
// properly the first time it is seen prior to any user-
// initiated resizing
onResize (null);
}


my question is: why is there a static variable called "_instance" in the BodyUI class? I see that in the LiquidGUI class the _body is actually retrieved by using the _instance variable in BodyUI....but why not just say

_body = new BodyUI()

instead of saying

_body = BodyUI.getInstance()?

I just don't understand how it makes sense to have a static variable here...it seems to be short-circuiting the OOP principles, not extending them.

Thanks

Krilnon
November 21st, 2008, 06:28 PM
It's an implementation of the singleton pattern.


it seems to be short-circuiting the OOP principles, not extending them.

Some people don't like singletons for that reason, haha.

Styles
November 22nd, 2008, 07:55 AM
I'm working on creating a liquid Flash layout and I found one tutorial that seems pretty helpful at http://www.adobe.com/devnet/flash/articles/liquid_gui_04.html

But can someone help me understand part of this code? My question about the code is down below the code

inside BodyUI Class


....
public static var _instance:BodyUI;
public var bg:Sprite;
private var _parent:Sprite;
public function BodyUI (p:Sprite)
{
_parent = p;
bg = new BodyBG ();
addChild (bg);
}
public static function getInstance (p:Sprite):BodyUI
{
if (_instance == null)
_instance = new BodyUI (p);
return _instance;
}....
then inside LiquidGUI_v3 Class

public function LiquidGUI_v3 ()
{
// Tell the player not to scale assets
stage.scaleMode = StageScaleMode.NO_SCALE;
// Tell the player to put coords 0,0 to the top left corner
stage.align = StageAlign.TOP_LEFT;
// Listen for resizing events
stage.addEventListener(Event.RESIZE, onResize);
// Create and add the Header UI
_header = HeaderUI.getInstance (this);
addChild (_header);
// Create and add the Body UI
_body = BodyUI.getInstance (this);
addChild (_body);
_body.y = _header.height;
// Create and add the Footer UI
_footer = FooterUI.getInstance (this);
addChild (_footer);
// Size everything after creation to ensure the app is drawn
// properly the first time it is seen prior to any user-
// initiated resizing
onResize (null);
}
my question is: why is there a static variable called "_instance" in the BodyUI class? I see that in the LiquidGUI class the _body is actually retrieved by using the _instance variable in BodyUI....but why not just say

_body = new BodyUI()

instead of saying

_body = BodyUI.getInstance()?

I just don't understand how it makes sense to have a static variable here...it seems to be short-circuiting the OOP principles, not extending them.

Thanks



The BodyUI class makes use of a design pattern called the Singleton design pattern which is a type of design pattern that make sure that only one instance of a class can be initiated at any time therefore disallowing you to create multiple instances of a same class.

Basically the way it works is like uppon instanciation it will check the existance of any prior intance, if there is it will return that instance and if there isn't it will create one and return it.
The getInstance method is there just for that. if it wasn't you will be allowed to create multiple instances of the same class by using the "new" keyword which is against the principles of the singleton pattern.

Anyway, being new to OOP, I don't know if I'm making myself clear here so I suggest you google Singleton design pattern to find out more about its principles.

Hope it helped.