views:

691

answers:

6

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

+8  A: 

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
Yes, you can say that, but I want it to limit till 10 instances which may vary till 15 or 20 etc....
Prashant
It's still possible with the multiton. Rather than using an array, use a resizable container.
Thomas Owens
And I don't know why the downvote - this definately answers your question. Using multiton, you can limit your number of objects to any n, as long as you know your n at compile time. Although you might be able to figure out a way to modify the multiton to not know its n until run time.
Thomas Owens
So maintaining a static variable in a class is a "pattern" these days eh?
Ed Swangren
+14  A: 

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
Using destructors is more failproof.
Dykam
This is almost the same as a variation of the Singleton pattern that allows a certain number of instances to be created.
Diaa Sami
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
Can you please provide me a sample code in C#?? I can better understand then.
Prashant
+6  A: 

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
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
@Thomas Owens: If that's a problem for the OP, he can always use the abstract factory pattern. :)
Noldorin
When and how is "numInstances" decreased?
Seventh Element
A: 

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
It isn't thread-safe and you are incrementing Count twice on each initialization.
Yuriy Faktorovich
Yes it isn't thread safe but it's a simple example that gets the point across.And I fixed the double increments, thanks :D
bufferz
Also you are never decrementing Count
Yuriy Faktorovich
The spec doesn't say anything about destroying the objects... :-)
Roger Lipscombe
A: 

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

ArsenMkrt
A: 

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.

Thomas Hamilton