1999Q3/
<!-- MHonArc v2.4.4 -->
<!--X-Subject: Re: [MUD&#45;Dev] Singleton design pattern (OT?) -->
<!--X-From-R13: Oqnz Ivttvaf <nqnzNnatry.pbz> -->
<!--X-Date: Tue, 31 Aug 1999 16:43:31 &#45;0700 -->
<!--X-Message-Id: Pine.SGI.3.96.990831161311.21900E&#45;100000#zazu,angel.com -->
<!--X-Content-Type: text/plain -->
<!--X-Reference: 199908312033.PAA18725#laurel,actlab.utexas.edu -->
<!--X-Head-End-->
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2//EN">
<html>
<head>
<title>MUD-Dev message, Re: [MUD-Dev] Singleton design pattern (OT?)</title>
<!-- meta name="robots" content="noindex,nofollow" -->
<link rev="made" href="mailto:adam#angel,com">
</head>
<body background="/backgrounds/paperback.gif" bgcolor="#ffffff"
      text="#000000" link="#0000FF" alink="#FF0000" vlink="#006000">

  <font size="+4" color="#804040">
    <strong><em>MUD-Dev<br>mailing list archive</em></strong>
  </font>
      
<br>
[&nbsp;<a href="../">Other Periods</a>
&nbsp;|&nbsp;<a href="../../">Other mailing lists</a>
&nbsp;|&nbsp;<a href="/search.php3">Search</a>
&nbsp;]
<br clear=all><hr>
<!--X-Body-Begin-->
<!--X-User-Header-->
<!--X-User-Header-End-->
<!--X-TopPNI-->

Date:&nbsp;
[&nbsp;<a href="msg00173.html">Previous</a>
&nbsp;|&nbsp;<a href="msg00175.html">Next</a>
&nbsp;]
&nbsp;&nbsp;&nbsp;&nbsp;
Thread:&nbsp;
[&nbsp;<a href="msg00167.html">Previous</a>
&nbsp;|&nbsp;<a href="msg00111.html">Next</a>
&nbsp;]
&nbsp;&nbsp;&nbsp;&nbsp;
Index:&nbsp;
[&nbsp;<A HREF="author.html#00174">Author</A>
&nbsp;|&nbsp;<A HREF="#00174">Date</A>
&nbsp;|&nbsp;<A HREF="thread.html#00174">Thread</A>
&nbsp;]

<!--X-TopPNI-End-->
<!--X-MsgBody-->
<!--X-Subject-Header-Begin-->
<H1>Re: [MUD-Dev] Singleton design pattern (OT?)</H1>
<HR>
<!--X-Subject-Header-End-->
<!--X-Head-of-Message-->
<UL>
<LI><em>To</em>: <A HREF="mailto:mud-dev#kanga,nu">mud-dev#kanga,nu</A></LI>
<LI><em>Subject</em>: Re: [MUD-Dev] Singleton design pattern (OT?)</LI>
<LI><em>From</em>: Adam Wiggins &lt;<A HREF="mailto:adam#angel,com">adam#angel,com</A>&gt;</LI>
<LI><em>Date</em>: Tue, 31 Aug 1999 16:20:43 -0700 (PDT)</LI>
<LI><em>Reply-To</em>: <A HREF="mailto:mud-dev#kanga,nu">mud-dev#kanga,nu</A></LI>
<LI><em>Sender</em>: <A HREF="mailto:mud-dev-admin#kanga,nu">mud-dev-admin#kanga,nu</A></LI>
</UL>
<!--X-Head-of-Message-End-->
<!--X-Head-Body-Sep-Begin-->
<HR>
<!--X-Head-Body-Sep-End-->
<!--X-Body-of-Message-->
<PRE>
On Tue, 31 Aug 1999, Cynbe ru Taren wrote:
&gt; &gt; Eli Stevens once said:
&gt; &gt; I have been working on the problem of how to best implement the Singleton
&gt; &gt; class [1] from _Design Patterns_, by Gamma, et al. in C++.  I have wasted a
&gt; &gt; lot of time fooling around with this, and now that I have an answer that I
&gt; &gt; think I like, I am going to throw it out in hopes that someone else will be
&gt; &gt; saved a bit of time.  :)
&gt; 
&gt; Last company I worked at (Activerse, a Java outfit) the guys were
&gt; threatening to &gt; strangle the next person to implement a Singleton in
&gt; company code.  Seems like in practice most of them turn out to need multiple
&gt; instances sooner or later, and recoding to allow this is a pain.

I'm pretty neutral on singletons (it always seemed to me that they don't
hold any particular advantage over a global variable), but they are very
popular here at Angel.  We've been using them heavily in our code for
years with nary a complaint.

Of course, it's very simple to create one: thanks to some macros we've long
used, you only need a single line in the class definition:

class foobar
{
   SINGLETON_INTERFACE(foobar);
/* rest of class stuff */
};

And one in the .c file itself:

#include "foobar.h"

SINGLETON_IMPLEMENTATION(foobar);

This implements GetInstance() and related functions.  Then we usually have
a macro for easy access in the .h:

#define FOOBAR foobar::GetInstance()


Generally we use this for all of the basic game "managers", including input,
render, sound, game, AI, and so forth.  So the code ends up looking like
this:


  [ ... ]

  GAME.AddToScore(1000);

  if (INPUT.ButtonPressed(BTN_3))
     /* ... */

  crCreature *cr = AI.FindClosestLivingCreature(pos);

  RENDER.Draw(cr-&gt;GetModel(), cr-&gt;GetMatrix());


Since they are all just macros, you could always change them to access
something else later anyways.  But we've never needed to.  *shrug*


Adam





