Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

mpramodjain

macrumors regular
Original poster
Nov 20, 2008
152
0
Banglore
Hi,

A programming basic quest :

When to go with instance methods and singleton classes(I mean taking a static object of the class and use it all over the application).


To be clear, I have some methods which have the generic functionality (getting substring , height of the string, validating string is numeric or not... etc).

Now I can do in both the ways, but stuck on which way to go. I would prefer to go with singleton class object, because AFAIK it would allow me to access non-static variables too.

Please let me know which is the best way to go (other than the usage of non-static variable scenario).. memory and code size , performance issues.. or experience you had in reference to better code.

Also in reference to OBJ-C.

Thanks,
Pramod Jain
 

Soulstorm

macrumors 68000
Feb 1, 2005
1,887
1
Hi,

A programming basic quest :

When to go with instance methods and singleton classes(I mean taking a static object of the class and use it all over the application).


To be clear, I have some methods which have the generic functionality (getting substring , height of the string, validating string is numeric or not... etc).

Now I can do in both the ways, but stuck on which way to go. I would prefer to go with singleton class object, because AFAIK it would allow me to access non-static variables too.

Please let me know which is the best way to go (other than the usage of non-static variable scenario).. memory and code size , performance issues.. or experience you had in reference to better code.

Also in reference to OBJ-C.

Thanks,
Pramod Jain

I think the best method for actions like getting a substring and returning the height of a string (what is that, exactly?) is best implemented as a category extension to the NSString and NSMutableString classes.

Performance between using a singleton and using categories should be the same, and memory allocation and deallocation in singletons works exactly as with normal classes. Note, however, that a singleton's allocated memory is deallocated during the termination of the application. So be careful of what data you store in it.

Singletons are used in cases where we need to have specific Application-Wide methods and variables. So, optimally, each application in general has one or two singletons at most. Use them with great care because they can easily lead to spaghetti code. I use them as containers for references and convenience methods (getting the applications delegate's ManagedObjectContext, saving loading, hold pointers to important classes, hold flags about the state of the program).

In general, I tend to avoid implementing functions like the ones you describe in them.
 

mpramodjain

macrumors regular
Original poster
Nov 20, 2008
152
0
Banglore
Hi,

Thanks for the quick reply.

In general, I tend to avoid implementing functions like the ones you describe in them.

If we avoid implementing functions, how can I achieve the code reusability. Also I have to use the same functionality in different .m files of the application.

If I go with the category, for just one or two method I would have to go with different categories (for eg. nsstring , uiimage, etc ) for different features.

So it won't be good AFAIK, not be true with my little knowledge over this.

Thanks
Pramod Jain.
 

dejo

Moderator emeritus
Sep 2, 2004
15,982
452
The Centennial State
If you have a method that does the same thing no matter what class of object you're dealing with then, yeah, it wouldn't seem to make sense to have it duplicated in multiple categories for the various classes. But I would doubt that you would have that happening for, say, NSString and UIImage. If you do, perhaps you could post those methods here or, at least, explain what they do.

In the end, there is not always a right and wrong way to code things. Frequently, there are many paths to the same solution. Just go with what you feel comfortable with / makes the most sense in your development environment. I would say don't be overly concerned with memory footprint and performance until they become obstacles; often you will find you were worried for nothing (unless you are writing apps where those things are critical, then a different tact is required).
 

PhoneyDeveloper

macrumors 68040
Sep 2, 2008
3,114
93
A category is a kind of utility function. In languages that don't support adding new methods to classes at runtime these kinds of things would be simple functions with C bindings. That's one way that I tend to do these things.

For instance I have an isIpad() function and a MMDocumentsFolder() function that returns the fullpath to the place where my app stores its documents. While these functions live in .m files in the header they are simply C language functions. (Hmmm, I guess NSString isn't a C language type but it could just as easily return a const char *).

Code:
// Utilities.h
NSString* MMDocumentsFolder(void);
BOOL isIpad();
My point is that these kinds of global functions don't have to be in a class. Apple has a bunch of utility functions that also aren't in a class.

I also sometimes create Utility classes that only have class methods. Apple tends not to do it that way. These classes tend not to have any state, like ivars, and all the methods have a clear relationship to each other.
 

mpramodjain

macrumors regular
Original poster
Nov 20, 2008
152
0
Banglore
Thanks for all replies..

So would it be good to go with a singleton class for generic methods or do I need make the instance methods..

What can be concluded.... Both performance wise and oops concept wise.
 

PhoneyDeveloper

macrumors 68040
Sep 2, 2008
3,114
93
What can be concluded is that there are different opinions and it depends on the particulars. You have to decide.
 

robbieduncan

Moderator emeritus
Jul 24, 2002
25,611
893
Harrogate
Thanks for all replies..

So would it be good to go with a singleton class for generic methods or do I need make the instance methods..

What can be concluded.... Both performance wise and oops concept wise.

If these "generic methods" could be put in a singleton class then they can't need access to internal object state from the objects they operate on. If that is the case I'd make them plain C functions rather than instance methods or methods on a singleton: it's faster to call a C function than send a message to an object...
 

(marc)

macrumors 6502a
Sep 15, 2010
724
2
the woods
I wanted to open a thread about singletons for some time but now that there's one already, I'll just post here. In my apps I'm often dealing with a set of data (read from disk) that I load from within a singleton. That way, whenever I need the data I just use the singleton without having to worry about allocating the data twice or passing around pointers through my app to prevent the creation of multiple different data sets. Is that a good use of singletons?
 

robbieduncan

Moderator emeritus
Jul 24, 2002
25,611
893
Harrogate
I wanted to open a thread about singletons for some time but now that there's one already, I'll just post here. In my apps I'm often dealing with a set of data (read from disk) that I load from within a singleton. That way, whenever I need the data I just use the singleton without having to worry about allocating the data twice or passing around pointers through my app to prevent the creation of multiple different data sets. Is that a good use of singletons?

I think so, yes. If you have a single set of data then it makes sense to represent it as such in your model layer.
 

mpramodjain

macrumors regular
Original poster
Nov 20, 2008
152
0
Banglore
I think so, yes. If you have a single set of data then it makes sense to represent it as such in your model layer.

So we can go with creating singleton classes for HttpNetworkConnection wrapper and XmlParser wrapper as I would like to have only one asynchronous thread to be run at a time for whole application
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.