[Prev][Next][Index][Thread]
Re: methods on make
Gabor Greif <gabor68@yahoo.com> writes:
> Also think "singleton"-pattern (from the Gang of Four). \make would
> store the singleton instance in an (unexported) global and pass it
> back next time.
Here's a singleton pattern from Andy Armstrong from the newsgroup a
while ago. Slightly modified by me not to use each-subclass, as we
can't access the each-subclass slot without an existing instance.
---------8<-------------------------
// An implementation of the singleton pattern - based on example
// posted to comp.lang.dylan by Andy Armstrong.
define constant *singleton-instances* = make(<table>);
define open abstract class <singleton-class> (<object>)
// each-subclass slot
// singleton-instance :: false-or(subclass(<singleton-class>)) = #f;
end class <singleton-class>;
define method make ( class :: subclass(<singleton-class>), #key )
=> (object :: <singleton-class>)
element(*singleton-instances*, class, default: #f)
| begin
*singleton-instances*[class] := next-method()
end
end method make;
---------8<-------------------------
Chris.
--
http://www.double.co.nz/dylan
References: