A dictionary with a built-in factory?
<p>I have a solution that works, but for educational purposes I want to understand if there is a better/cleaner/right way to do it.</p>

<p>Problem: In my "client" app I have a dictionary <code>Dictionary&lt;String, PremiseObject&gt;</code> where the key (String) is a immutable URL to a resource (it's actually a REST URL).  PremiseObject is the base type of a whole set of derived classes; thus the Dictionary actually contains a family of classes all derived from PremiseObject.</p>

<p>A key requirement is I want to try to 'guarantee' that no PremiseObjects get created OUTSIDE of the dictionary. </p>

<p>Solution: I have the following function for getting a object out of the dictionary. It either accesses the existing instance, or if it does not exist creates it:</p>

<pre><code>public PremiseObject GetOrCreateServerObject(string premiseObjectType, string location)
{
    PremiseObject po = null;
    if (!premiseObjects.TryGetValue(location, out po))
    {
        string classname;
        // Create an instance of the right PremiseObject derived class:
        po = // gobbly-gook that is not relevant to this question.
        premiseObjects.Add(location, po);
    }
    else
    {
        Debug.WriteLine("Already exists: {0}", location);
    }
    return po;
}
</code></pre>

<p>Callers do this:</p>

<pre><code>DoorSensor door = 
         (DoorSensor)server.GetOrCreateServerObject("DoorSensor", 
                                                    "http://xyz/FrontDoor");
</code></pre>

<p>Works great. But I think there's a pattern or design that would elegantly allow me to encapsulate the "single-instance of each object contained in the dictionary" more.  </p>

<p>For example, callers could do this:</p>

<pre><code>DoorSensor door = null;
if (!server.ServerObjects.TryGetValue("DoorSensor",
                                      "http://xyz/FrontDoor", 
                                      out door))
    Debug.WriteLine("Something went very wrong");
</code></pre>

<p>I'm not really what to call this pattern. My ServerObjects are "single-instanced" by location. And my GetOrCreateServerObject is like a factory that lazy creates.</p>

<p>But it's possible for instances to be created that don't get put into the dictionary, which could lead to problems. </p>

<p>Like I said, what I have works...  Cheers!</p>

<p><strong>UPDATE 1/26/2011 10:13PM -</strong> 
I just realized a potential problem: On the server side the object represented by a <code>location/URL</code> can actually be multi-inherited. It is THEORETICALLY possible for an object to be both a <code>DoorSensor</code> and an <code>DigitalRelay</code>. </p>

<p>I currently don't care about any of those cases (e.g. for garage doors I simplified my example above; there really is no DoorSensor I exposed, just a GarageDoorOpener which includes BOTH properties for sensing (e.g. <code>Status</code>) and actuation (e.g. <code>Trigger</code>). But this puts a wrinkle in my whole scheme if I were to care. Since this project is just for me :-) I am going to declare I don't care and document it. </p>
