views:

398

answers:

5
+2  Q: 

Name this pattern

Often seen it and often used it, wonder if it has a name?

C# version:

public class Register
{
    protected Register()
    {
        Register.registry.Add(this);
    }

    public static ReadOnlyCollection<Register> Instances
    {
      get { return new ReadOnlyCollection<Register>(registry); }
    }

    private static List<Register> registry = new List<Register>();
}

it keeps a track of instances created if you couldn't work it out :)

Edit: it's just a snippet, don't get over excited about GC issues people

+4  A: 

A memory leak? No instances of Register will ever be collected, unless you provide a way to explicitly remove them from the static list "registry".

Robert
Actually, the garbage collection issue can be avoided easily by usining WeakReference.
DrJokepu
well indeed, it's just a snippet of the bit which is relevant
annakata
Agreed WeekReference is a good solution, sorry if my post seemed rude but the memory probably is really the first thing that jumps out at me.
Robert
Not rude, but fixated on a detail perhaps :)
annakata
A: 

Looks like you are trying to keep track of all instances of an object... why?

baretta
One live use case is for a status page which can report on all the live instances of the class. That implementation is actually abstract as well, so it's reporting on all the various flavours of instances.
annakata
Can also use this to keep track of all the sessions that an application creates. This way you know how many users are active in the system. There are actually many more instances where you want to do this kind of thing in a stand-alone custom server.
sjbotha
I can see the use for this kind of information, although maybe there are other approaches, such as profiling systems etc.. However, i don't know if this is a common pattern, with its own name.
baretta
@sjbotha: ooh that's a solid example right there...
annakata
@sjbotha: For tracking users online, you could consider using the database, or session state
baretta
@Dennis - but what you're not seeing is that session *is* an implementation of this pattern
annakata
You are right, I really don't see that...
baretta
well behind session is a factory which has to be keeping track of the session instances...
annakata
I see your point, this pattern might be applicable in some context. But as far as session pattern; if you're talking ASP.NET Session State, i guess this data is just indexed using the session ID, which the client sends to identify itself. I don't think this pattern is used here
baretta
+2  A: 

It not a Factory Pattern as it doesn't involve the use of a separate object to create the instances. It works more like a Lazy Initialization Pattern.

It is used when you need to control all instances of a class.

RS Conley
hmm, shares a lot of the same features, but not factoried
annakata
+1  A: 

It reminds me somewhat of string interning.

Bill the Lizard
yeah I can kinda see the relationship there too, but interning seems like RS Conley's Lazy Init Pattern more than this.
annakata
A: 

Singleton (of the collection).

whether the way it's used here constitutes a pattern or an anti-pattern, though: you be the judge. do you ever use it this way with inheritable objects?

ifatree