On 4/23/07, Andrew <[EMAIL PROTECTED]> wrote:
Hi
I'm new to OOP programming in AS2.
I'm writing two classes right now. One which creates/manipulates an array
of XMLNodes and one which watches for an internet connection. When it finds
a connection, it starts to send the XMLNodes from the queue back to a
server one by one.
So far I've written the Queue class.
class com.Queue {
private var _milestoneXMLNode:XMLNode;
private var _aQueue:Array;
//Constructor
public function Queue() {
_aQueue = new Array();
}
//Methods
public function addMilestoneToQueue(milestoneXMLNode:XMLNode):Void {
_aQueue.push(milestoneXMLNode);
}
private function getQueue():Array {
return _aQueue;
}
private function getFirstMessageInQueue():XMLNode {
return _aQueue[0];
}
private function deleteFirstMessageFromQueue():Void {
_aQueue.splice(0,1)
}
}
Your private functions are kind of unnecessary. For the latter two,
there's no immediate reason they couldn't be made public. (But
getQueue() would break encapsulation; more on that later.)
Generally when doing structures like this I set it up like this:
class ItemList extends Object {
public function ItemList() {
super();
items = [];
}
public function addItem(item:Item):Void {
if (item instanceof Item) {
items.push(item);
}
}
public function countItems():Number {
return items.length;
}
public function getItem(index:Number):Item {
return Item(items[index]);
}
public function hasItem(item:Item):Boolean {
for (var i:Number = items.length - 1; i >= 0; --i) {
if (items[i] == item) {
return true;
}
}
return false;
}
public function removeItem(item:Item):Void {
for (var i:Number = items.length - 1; i >= 0; --i) {
if (items[i] == item) {
items.splice(i, 1);
}
}
}
public function toString():String {
return "[object ItemList (" + items.join(", ") + ")]";
}
private var items:Array;
}
For a stack, instead of addItem and removeItem you might have pushItem
and popItem. For a queue, you might have addItem and popItem. All
sorts of variations are possible.
If you ever have to convert it to an array, there are some tricks you
can use to make sure the private array is not externally modifiable.
For example:
public function toArray():Array {
return items.concat();
}
This will make an identical copy of the "items" array. Modifying it
will not affect "items".
--
T. Michael Keesey
Director of Technology
Exopolis, Inc.
2894 Rowena Avenue Ste. B
Los Angeles, California 90039
--
The Dinosauricon: http://dino.lm.com
Parry & Carney: http://parryandcarney.com
ISPN Forum: http://www.phylonames.org/forum/
_______________________________________________
Flashcoders@chattyfig.figleaf.com
To change your subscription options or search the archive:
http://chattyfig.figleaf.com/mailman/listinfo/flashcoders
Brought to you by Fig Leaf Software
Premier Authorized Adobe Consulting and Training
http://www.figleaf.com
http://training.figleaf.com