Skip to main content
January 15, 2013
Question

A general OOP question

  • January 15, 2013
  • 2 replies
  • 983 views

Hi

I have a general OOP design question, and am wondering if someone could relate an answer to the following design?

I have a class called MediaFolderImport(); - it's designed to build a window with various editing tools in it.

Within it's constructor, I'm calling a bunch of functions to build the window...

   createTitle();

   createInstructions();

   createToolPanel();

   createDataGrid ();

   createOpen();

   createSave();

In my document class, I instantiate it...

public var File_Folder_Import:MediaFolderImport=new MediaFolderImport();

and then...

addChild(File_Folder_Import);

Voila! - the window appears. I WAS very proud of myself.

Now I want to access something inside the window.  Specifically, there's a radio button that was created in createToolPanel(); - I want to update it to be selected or not selected when I receieve the user's preference from an xml settings file at start up (xml is loaded into the doc class).

General question:

What is the best practice, smart way to have designed this?

- call createToolPanel(); from the doc class instead of within MediaFolderImport();, and somehow (magically) have access to the radio button?

- leave the design as is, but add some sort of listener within MediaFolderImport that listens for changes to the xml in the doc class, and updates accordingly?

- do it the way I'm trying to, ie try to access the radio button directly from the doc class (which isn't working):

File_Folder_Import.myRadioButton.selected = true;

- a better way someone can briefly explain the concept of?

Another way to explain my design is...

- a bunch of different windows, each created by a different class

- xml file loads preferences, which need to be applied to different tools (radio buttons, check boxes, text fields etc) in the different windows

I read a lot of posts that talk about how public vars are mostly bad practice.  So if you are making your class vars private, what is the best way to do the kind of inter-class communicating I'm talking about here?

I think someone throwing light on this will help me solidify my understanding of OOP.

Thank you for your time and help.

This topic has been closed for replies.

2 replies

sinious
Legend
January 15, 2013

You're already very used to using properties for the built-in AS classes and that's the best practice means of configuring your class. It's a "state" that you want to simply expose. The get/set method moccamaximum mentioned is the ideal route.

The main reason you want to use get/set functions is validation. You want your class to act properly if you send an invalid value. Especially if anyone else besides yourself is going to use the class. Plan for the worst.

The general concept is, make a private variable for any 'state' you want to remember using an underscore in the variable name to easily identify it as a private var, then make get/set functions with the same name with any required validation without the underscore.

e.g.

package

{

     public class MyClass

     {

          // property called 'mode' to track something with an int

           private var _mode:int = 0;

          public function MyClass() {} // empty constructor

          // get (type enforced)

          public function get mode():int { return mode; }

          // set, requiring a value

          public function set isChecked(modeVal:int):void

          {

               // if no value is sent, ignore

               if (!modeVal) { return; }

               _mode = modeVal;

          }

     }

}

Your validation will go a long way to easily debugging your classes as they grow in size. Eventually they should throw exceptions and errors if they're not valid. Then you will be best practice. Do note that if your validation requires quite a bit of logic it's common to upgrade the property to a public method. get/set should be reserved for simple properties.

January 15, 2013

And thanks Sinious - I just caught your reply too.  I'll practice adding a radio button to your class tomorrow, and seeing if I can set it's state from the doc class.

sinious
Legend
January 15, 2013

You use the private var (e.g. _mode above) for safety when checking its current value but you typically use your own setter method to actually set the property  (e.g. mode(5) instead of _mode = 5;) so your own validation is utilized both yourself as well as outside class users.

Inspiring
January 15, 2013

I read a lot of posts that talk about how public vars are mostly bad practice.  So if you are making your class vars private, what is the best way to do the kind of inter-class communicating I'm talking about here?

Have look at getters/setters and how/why to use them properly in as3. (Point 13 here)

January 15, 2013

OK - I'm going to sleep on that, and re-read the part about implicit setters. I think how it ties with what I'm trying to do is...

- use a setter function in the class that built the radio button to update a private var with the xml setting, and that private var controls the radio button somehow.  I haven't got it a 100%, but a fresh brain tomorrow will.

Thank you - that article is fantastic.