Skip to main content
February 15, 2013
Answered

Static vars - quick question

  • February 15, 2013
  • 1 reply
  • 2169 views

Gidday

Slowly converting my timeline coded AIR dekstop app to OOP, and I'm up to the part where I'm loading in user settings from an SQL table.

Just wondering, is a static var a good thing to use for holding user settings? 

I was thinking of having a UserSettings class that user settings can be held in for getting and setting as they are needed or changed.

Then on save, I can just iterate through them and save them back into SQL.

And all my classes can have access to them when needed.

Or is there a better way?

Cheers

This topic has been closed for replies.
Correct answer Amy Blankenship

You need to be more specific about what YipeeClass is and what it really needs. YippeeClass probably does not need access to all of the settings variables.

For example, if YippeeClass is a View, it probably just needs the things it's designed to show. You'd provide properties (getters/setters) on the View, and then your main Document Class (or better yet, a dedicated Controller Class) would look at the settings and populate the properties of the View based on the relevant settings. The Document Class wouldn't worry about how those settings affect the View--it's now the responsibility of the View to translate the properties it got in whatever way (such as setting the text of a text field). This is known as Dependency Injection.

If the user interacts with the View in a way that impacts the settings, you dispatch an event from the View that will be caught by a controller or by the main document Class and that Class will then make the changes needed to the settings.

Classes should always know the absolute minimum needed to accomplish whatever they're directly responsible, and each Class should be responsible for one thing.  I would suggest that you get the book associated with the links I've posted: Actionscript 3 Design Patterns. It at least would give you a framework for thinking about this stuff.

1 reply

Amy Blankenship
Legend
February 15, 2013

NO! Statics have only one use in good OOP, and that is in storing values that never change. Start cultivating good habits now

February 15, 2013

Thanks Amy

So would a good alternative still be to have a class that holds all the user settings (private vars), and then use getters and setters to acccess and modify them?

If so, the main thing that evades me (and why I considered statics) is - how do you access your vars from classes other than the doc class, without doing something like:

(in doc class)

public static var instance:DocumentClass

public var user_settings:UserSettings = new UserSettings();

(in another class that needs to access the user settings - say YippeeClass)

myBackgroundColor = DocumentClass.instance.user_settings.backgroundColor;

because instantiating ...

public var user_settings:UserSettings = new UserSettings();

...in YippeeClass will give me a whole new set of user setting vars

What is the best coding habit in this respect?

Cheers

Amy Blankenship
Amy BlankenshipCorrect answer
Legend
February 15, 2013

You need to be more specific about what YipeeClass is and what it really needs. YippeeClass probably does not need access to all of the settings variables.

For example, if YippeeClass is a View, it probably just needs the things it's designed to show. You'd provide properties (getters/setters) on the View, and then your main Document Class (or better yet, a dedicated Controller Class) would look at the settings and populate the properties of the View based on the relevant settings. The Document Class wouldn't worry about how those settings affect the View--it's now the responsibility of the View to translate the properties it got in whatever way (such as setting the text of a text field). This is known as Dependency Injection.

If the user interacts with the View in a way that impacts the settings, you dispatch an event from the View that will be caught by a controller or by the main document Class and that Class will then make the changes needed to the settings.

Classes should always know the absolute minimum needed to accomplish whatever they're directly responsible, and each Class should be responsible for one thing.  I would suggest that you get the book associated with the links I've posted: Actionscript 3 Design Patterns. It at least would give you a framework for thinking about this stuff.