Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7528771
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T04:28:23+00:00 2026-05-30T04:28:23+00:00

I apologize in advance if my question is not clear, because I don’t know

  • 0

I apologize in advance if my question is not clear, because I don’t know how to put this.

What I am trying to do is to reduce few lines of repeated code by implementing various OOP methods/concepts.

The problem

I have few set of of classes which has initialization process. So, I am implementing an init() method in all those classes. From the calling class (main), these objects will be instantiated and init() method of each object is called in the the order and call some other process after all of them are initialized.

Something like this

     public function mainClass(){

        _obj1 = new Class1();
        _obj1.init();
        _obj2 = new Class2();
        _obj2.init();
        _obj3 = new Class3();
        _obj3.init();

        doSomething();
     }

Well, its not a big deal, but some of the classes’ init() methods are asynchronous and I need to add an event listener to get notified when they have finished initialization.

I tried that by extending EventDispatcher for each of those classes and dispatch event and handle it. I even implemented a logic to handle multiple asynchhnous calls by maintaining a counter.

It will be a painful job for me whenever I need to add a new class. I thought I could untilize OOP and reduce and simplify the code.

So I came up with some thing like this, which is currently not possible (abstract class).

abstract class Initializable
{
    private var _callBack:Function;

    //implement initializaton process in this method
    function init(callback:Function=null):void;

    protected function get callback():Function{
        return _callBack; 
    }

    protected function set callback(func:Function):void{
        _callBack = func;
    }

    protected function onComplete():void{

        if (_callBack){
            _callBack(this);
        }
    }


}

This is the main problem for me, as you know abstract class is not allowed in AS3, and the “this” refers to the Initializer class but not its subclass I guess.
This is what I am asking for your help (for the hack)

I need it very much to make my system design simple and flexible, because I can extend the solution to allow mass synchronous initialization which will allow to easily queue up all objects in the order and call init() one after the other in the order in which they are added.

The mass initializer which takes care of handling the asynchronous job

public class MassInitializer
{
    private var _objList:Array; //holds objects
    private var _callBacks:Array;
    private var _onComplete:Function;

    public function MassInitializer()
    {
        _objList = new Array();
    }

    public function add(obj:Initializable,callback:Function=null):void{
        _objList.push(obj);
        _callBacks.push(callback);
    }

    public function init():void{

        for (var i:int = 0;i < _objList.length;i++){
            _objList.init(this);
        }

    }
    private function onProgress(obj:Initializable):void{

        //do updates here
        for (var i:int;i<_objList.length;i++){

            var obj:Initializable = _objList[i];
            var fun:Function = _callBacks[i];
            //update progress
            if (fun){
                fun(obj);
            }

            _callBacks.splice(i,1);
            _objList.splice(i, 1);
        }

        if (_objList.length == 0){
            onComplete();
        }

    }

    private function onComplete():void{
        _onComplete(this);
    }

}

the main (manager/caller) class (ClassA, ClassB are subclasses of Initialzable class)

public class MainClass
{
    private var _obj1:ClassA;
    private var _obj2:ClassB;
    public function MainClass()
    {
        _obj1 = new ClassA();
        _obj2 = new ClassB();
    }

    public function init():void{

        var initManager:MassInitializer = new MassInitializer();

        initManager.add(obj1);
        initManager.start();

    }
}

probably I am trying to (or want to )implement an observer pattern, but I don’t want to confuse you by saying it in advance. Oops I said it? please ignore.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-30T04:28:25+00:00Added an answer on May 30, 2026 at 4:28 am

    You can emulate abstract classes in ActionScript by enforcing method overrides: Just throw an error if the “abstract” method is called. I like to also implement an interface, but that’s not a must, of course:

    public interface Initializable 
    {
        function init (callback : Function = null) : void;
        function get callback () : Function;
        function set callback ( callback : Function ) : void;
    }
    
    public class AbstractInitializableImpl implements Initializable
    {
        private var _callBack:Function;
    
        protected function init(callback:Function=null):void {
            throw new Error ("You must implement the init() method!");
        }
    
        protected function get callback():Function {
            return _callBack; 
        }
    
        protected function set callback(func:Function):void {
            _callBack = func;
        }
    
        protected function onComplete():void {
    
            if (_callBack){
                _callBack(this);
            }
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'd like to apologize in advance, because this is not a very good question.
I apologize in advance for asking this question, I know similar questions have already
I apologize in advance for not knowing how to better state this question. In
I apologize in advance if this question seems confused. The behaviour I am seeing
I apologize in advance; this is a long question. I've tried to simplify as
This is a very noobish question, so I apologize in advance! I have two
First of all I apologize in advance for this question, a bit off the
I apologize in advance for the rambling nature of this question, but I have
I apologize in advance for this somewhat ignorant question, but I have researched this
This question might be very abstract, so apologize in advance. I have a log

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.