return this instance through interface
Hi,
i want to return the current instance to my interface in the implementation class. Anything wrong with the following? in terms of design or ..?
Code:
public interface IMyInterface
{
IMyInterface GetInstance();
}
public class MyInterface:
{
public static MyInterface Instance = null;
public MyInterface GetInstance()
{
return Instance;
}
public MyInterface()
{
Instance = this;
}
}
Re: return this instance through interface
I don't really understand why you would use any of that code. It looks like an incorrect implementation of the singleton pattern.
Re: return this instance through interface
I agree. Furthermore I don't think that will compile.
Now, I don't know your skill level, so don't get upset if I go to much in detail here.
A few guidelines:
- Learn how to implement interfaces.
- After that, you'll see that there's no reason for your class to have the same name as the interface (although it might have a same or similar one;
expanding on that, "MyInterface" is generally a bad name for a class, since a class is not an interface).
- In terms of design: why do you need this done that way? Singleton pattern?
- If so, you must first understand why and when the Singleton design pattern is used, before implementing it.
- Note that an interface might not be the best choice here - it all depends on what are you trying to do.
- That said, try to understand the usage of interfaces (which, I admit, can be a bit confusing topic).
To sum it up - when you design something, you should get an as clear an idea as possible about what it should do, and then think about how it should function. Once you're sure about the way to go - go, and do the research where necessary.
So, just google on some of these topics.
Hope it helps.