I need to complete the piece of code to write a method that takes a factory method that returns an object and determines if the object is a singleton instance.This is for an exercise of a course i am taking about design patterns. All the previous exercises were about implementing the pattern but this one is about testing and i dont know how to test this. I tried to create another instance and check if the hashcodes are the same but i dont know how to create another instance with that function.
public class SingletonTester
{
public static bool IsSingleton(Func<object> func)
{
// todo
}
}
ANSWER
.Net v1
public class SingletonTester
{
private static int instanceId = 0;
public static bool IsSingleton(Func<object> func)
{
var response = false;
if(instanceId != func.GetHashCode()){
response = true;
instanceId = func.GetHashCode();
}
return response;
}
}