I have done some research and still didn't found a solution that seems correct, in a "best practices" way.
I'm working on a App for iPhone using Xamarin. This app will be initial iPhone only, but there are plans to make versions for Android and Windows Phone in the near future.
This App crete/saves a "Moment". This moment have some pictures plus some information. Basically, this moment will be used all over the App, been incremented with more data from lots of views. While I do save this moment to some kind of repository (SQL, filesystem, ..., I still have to implement this), I need it to be alive thru the workflow.
One way of doing it, would be:
var moment = new Moment()
// .. add infos from view to moment
nextView.Moment = moment;
PerformSegue(...);
Is this the right way of doing it? There isn't any pattern that I could use to solved it from all platforms and control better how to pass this infos between the views (samples would be appreciated)?
You might have seen even apple is using the singleton pattern to access the current application instance to open the URL its just an example
UIApplication.SharedApplication.OpenUrl(urlToSend)
So in your case Singleton is right choice make sure you are not putting everything here because its a performance hit.
so use singleton when you have no other ways of doing it.
Here is my sample thread safe code for making the singleton class
using System;
namespace MySingletonPattern
{
sealed class Singleton
{
//To make the thread safe access we need an instance
private static readonly object _lockObj = new object();
//Lazy initialisation for the real instance
private static volatile Singleton _instance;
private Singleton()
{
}
static internal Singleton Instance()
{
if (_instance == null)
{
//To make the access Thread safe
lock(_lockObj)
{
if(_instance == null)
{
//Creating the instance of singleton class and this will be re-used
_instance = new Singleton();
}
}
}
return _instance;
}
}
}
Here is how to use it
Singleton obj1 = Singleton.Instance();
You could use the Singleton pattern to get access to your data in every point of your app.
Check out the Wiki page(with a sample): http://en.wikipedia.org/wiki/Singleton_pattern
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With