# Naughty use of Singleton?

**URL:** <https://forum.kirupa.com/t/naughty-use-of-singleton/265652>\
**Category:** flash\
**Created:** [July 11, 2008, 2:05am UTC](https://forum.kirupa.com/t/naughty-use-of-singleton/265652 "2008-07-11T02:05:36Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![sekasi](https://avatars.discourse-cdn.com/v4/letter/s/5f8ce5/32.png) [@sekasi](https://forum.kirupa.com/u/sekasi)\
**Post date:** [July 11, 2008, 2:05am UTC](https://forum.kirupa.com/t/naughty-use-of-singleton/265652/1 "2008-07-11T02:05:36Z")

</div>

I’ll try to describe the basic scenario;

It’s a pretty visually complex site, with lots of things going on and loads of mouse interaction in a plethora of different classes.

I’ve been struggling with the best and most efficient way to effectively “lock down” all mouse interaction in 70% of the objects when certain things happen. The best I could think of was to create a singelton that has a public setter. All the visual classes are listening to the Singelton and it dispatches Event.CHANGE when something happens which effectively triggers a lockdown in all the aux classes.

Is this … like… I mean. Will I go to Developer Hell for this? 🙂 I can’t think of any ways that don’t include a _LOT_ more code.

Thanks guys. 🏒

---

<div class="post-metadata">

**Author:** ![McGuffin](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/mcguffin/32/408_2.png) [@McGuffin](https://forum.kirupa.com/u/McGuffin)\
**Post date:** [July 11, 2008, 2:12am UTC](https://forum.kirupa.com/t/naughty-use-of-singleton/265652/2 "2008-07-11T02:12:41Z")

</div>

I like using a mixture of Singleton with something called a “semaphore”. Basically it acts as a lock, something as simple as…

```auto

package {
    class Navigation {
        private var _locked:Boolean = false;
        public function Navigation() {}
        
        public function isLocked():Boolean
        {
            return _locked;
        }

        public function lock():void
        {
            _locked = true;
        }

        public function unlock():void
        {
            _locked = false;
        }
    }
}

```

So not a true semaphore, but built off the same system, and you can obviously change this into a Singleton or a class that uses static methods. I just like it because a) it’s very very simple, and b) it logically makes sense.

```auto

mc.addEventListener( Event.CLICK, onClick );

function onClick( e:Event ):void
{
    if ( !navigationInstance.isLocked() ) trace( "navigation is unlocked" );
}

```

---

<div class="post-metadata">

**Author:** ![unhitched](https://avatars.discourse-cdn.com/v4/letter/u/b38774/32.png) [@unhitched](https://forum.kirupa.com/u/unhitched)\
**Post date:** [July 12, 2008, 2:07am UTC](https://forum.kirupa.com/t/naughty-use-of-singleton/265652/3 "2008-07-12T02:07:16Z")

</div>

McGuffin, thank you! Just what I was looking for and makes perfect sense.
