Reputation: 1601
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 ..."
Upvotes: 1
Views: 114
Reputation: 13215
You're mixing up two different fundamental OOP concepts: composition and inheritence. It probably doesn't make any sense for your specific Subject
classes to be extending MasterApi
(inheritence) AND have the MasterApi
contain an instance of each of its descendents. It is circular.
What you may want to do instead is have a base Subject
class that each of the specific Subjects
extends, so that they can all use some common methods, and then have your MasterApi
be an unrelated class that contains a singleton instance of each of your specific Subjects
.
'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 static
properties and setting them to new instances of the specific Subject
classes.
A barebones example (excluding imports
statements, etc). I have no idea what your "subjects" are so I just present some made up properties:
class SubjectBase {
private var _name:String;
private var _description:String;
public function SubjectBase() { }
protected function loadSubject(subjectID:String):void {
var qResult:Object = SomeClass.SomeMethodThatQueriesYourServer(subjectID);
_name = qResult["SUBJECT_NAME"];
_description = qResult["SUBJECT_DESC"];
}
public function get name():String {
return _name;
}
public function get description():String {
return _description;
}
}
class Subject1 extends SubjectBase {
public function Subject1() {
super();
loadSubject("SUBJECT1");
}
}
class Subject2 extends SubjectBase {
public function Subject2() {
super();
loadSubject("SUBJECT2");
}
}
class MasterApi {
private static _subject1:Subject1 = new Subject1();
private static _subject2:Subject2 = new Subject2();
public function MasterApi() { }
public static function get subject1():Subject1 { return _subject1; }
public static function get subject2():Subject2 { return _subject2; }
}
Upvotes: 2
Reputation: 48147
Do you want Subject1
and Subject2
to be static? If so, myMasterApi.Subject1
is how you get to those static items.
Instead, you probably want to remove the static from the definitions, and everything should work for you.
Upvotes: 0