This is a really minor thing, but I've got it stuck in my craw...

I'm trying to figure out a 'clever' way to implement singleton
patterns and am wondering if anyone has one.

The book suggests doing:

   call Singleton ()
       method aMethod()
       end
       initially
           Singleton := self
   end

which is really nifty, converting the constructor into an
instance of the class itself!

However, this means that you need to know the (temporal)
location of the first invocation and distinguish from
subsequent invocations [the first one would be Singleton(),
and all subsequent invocations would be something like
Singleton.aMethod().  Which means that code such as:

    case ?"ab" of {
        "a" : Singleton().aMethod()
        "b" : Singleton.aMethod()
        }

is problematic.  I can *almost* solve the problem by doing something
like:

    class Singleton
        method aMethod()
        end
        initially
            Singleton := aSingleton
            aSingleton(self)
    end

    procedure aSingleton(c)
        static s
        # the test here handles the case where someone calls
        #   aSingleton directly...
        initial s := if Singleton === aSingleton then c
                                                 else Singleton()
        return s
    end

and now the above (useless) case expression could be written:

    case ?"ab" of {
        "a" : Singleton().aMethod()
        "b" : Singleton().aMethod()
    esac

but that leaves this procedure "aSingleton" lying around, which just
bothers me...

Does anyone have another way?

Thanks!
Steve


-- 
Steve Wampler <[EMAIL PROTECTED]>
National Solar Observatory


-------------------------------------------------------
This SF.NET email is sponsored by:
SourceForge Enterprise Edition + IBM + LinuxWorld = Something 2 See!
http://www.vasoftware.com
_______________________________________________
Unicon-group mailing list
[EMAIL PROTECTED]
https://lists.sourceforge.net/lists/listinfo/unicon-group

Reply via email to