[ACCEPTED]-Limit instances creation of a class?-instance

Accepted answer
Score: 20

Keep a static variable with the number of 7 instances created. Increment that number 6 with each construction of the object. Make 5 the object IDisposable and decrement that 4 number on each call to Dispose(). If you 3 want it to be thread-safe, use Interlocked.Increment() and 2 Interlocked.Decrement() to change the value 1 of this variable instead of ++ and --.

Score: 12

I believe that you want some form of the 11 multiton pattern.

The multiton pattern is a variation on 10 the singleton pattern, but that allows for 9 n instances of an object. Much like how 8 the singleton class has a static variable 7 to hold the single instance, the multiton 6 is often implemented with a static array 5 or map of instances, depending on how you 4 want to access the instances - arrays only 3 allow for numerical access, but by using 2 a map, you could provide String keys to 1 your instances, making them named.

Score: 9

You'll simply need to use the factory pattern with a counter 12 of the number of instances created, after 11 which the factory method will throw an exception/return 10 null.

Example:

public class Foobar
{
    private static int numInstances = 0;

    public static Foobar CreateFoobar()
    {
        if (numInstances++ < 10)
        {
            return new Foobar();
        }

        return null;
    }

    protected Foobar()
    {
        ...
    }
}

The above method will work 9 perfectly well for a single-instance application, but 8 for a multi-instance application, you'll 7 probably want to use a semaphore (an implementation 6 exists in System.Threading), which is intended for precisely 5 this sort of situation (limiting access 4 to resources/objects). It gets around the 3 problem of multiple instances of the class 2 being requested almost simultaneously and 1 the count checks failing.

Score: 0

I would create a static integer and update it 1 when you instantiate a new object.

class YourClass
{
    static int Count = 0;

    public YourClass()
    {
       Count++;
       if(Count > 10)
       {
           //throw exception
       }
    }
}
Score: 0

take an static counter in the class, and 2 throw an exception in your class constructor 1 if count>10

Score: 0

For disposing of instance also create a 19 static unload method (similar to AppDomain). Have 18 the unload method call implementation of 17 IDisposable which decrements counter using 16 Interlocked.Decrement and also dispose of 15 the instance.

(I'm assuming if your limiting 14 the number of instances you have resources 13 in the instance to manage.)

You can also 12 use generics to allow the factory class 11 to be re-used for limiting instances of 10 different classes. Use constraints to require 9 instance to implement IDisposible and have 8 a default constructor. Also provide a non-static 7 property to return the actual instance.


public class foo : IDisposable 
   {
   public foo() { ; }
   public string Name;

   public void Dispose()  { ; } 
   // Real class would free up instance resources
   }

  LimitedInstance< foo > li = LimitedInstance< foo >.CreateInstance();

  li.Instance.Name = "Friendly Name for instance";
  // do stuff with li

  LimitedInstance< foo >.UnloadInstance( ref li );

Only 6 problem is you can't overload the assignment 5 operator in C#. So if you do the following:


   li = null;

Instead 4 of calling the unload method then the instance 3 will remain on the heap, and your counter 2 to number of instances wont be decremented, until 1 GC occurs.

More Related questions