_______________________________________________
MUD-Dev maillist  -  MUD-Dev#kanga,nu
<A  HREF="http://www.kanga.nu/lists/listinfo/mud-dev">http://www.kanga.nu/lists/listinfo/mud-dev</A>

</PRE>

<!--X-Body-of-Message-End-->
<!--X-MsgBody-End-->
<!--X-Follow-Ups-->
<HR>
<!--X-Follow-Ups-End-->
<!--X-References-->
<UL><LI><STRONG>References</STRONG>:
<UL>
<LI><STRONG><A NAME="00167" HREF="msg00167.html">Re: [MUD-Dev] Singleton design pattern (OT?)</A></STRONG>
<UL><LI><EM>From:</EM> Cynbe ru Taren &lt;cynbe#muq,org&gt;</LI></UL></LI>
</UL></LI></UL>
<!--X-References-End-->
<!--X-BotPNI-->
<UL>
<LI>Prev by Date:
<STRONG><A HREF="msg00173.html">RE: [MUD-Dev] Re: MUD-Dev digest, Vol 1 #142 - 4 msgs</A></STRONG>
</LI>
<LI>Next by Date:
<STRONG><A HREF="msg00175.html">RE: [MUD-Dev] Re: MUD-Dev digest, Vol 1 #142 - 4 msgs</A></STRONG>
</LI>
<LI>Prev by thread:
<STRONG><A HREF="msg00167.html">Re: [MUD-Dev] Singleton design pattern (OT?)</A></STRONG>
</LI>
<LI>Next by thread:
<STRONG><A HREF="msg00111.html">[MUD-Dev] Monthly FAQ Posting</A></STRONG>
</LI>
<LI>Index(es):
<UL>
<LI><A HREF="index.html#00174"><STRONG>Date</STRONG></A></LI>
<LI><A HREF="thread.html#00174"><STRONG>Thread</STRONG></A></LI>
</UL>
</LI>
</UL>

<!--X-BotPNI-End-->
<!--X-User-Footer-->
<!--X-User-Footer-End-->
<ul><li>Thread context:
<BLOCKQUOTE><UL>
<LI><STRONG>Re: [MUD-Dev] The Best Guy on the Mud Thing</STRONG>, <EM>(continued)</EM>
<ul compact>
<LI><strong><A NAME="00459" HREF="msg00459.html">Re: [MUD-Dev] The Best Guy on the Mud Thing</A></strong>, 
Ilya, Game Commandos <a href="mailto:Ilya#gamecommandos,com">Ilya#gamecommandos,com</a>, Wed 22 Sep 1999, 18:09 GMT
</LI>
</ul>
</LI>
<LI><strong><A NAME="00162" HREF="msg00162.html">[MUD-Dev] Singleton design pattern (OT?)</A></strong>, 
Eli Stevens (KiZurich) <a href="mailto:c718157#showme,missouri.edu">c718157#showme,missouri.edu</a>, Tue 31 Aug 1999, 17:05 GMT
<UL>
<LI><strong><A NAME="00159" HREF="msg00159.html">Re: [MUD-Dev] Singleton design pattern (OT?)</A></strong>, 
Wayne Pearson <a href="mailto:wpearson#verity,com">wpearson#verity,com</a>, Tue 31 Aug 1999, 18:22 GMT
</LI>
</UL>
<UL>
<li>&lt;Possible follow-up(s)&gt;<br>
<LI><strong><A NAME="00167" HREF="msg00167.html">Re: [MUD-Dev] Singleton design pattern (OT?)</A></strong>, 
Cynbe ru Taren <a href="mailto:cynbe#muq,org">cynbe#muq,org</a>, Tue 31 Aug 1999, 20:52 GMT
<UL>
<LI><strong><A NAME="00174" HREF="msg00174.html">Re: [MUD-Dev] Singleton design pattern (OT?)</A></strong>, 
Adam Wiggins <a href="mailto:adam#angel,com">adam#angel,com</a>, Tue 31 Aug 1999, 23:43 GMT
</LI>
</UL>
</LI>
</UL>
</LI>
<LI><strong><A NAME="00111" HREF="msg00111.html">[MUD-Dev] Monthly FAQ Posting</A></strong>, 
Ling Lo <a href="mailto:ling#frost,slimy.com">ling#frost,slimy.com</a>, Tue 17 Aug 1999, 22:24 GMT
<UL>
<LI><strong><A NAME="00115" HREF="msg00115.html">Re: [MUD-Dev] Monthly FAQ Posting</A></strong>, 
Greg Miller <a href="mailto:gmiller#classic-games,com">gmiller#classic-games,com</a>, Wed 18 Aug 1999, 20:51 GMT
</LI>
</UL>
<UL>
<li>&lt;Possible follow-up(s)&gt;<br>
<LI><strong><A NAME="00119" HREF="msg00119.html">RE: [MUD-Dev] Monthly FAQ Posting</A></strong>, 
Koster, Raph <a href="mailto:rkoster#origin,ea.com">rkoster#origin,ea.com</a>, Wed 18 Aug 1999, 21:28 GMT
<UL>
<LI><strong><A NAME="00123" HREF="msg00123.html">RE: [MUD-Dev] Monthly FAQ Posting</A></strong>, 
Ling Lo <a href="mailto:ling#frost,slimy.com">ling#frost,slimy.com</a>, Fri 20 Aug 1999, 22:58 GMT
</LI>
</UL>
</LI>
</UL>
</LI>
</UL></BLOCKQUOTE>

</ul>
<hr>
<center>
[&nbsp;<a href="../">Other Periods</a>
&nbsp;|&nbsp;<a href="../../">Other mailing lists</a>
&nbsp;|&nbsp;<a href="/search.php3">Search</a>
&nbsp;]
</center>
<hr>
</body>
</html>