Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
0

whats wrong with my singleton class

Explorer ,
Apr 04, 2014 Apr 04, 2014

I have written a singleton class called Stone.as shown below?

package mesh {

 

          public final class Stone{

                    private static var _instance:Stone;

                    public function Stone() {

                              if(_instance){

                                        throw new Error("Singleton... use getInstance()");

                              }

                              _instance = this;

                    }

                    public static function getInstance():Stone {

                              if(!_instance){

                                        _instance = new Stone();

                              }

                              return _instance;

                    }

          }

}

In the calling class I do this.

var stone:Stone = Stone.getInstance();

However this throws an error;

1061: Call to a possibly undefined method getInstance through a reference with static type Class.
TOPICS
ActionScript
608
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Guide ,
Apr 04, 2014 Apr 04, 2014

The code looks right, except that you're actually calling the constructor in the getInstance() method, which should throw your custom error. But honestly using Singleton will cause you a ton of trouble down the road. What are you trying to do? I can probably help you solve it either through dependency injection or using events to communicate, both of which offer superior encapsulation/decoupling.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Apr 04, 2014 Apr 04, 2014

Most probably you have another Class Stone (which does not have static getInstance() method) somewhere in your package or, if you use Flash IDE, there may be a symbol with Class name Stone.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Apr 04, 2014 Apr 04, 2014

Andrei1 wrote:

Most probably you have another Class Stone (which does not have static getInstance() method) somewhere in your package or, if you use Flash IDE, there may be a symbol with Class name Stone.

Thanks. It seems I had forgot to remove a symbol from the Flash IDE library with the same Class name.

Amy is probably right in that a singleton may not be the answer I'm looking for. What I need is classes that if called.

  • the first time it gets constructed
  • subsequent calls the already constructed class gets called.

Each class may or may not be required. This is controlled by a data source, and I do not want the class constructor unless it is needed.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Guide ,
Apr 04, 2014 Apr 04, 2014
LATEST

rerobnett

You are incorrect that you need to do that. If you satisfy your instance's dependencies from outside, that outside thing (for instance, a Controller Class or even your Main Document Class) can only create one instance as needed and pass it in. That way, if you need two instances down the road, you haven't *'ed yourself royally. More importantly, you don't have all these hands touching your Stone and no way to control who is changing what. I've worked on an enterprise-level project where someone thought it was a good idea to stick a bunch of Singletons in, and it was a royal pain in the asterisk to track down any bug because hundreds of places in the code could have changed a value to something wrong.

For example, you could do something like this:

public class Main extends Sprite {

     protected var stone:Stone = new Stone();//just created one instance, no other place in the code do we do this

     public function Main() {

          super();

          addEventListener(Event.ADDED_TO_STAGE, checkStoner, true);

     }

     protected function checkStoner(e:Event):void {

          //any MC's that want a reference to the stone will

          //express that interest by implementing IStoner

          var stoner:IStoner = e.target as IStoner;

          if (stoner) {

               stoner.stone = stone;

          }

     }

}

The Interface would look like this:

public interface IStoner {

     function get stone():Stone;

     function set stone(value:Stone):void;

}

An implementation might look like this:

public class StoneMover extends Sprite implements IStoner {

     protected var _stone:Stone;

     public function StoneMover() {

          super();

          //listen for your custom event type and move the stone based on receiving that

          //note I am not providing you the code for that, I assume you know how to write an Event Class

          stage.addEventListener(CustomEvent.CUSTOM_EVENT_TYPE, moveStone);

     }

     public function get stone():Stone {

          return _stone;

     }

     public function set stone(value:Stone):void {

          if (value != _stone) {

               //may want to future-proof with cleanup code here

               _stone = value;

               //if you need to listen to the stone, do that here

          }

     }

     protected function moveStone(e:CustomEvent):void {

          if (e.customProperty = 'foo') {

               stone.x++;

          } else {

               stone.y++;

          }

     }

}

An alternate approach you could take is communicating via events, either on the Display List or via one or more EventDispatchers that can be handed out to the right instances.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines