views:

52

answers:

2

In an attempt to organize my code, I'm trying to split up my (lengthy) main controller class into separate files, but my new files must still have access to the variables and functions of the main controller class.

I'm trying to cut and paste code from my controller class into a new class/file, allowing the controller class to call the new class, and allowing the new class to have access to the controller class's properties and function.

Assuming I'm not totally bludgeoning appropriate design patterns, below is my unsuccessful attempt at accomplishing this task:

package
{   
import flash.display.Sprite;
import flash.events.Event;

public class Test extends Sprite
    {
    public var myString:String;

    public function Test()
        {
        if  (stage)
            init(null);
            else
            addEventListener(Event.ADDED_TO_STAGE, init);
        }

    private function init(evt:Event):void
        {
        if  (hasEventListener(Event.ADDED_TO_STAGE))
            removeEventListener(Event.ADDED_TO_STAGE, init);

/////////////// MOVE COMMENTED CODE TO NEW FILE ///////////////////////
//      
//      //Assign The String A Value
//      myString = "Hello world";
//      
//      //Draw A Blue Square
//      var sq:Sprite = new Sprite();
//      sq.graphics.beginFill(0x0000FF);
//      sq.graphics.drawRect(10, 10, 100, 100);
//      sq.graphics.endFill();
//      super.addChild(sq);
//      
//      //Call Tracer Function
//      tracer();
//
//////////////////////////////////////////////////////////////////////

        //Call pasted method in NewFile.as
        NewFile.myNewFunction();     // <- this doesn't work
        }

    public function tracer():void
        {
        trace(myString);
        }
    }
}

new file doesn't have access to the Controller class - doesn't work. how can i write the new file so that it does have access to the properties, functions, stage, etc. of the Controller class, as if its code was never removed and is still in its original place.

package
{   
public class NewFile
    {
    public static function myNewFuntion():void
        {
        //Assign The String A Value
        myString = "Hello world";

        //Draw A Blue Square
        var sq:Sprite = new Sprite();
        sq.graphics.beginFill(0x0000FF);
        sq.graphics.drawRect(10, 10, 100, 100);
        sq.graphics.endFill();
        super.addChild(sq);

        //Call Tracer Function
        tracer();
        }
    }
}
+3  A: 
public class MainClass extends Sprite
{
    private var subClass:SubClass;

    public function MainClass
    {
       var controller:Controller = new Controller();

       subClass = new SubClass(controller);
       addChild( subClass );
    }

    private function init():void
    {
       subClass.doWhatever();
    }
}

public class Controller
{

    public function doThis():void
    {
     }
    public function doThat():void
    {
         trace("controller do that...");
     }
    public function doSomethingElse():void
    {
     }
}

public class Subclass extends Sprite
{
     private var controller:Controller;

     public function Subclass(controller:Controller)
     {
         this.controller = controller;
         trace( "new Subclass instance!" );
     }

     public function doWhatever():void
     {
         controller.doThat();
     }
}
PatrickS
this doesn`t work since i also want to use properties and functions of the superclass inside functions of the subclass. maybe there is a more straight forward approach? maybe using a static class that can be called from my controller class, though i`m still not sure if i`ll be able to use methods and variables of the controller class in the static class.
TheDarkInI1978
i've edited my question so that what i'm trying to do is more clear.
TheDarkInI1978
PatrickS
ok... i should probably just not do what i'm trying to do since it's neither easy nor common. i'll look into design patterns.
TheDarkInI1978
it's not common because your classes would be tightly coupled with your main class making it very difficult to upgrade or even debug. One of the main concepts in design patterns is the black box principle, meaning that you can interact with an object without having to know what's inside and the object being dependent on what's outside to function. This is basically how your classes should be structured. Then you'd be able to remove, add or make whatever changes to a class without breaking anything.
PatrickS
+2  A: 

This code

//Call Tracer Function 
tracer(); 

is not going to work, since tracer is not a static method.

This one :

super.addChild(sq);

won't work either, since NewFile doesn't inherit any class ; super() calls the homonyme method of the mother class in an INHERITANCE relation. What you should do there is more probably a COMPOSITION or AGGREGATION relation : newFile IS NOT a Controller, but Controller HAS a newFile.

It's difficult to know exactly what is wrong if you do not give us an error message.

On the design aspect, I have to agree with PatrickS. You might want to check the composite pattern, which could be what you need there.

Raveline
yeah, i think i've tried all i could on this. what might have worked is to set the properties and functions needed by my newFile class as static, but the problem there was that my main controller class is a document class, which also needs to have those properties and functions as non static. i'm making a mess and will retreat. thanks for the input.
TheDarkInI1978