CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3
  1. #1
    Join Date
    Jul 2002
    Posts
    788

    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;
            }
    }

  2. #2
    Join Date
    Jun 2008
    Posts
    2,477

    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/

  3. #3
    Join Date
    Jan 2010
    Posts
    1,133

    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
  •  





Click Here to Expand Forum to Full Width

Featured