views:

74

answers:

2

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?

+6  A: 

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;
}
Josh Einstein
+1 For DI - the instance scoping offered by DI containers can offer much more flexibility that simply using the singleton pattern.
David Hall
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
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 Einstein
I updated my answer to show hypothetical form_load events.
Josh Einstein
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
bingo, got it, thanks for following up!
baron
Yes that is correct.
Josh Einstein
@Josh: I had this same question. You saved me a ton of confusion, thanks for the clear answer and the wiki link! +1
Otaku
+1  A: 

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();
Michael Shimmins
Don't you need private static C instace; ?
Pim Jager
I was just about to ask, shouldn't line 3 read: `private static C instance;`
baron
Editted - quite right.
Michael Shimmins