Register FAQ / Rules Forum Spy Search Today's Posts Mark Forums Read
Go Back   MacRumors Forums > Apple Systems and Services > Programming > iPhone/iPad Programming

Reply
 
Thread Tools Search this Thread Display Modes
Old Jan 7, 2011, 01:27 AM   #1
mpramodjain
macrumors regular
 
Join Date: Nov 2008
Location: Banglore
Instance methods or singleton class

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
mpramodjain is offline   0 Reply With Quote
Old Jan 7, 2011, 02:22 AM   #2
Soulstorm
macrumors 68000
 
Soulstorm's Avatar
 
Join Date: Feb 2005
Quote:
Originally Posted by mpramodjain View Post
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.
Soulstorm is offline   0 Reply With Quote
Old Jan 7, 2011, 03:21 AM   #3
mpramodjain
Thread Starter
macrumors regular
 
Join Date: Nov 2008
Location: Banglore
Hi,

Thanks for the quick reply.

Quote:
Originally Posted by Soulstorm View Post

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.
mpramodjain is offline   0 Reply With Quote
Old Jan 7, 2011, 09:16 AM   #4
dejo
Moderator
 
dejo's Avatar
 
Join Date: Sep 2004
Location: 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).
__________________
dejo is offline   0 Reply With Quote
Old Jan 7, 2011, 10:18 AM   #5
PhoneyDeveloper
macrumors 68030
 
PhoneyDeveloper's Avatar
 
Join Date: Sep 2008
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.
PhoneyDeveloper is offline   0 Reply With Quote
Old Jan 7, 2011, 10:50 AM   #6
mpramodjain
Thread Starter
macrumors regular
 
Join Date: Nov 2008
Location: 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.
mpramodjain is offline   0 Reply With Quote
Old Jan 7, 2011, 11:45 AM   #7
PhoneyDeveloper
macrumors 68030
 
PhoneyDeveloper's Avatar
 
Join Date: Sep 2008
What can be concluded is that there are different opinions and it depends on the particulars. You have to decide.
PhoneyDeveloper is offline   0 Reply With Quote
Old Jan 7, 2011, 11:51 AM   #8
robbieduncan
Moderator
 
robbieduncan's Avatar
 
Join Date: Jul 2002
Location: London
Quote:
Originally Posted by mpramodjain View Post
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...
robbieduncan is offline   0 Reply With Quote
Old Jan 8, 2011, 09:18 AM   #9
(marc)
macrumors 6502a
 
(marc)'s Avatar
 
Join Date: Sep 2010
Location: 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?
(marc) is offline   0 Reply With Quote
Old Jan 8, 2011, 09:21 AM   #10
robbieduncan
Moderator
 
robbieduncan's Avatar
 
Join Date: Jul 2002
Location: London
Quote:
Originally Posted by (marc) View Post
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.
robbieduncan is offline   0 Reply With Quote
Old Jan 11, 2011, 03:37 AM   #11
mpramodjain
Thread Starter
macrumors regular
 
Join Date: Nov 2008
Location: Banglore
Quote:
Originally Posted by robbieduncan View Post
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
mpramodjain is offline   0 Reply With Quote

Reply
MacRumors Forums > Apple Systems and Services > Programming > iPhone/iPad Programming

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Similar Threads
thread Thread Starter Forum Replies Last Post
Assigning new instance of a class to an object that had an instance of the class moonman239 iPhone/iPad Programming 3 Oct 11, 2013 12:58 PM
How to have user create class instance Abrexas iPhone/iPad Programming 21 Mar 28, 2013 12:54 PM
Class methods vs instance methods zijianz iPhone/iPad Programming 8 Jul 20, 2012 11:15 AM
Help With Bug: -[NSConcreteValue class]: message sent to deallocated instance ArtOfWarfare Mac Programming 5 Jul 11, 2012 11:13 PM
Private class methods KarlJay iPhone/iPad Programming 9 Jun 12, 2012 07:29 AM

Forum Jump

All times are GMT -5. The time now is 11:24 AM.

Mac Rumors | Mac | iPhone | iPhone Game Reviews | iPhone Apps

Mobile Version | Fixed | Fluid | Fluid HD
Copyright 2002-2013, MacRumors.com, LLC