Limit instances creation of a class?

前端 未结 6 1584
暖寄归人
暖寄归人 2021-01-11 16:15

I am using C#. I have created a class which can be included in any c#.net project (desktop or web based), but I want that only 10 objects will be created in that application

相关标签:
6条回答
  • 2021-01-11 16:33

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

    0 讨论(0)
  • 2021-01-11 16:39

    You'll simply need to use the factory pattern with a counter of the number of instances created, after which the factory method will throw an exception/return 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 perfectly well for a single-instance application, but for a multi-instance application, you'll probably want to use a semaphore (an implementation exists in System.Threading), which is intended for precisely this sort of situation (limiting access to resources/objects). It gets around the problem of multiple instances of the class being requested almost simultaneously and the count checks failing.

    0 讨论(0)
  • 2021-01-11 16:47

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

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

    0 讨论(0)
  • 2021-01-11 16:48

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

    0 讨论(0)
  • 2021-01-11 16:49

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

    class YourClass
    {
        static int Count = 0;
    
        public YourClass()
        {
           Count++;
           if(Count > 10)
           {
               //throw exception
           }
        }
    }
    
    0 讨论(0)
  • 2021-01-11 16:54

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

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

    You can also use generics to allow the factory class to be re-used for limiting instances of different classes. Use constraints to require instance to implement IDisposible and have a default constructor. Also provide a non-static 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 problem is you can't overload the assignment operator in C#. So if you do the following:

    
       li = null;
    

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

    0 讨论(0)
提交回复
热议问题