Singletons: Pros,Cons, Design Concerns

Discussion in 'Mac Programming' started by Soulstorm, Jul 24, 2011.

  1. Soulstorm, Jul 24, 2011
    Last edited: Jul 24, 2011

    Soulstorm macrumors 68000

    Soulstorm

    Joined:
    Feb 1, 2005
    #1
    I admit it. I am using singletons. I know what you may all say, and frankly, seeing all these answers on the Internet, saying about the bad aspects of singletons, and advising against them really made me question my programming practices.

    I feel I must really clarify some things here and ask fore directions.

    So lets consider some cases where I use singletons a lot:

    1) To create accessors to global variables, like my root view controller, specific and always existing view controllers, application state, my global managed object context... stuff like that
    2) To create utility classes whose job is to handle data application-wide. For example, I create a Singleton that will operate my caching database, which relies on Core Data. Since I need to create caches and other stuff to be put in the database in different views, it somehow felt better to create a class that will handle database ins/outs (being careful about thread safety).
    3) Handling network sessions. Actually, I use it to keep alive a connection and sending something like a PINg to a server each XX seconds.

    I think that about sums it up. I would really like opinions from other developers on the matter. You think that there are better solutions for these problems above? Do you think that there are always better alternatives to singletons and that they should be avoided?

    Any recommendations and thoughts would be useful, and most welcome.
     
  2. ehoui macrumors regular

    Joined:
    Jan 27, 2011
    #2
    Why be concerned over a few singletons -- even if there are more robust/elegant patterns, if it works for you and is maintainable, then go forth and dance, Mr. Bo-singleton.
     

Share This Page