Erik's blog

Code, notes, recipes, general musings

Simpleton Pattern

leave a comment »

Simpleton pattern

Problem

We don’t want multiple objects of the same class, but we also don’t want to clutter our code with the kind of checks required to implement the Singleton pattern

Solution

Kill program execution if a second attempt to instantiate an object occurs

Example

class Foo {
    function constructor(){
        if(objectOfClassAlreadyExists('Foo')){
            stopExecution();
        }

        ...

    }
}

...

variable foo = new Foo //all good

...

variable bar = new Foo  //poof!
Advertisements

Written by Erik

November 25, 2009 at 8:02 pm

Posted in Uncategorized

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: