annakata
annakata

Reputation: 75794

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

Upvotes: 2

Views: 538

Answers (5)

ifatree
ifatree

Reputation: 843

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?

Upvotes: 0

Bill the Lizard
Bill the Lizard

Reputation: 405695

It reminds me somewhat of string interning.

Upvotes: 1

baretta
baretta

Reputation: 7585

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

Upvotes: 0

RS Conley
RS Conley

Reputation: 7196

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.

Upvotes: 2

Robert
Robert

Reputation: 6437

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".

Upvotes: 5

Related Questions