views:

37

answers:

2

I'm afraid this is a bit of an abstract question. I'm basically writing an iPhone game which revolves around a Pet, a bit like a tamagotchi for example. I've created a Pet object, which contains all the data relevant to the Pet's status. The rest of the program accesses the Pet's data and performs relevant actions.

The pet is saved to file using encodeWithCoder and initWithCoder methods. So whenever a method in the program needs to access any pet data, it creates a new instance of the pet by loading it from file. If any changes are made, they are done within methods of the Pet class. These methods always end by writing the Pet to file.

So as the program is running, it is constantly writing to file and reading from file, every time a change is made. If I then want to add a new variable to the Pet, say a BOOL variable called showReaction (to determine whether or not to show the Pet's reaction to a certain event say), I have to include this variable in both my encodeWithCoder and initWithCoder methods. This seems a bit cumbersome since these methods are consequently growing larger and larger. And constantly reading and writing to file seems inefficient.

I would have thought a better approach would be to have a global variable which represents the Pet. And that any method in the program can accesses this global variable in order to change it. The only time I write to file is when the player exits the game, and I only read from file once when the game loads.

My question is - I'm not exactly sure of the best approach to this. And whether or not this is good programming practice? Should I declare the global variable in my main ViewController and then refrain from releasing it during the program's running? Will I be able to alter variables in the Pet class and have this data retained during the running of the program. I.e. setting showReaction to TRUE in one method, reading it in another method and setting it to FALSE in another method...

Any advice would be much appreciated. I've been writing this program for a while now, and am wondering if I should make these changes now before I ingrain any bad practices further. But given the size of my code, I don't want to experiment too much without checking with the experts first!

Thanks,

Michael

A: 

I think its a bit difficult to say on an abstract level what is best in your case but often what seems simpler is often the best approach. Having just a global instance which you interact with seems to be a pretty straightforward way.

Anders K.
+1  A: 

Why don't you create a singleton? that way your code won't need to worry about how it is being done. Try to decouple your code.

Mr Shoubs
Ah I see - a singleton eh. I was not previously aware of such a thing. Although having read up a bit on this forum, there seems to be a fair bit of anti-singleton sentiment. I'll look in to it further, but thanks for the advice :)
Smikey
Yes, it is bad to use the singleton pattern too much, maybe your design is wrong, but the problem you are currently facing would be solved by implementing a singleton pattern... it is better than using a global variable and has valid uses, just don't treat them as magic solve-all-ur-problems solution.
Mr Shoubs
Sorry, I should have phrased that - using too many singletons is bad.
Mr Shoubs
Ok, thanks for the update. I would only be using the one, so perhaps it would work. I have a feeling it would be better than constantly reading and writing to file, however at this stage in my code, I wonder if it's worth making such a big change. Well thanks again for the advice :)
Smikey
Well your singleton controls access to an object, if you make that object use an interface, you can return the interface from your singleton and it wouldn't matter how the object was implemented. you could even change what it did at run time if you wanted to.
Mr Shoubs