13

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 of my class. If object instances created more than 10 then it should give an error or simple will not work.

There can be two situations,

  1. I'll included myclass.cs file in any project or
  2. I'll bundle my class in a DLL and then include it in any application

In both situations it must through error if more than 10 instances of my class is created in the application.

This question was asked by my teacher, he told me to search for the answer on internet, I tried but no where found any solution for this problem, I haven't heard that we can limit objects?

Is it possible, if yes then how?

Thanks

Roman Starkov
  • 59,298
  • 38
  • 251
  • 324
djmzfKnm
  • 26,679
  • 70
  • 166
  • 227

6 Answers6

20

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

Dave Markle
  • 95,573
  • 20
  • 147
  • 170
  • 1
    Using destructors is more failproof. – Dykam Aug 09 '09 at 14:09
  • This is almost the same as a variation of the Singleton pattern that allows a certain number of instances to be created. – Diaa Sami Aug 09 '09 at 14:10
  • 7
    Dykam: This is C#. C# does not have destructors, it has nondeterministic finalizers. If you decrement the count in the finalizer, you could be setting yourself up for cases when your classes have all fallen out of scope, but have not yet been garbage-collected, and you can't create any more objects. – Dave Markle Aug 09 '09 at 14:40
  • Can you please provide me a sample code in C#?? I can better understand then. – djmzfKnm Aug 22 '09 at 19:07
  • 1
    @Dave, C# does have destructor. – koo9 Dec 19 '14 at 16:25
12

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.

Thomas Owens
  • 114,398
  • 98
  • 311
  • 431
9

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.

Noldorin
  • 144,213
  • 56
  • 264
  • 302
  • The only problem with putting this in the Factory is that the Factory becomes less reusable as it is tied to a specific implementation of an application because it knows how many instances that application needs. – Thomas Owens Aug 09 '09 at 14:14
  • @Thomas Owens: If that's a problem for the OP, he can always use the abstract factory pattern. :) – Noldorin Aug 09 '09 at 14:31
  • 2
    When and how is "numInstances" decreased? – Captain Sensible Apr 24 '10 at 19:03
0

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
       }
    }
}
bufferz
  • 3,400
  • 5
  • 24
  • 37
0

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

Arsen Mkrtchyan
  • 49,896
  • 32
  • 148
  • 184
0

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.