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?
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.
Then you would use it like: