|
-
May 19th, 2010, 04:25 AM
#1
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;
}
}
-
May 19th, 2010, 12:18 PM
#2
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.
If you liked my post go ahead and give me an upvote so that my epee.... ahem, reputation will grow.
Yes; I have a blog too - http://the-angry-gorilla.com/
-
May 19th, 2010, 01:16 PM
#3
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.
Last edited by TheGreatCthulhu; May 19th, 2010 at 01:19 PM.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|