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,
- I'll included myclass.cs file in any
project or
- 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
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 --.
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.
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.
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
}
}
}
take an static counter in the class, and throw an exception in your class constructor if count>10
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.