How you can limit a class to create only two(n) instances ?
This is one of my favorite question in an interview.So before watching the answer take a minute and think how you can create a such class .. tough ? Ahan its very easy.
Answer : The basic idea is create a static property and increment that in constructor every time you are creating an object of that class. And throw an exception when that count is greater than two.
How you will decrement the count of that property when object is disposed ? Is it thread safe ?
Below is the running example :
Hope my this series will help somebody to get a job 🙂 🙂 . Happy coding 🙂
via Blogger http://ift.tt/2jJfEBc January 21, 2017 at 12:04PM