I'm taking a stab at creating a multiton in Flex 2.0. Is there an easier way to do it other than how I have done it in the code below? I'm looking for the best implementation.
 
Thanks!
 

package com.model {
 import flash.net.SharedObject;
 import mx.collections.ArrayCollection;
 
 public class Record {
  
  private static var instance:Record = null;
  private static var blocker:Object = null;
  
  private var _so:SharedObject;
  
  public function Record(record:int) {
   _so = SharedObject.getLocal(record.toString());
   if(blocker == null)
    throw new Error("Public construction not allowed. Use getInstance()");
  }
  
  public static function getInstance(record:int):Record {
   blocker = new Object();
    var key:Object = new Object();
    key.value = record;
    instance = Record.get(key);
    if(instance == null) {
     instance = new Record(record);
     Record.put(key, instance);
    }
   
   return instance;
  }
  
  public function set i(s:String):void {
   _so.data.i = s;
   _so.flush();
  }
  
  public function get i():String {
   return _so.data.i;
  }

 

 


  // INSTANCE FUNCTIONS TO MANAGE MULTITON...
  public static var instanceContainer:ArrayCollection = new ArrayCollection();
  
  public static function put(key:Object, instance:Object) {
   var newInstance:Object = new Object();
   newInstance.key = key;
   newInstance.instance = instance;
   instanceContainer.addItem(newInstance);
  }
  
  public static function get(key:Object) {
   for(var i=0;i<instanceContainer.length ;i++) {
    var currentInstance = instanceContainer.getItemAt(i);
    if(currentInstance.key == key)
     return currentInstance.instance;
    else
     return null;
   }
  }

  
 }
}



--
Flexcoders Mailing List
FAQ: http://groups.yahoo.com/group/flexcoders/files/flexcodersFAQ.txt
Search Archives: http://www.mail-archive.com/flexcoders%40yahoogroups.com




SPONSORED LINKS
Web site design development Computer software development Software design and development
Macromedia flex Software development best practice


YAHOO! GROUPS LINKS




Reply via email to