I have a noob question regarding how to architect my iPhone app.
I have two main components, a map and an audio player, both self contained classes (MapController and AudioController). I read this great article and am trying to apply it to my app:
http://blog.shinetech.com/2011/06/14/delegation-notification-and-observation/
I have a third class, WebServices, that handles uploading POST data to my server as well as making queries to external API’s.
My question:
- Do I import header files, and create a new instance of WebServices in both the map controller, and the audio player? And then each controller can reference it’s own WebServices for queries?
- Or, should I create one WebServices instance on the RootController, and then pass this to the map and audio controllers on init?
In particular, I’m interested in which approach consumes memory more efficiently. Or if it doesn’t matter at all.
Thanks!
Point Zero
Do not resort to singletons, and especially not in multithreaded contexts. You can search this site if you need explanations (hint: the reasons against them are quite similar in OO languages similar to ObjC. e.g. C++, Java, C#…).
To share, or not?
It ultimately depends on the behavior your design needs, but I favor Option #1 as the default.
WebServicesmay be huge). Smaller objects allow more flexibility and reduce the chances that your program will compete for resources.Assume multiple instances (1) until have identified a problem. Even after the problem is identified, you can usually avoid sharing.
To determine which consumes less memory requires much more information. If the objects you are considering sharing are not huge (very unlikely), they themselves are not problems wrt memory consumption.
Either approach proposed can result in more memory consumption – it depends entirely on the context and execution. We’ll need some more information to answer this specifically.
This is not to imply that sharing is bad, but sharing usually complicates things because you need to design objects to behave predictably when shared (especially since you are operating in a multithreaded context).
Ask yourself:
WebServicesprovide?WebServicesimprove your program and reduce complexity?It’s likely the case that the
WebServicesmap stuff does not interact with the audio stuff. In that case,WebServicesmay be a good candidate for a base class, where the audio and map services are separate subclasses (assuming theWebServicesclass is already complex).