I am developing an Air application that interacts with my server. There are plenty of various methods related to different subjects.
To structure that, I introduce a master ‘API’ object and several extenders of it, each implementing methods for particular Subject. Each of them uses the master API _call method that actually makes a call to server.
How do I set the master API, so that I could reference its children like so:
var Api:myMasterApi = Application.getApi();
// Now do the API calls:
Api.Subject1.Method1();
Api.Subject2.Method3();
I tryed setting MasterApi public static properties to the instantiated subclasses, perhaps in a wrong way:
class myMasterApi {
public static Subject1:mySubject1 = new mySubject1();
public static Subject2:mySubject2 = new mySubject2();
protected function _call( args... ) { /* does the call */ }
}
And the extenders are like so:
class mySubject1 extends myMasterApi {
public function Method1( args... ) {
prepare things;
this._call( args ); // do the call
}
}
Is this the right approach to a structured implementation of API, or I’m on the wrong track?
The problem I’m stuck with is that when Api.Subject1 is not static it creates stack overflow by circular self reference: as its base class has Subject1 as a property.
When Api.Subject1 is static, it gives error 1119: “Access of possibly undefined property post through a reference with static type …”
You’re mixing up two different fundamental OOP concepts: composition and inheritence. It probably doesn’t make any sense for your specific
Subjectclasses to be extendingMasterApi(inheritence) AND have theMasterApicontain an instance of each of its descendents. It is circular.What you may want to do instead is have a base
Subjectclass that each of the specificSubjectsextends, so that they can all use some common methods, and then have yourMasterApibe an unrelated class that contains a singleton instance of each of your specificSubjects.‘Singleton’ is just a term meaning “it’s an instance (not a static class), but there is only and exactly one of them”. This is exactly what you get by declaring your
staticproperties and setting them to new instances of the specificSubjectclasses.A barebones example (excluding
importsstatements, etc). I have no idea what your “subjects” are so I just present some made up properties: