Software engineering notes

Simpleton Pattern

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!

Written by Erik

November 25, 2009 at 8:02 pm

Posted in Uncategorized

%d bloggers like this: