Reputation: 75794
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
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
Reputation: 7585
Looks like you are trying to keep track of all instances of an object... why?
Upvotes: 0
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
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