4

Say I have two separate classes, A and B.

I also have Repository class C which loads some information from a textfile. E.g. It has methods loadLines(), addLine(), deleteLine().

Ignoring databinding, how can I make A and B both work on the same class C. Is it possible?

For example at the moment, in class A and B formload, I have:

var classC = new C();

This causes repeated execution. It would be much better if I could have one copy of class c to work on from either A or B.

edit: so with using the singleton, when does class C's constructor execute? - whichever class first creates it? and it only happens the once I take it?

edit1: does using the singleton pattern imply that you should only have one of them in your project solution? could I have multiple?

2
  • In my example, an instance of C is created the first type the type is used in any way. It's much less explicit. For all intents and purposes you can think of it as being instantiated the first time it is used. In shimm's example, creation is much more explicit and you can see it's created the first time the GetInstance method is called.
    – Josh
    Mar 2, 2010 at 2:56
  • Does having a singleton also imply that you should only have one of them in your project solution? could I have multiple? as in a FileIO and a ClassAController, ClassBController...?
    – baron
    Mar 2, 2010 at 3:18

2 Answers 2

6

You can use the Singleton pattern or if you want to go the route of a Dependency Injection framework (which is about much more than just creating singletons but well worth looking into) most DI containers have the ability to create only a single instance of an object.

class C {

    public static readonly C Instance = new C();

    private C() {
    }

}

Then you would use it like:

private void A_Load(object sender, EventArgs e) {
    var classC = C.Instance;
}

private void B_Load(object sender, EventArgs e) {
    var classC = C.Instance;
}
5
  • 1
    +1 For DI - the instance scoping offered by DI containers can offer much more flexibility that simply using the singleton pattern.
    – David Hall
    Mar 2, 2010 at 2:37
  • Can you show me more code how to "create/link" it from the other classes? Say we are in class B wouldn't it be like: var cSingleton = new C(C.Instance); ? from your e.g. maybe it looks like I should say var b = new B(C.Instance); why am I creating B inside itself? think i'm missing something...
    – baron
    Mar 2, 2010 at 3:43
  • The last two lines are just examples of how you'd reference the one and only instance of C. You don't have to pass them to constructors. As for new C(...) you won't be doing that and in fact in my example the compiler won't let you since the constructor is private.
    – Josh
    Mar 2, 2010 at 3:56
  • so how do I create use it? I can't reference like you did because ClassA constructor will have 0 parameters.... I don't understand how to use the one and only instance. Do I just go like var cSingleton = C.Instance;
    – baron
    Mar 2, 2010 at 3:59
  • @Josh: I had this same question. You saved me a ton of confusion, thanks for the clear answer and the wiki link! +1
    – Todd Main
    Apr 28, 2010 at 16:53
1

If you want to ensure you only ever have one instance of class C running around, you can use the singleton pattern.

By making the constructor on class C private, and having a public static method that takes care of giving you the right instance of C, you can ensure that A and B will always get the same instance:

public class C
{
    private static C instance;

    private C()
    {
        // do some stuff
    }

    public static C GetInstance()
    {
        if(instance==null)
           instance = new C();

        return instance;
    }
}

In A and B you can do:

C theSameInstanceOfC = C.GetInstance();
2
  • 1
    Don't you need private static C instace; ?
    – Pim Jager
    Mar 2, 2010 at 2:41
  • I was just about to ask, shouldn't line 3 read: private static C instance;
    – baron
    Mar 2, 2010 at 2:46

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.