I would avoid the Singleton at all costs. Your approach encourages loose-coupling and allows you to increase your testability by mocking out the object (if you converted it from your concrete implementation). It generally adds more code but you should have better testability in your application. That allows you to use an IoC container and utilize Dependency Injection. There are many great articles and posts on the web and here on StackOverflow about such topics.
Perhaps your singleton might look like this:
public class MyViewController : UIViewController
{
public MyViewController ()
{
}
public void Foo()
{
FaceBookSingleton.Instance.DoSomeAction();
FaceBookSingleton.Instance.Something = 4;
}
}
How do you do your testing around Foo()
? How would you know, sooner, if you introduced a breaking change? How do you know what your dependency graph looks like? It's harder to test code like this. Don't count on people browsing your code base to try and figure out if they broke something. Help them out and yourself out by writing code that can be tested.
Now maybe look at something as such:
public interface ISocialMediaWidget
{
ISocialMediaWidgetResponse DoSomeUnitOfWork();
}
public class ConcreteSocialMediaWidgetService
{
protected readonly ISocialMediaWidget socialMediaWidget;
public ConcreteSocialMediaWidgetService(ISocialMediaWidget widget)
{
this.socialMediaWidget = widget;
}
public ISocialMediaWidgetResponse Foo()
{
return socialMediaWidget.DoUnitOfWork();
}
}
Writing tests around the aforementioned is much easier. You can also mock out the response from each interface to create all kinds of tests.
public void SocialMediaWidgetTest()
{ //pseudo code
var testService = new MockObject(ConcreteSocialMediaWidgetService(MockObject_of_ISocialMediaWidget));
Assert.Equals(someImplementation_of_ISocialMediaWidgetResponse, testService.Foo());
}