Objective-C Singleton Class template

The Singleton design pattern is widely spread when developing software with the Cocoa (touch) framgework. A Singleton makes sure, that only one object of your class is allocated.
When using the template a cocoa class file with 2 additions will be generated:
-a static pointer to the singleton object
-a static method to access the singleton object: shared<<CLASSNAME>>
Template Installation
Download the attached archive and extract the included files to the following locations:
Cocoa Template:
“/Developer/Library/Xcode/File Templates/Cocoa Class/Objective-C Singleton class.pbfiletemplate”
Cocoa Touch Template:
“/Developer/Platforms/iPhoneOS.platform/Developer/Library/Xcode/File Templates/Objective-C Singleton class (iPhone).pbfiletemplate”
Usage:
Just choose the template “Objective-C Singleton class” when adding a new file to your project.

Update: template was optimized by ensuring thread safety when allocating the object.

The Singleton design pattern is widely spread when developing software with the Cocoa (touch) framework. A Singleton makes sure, that only one object of your class is allocated.

When using the template, a cocoa class file with 2 additions will be generated:

  • a static pointer to the singleton object
  • a static method to access the singleton object: shared<<CLASSNAME>>

Template Installation

Download the attached archive and extract the included files to the following locations:

  • “/Developer/Library/Xcode/File Templates/Cocoa Class/Objective-C Singleton class.pbfiletemplate”
  • “/Developer/Platforms/iPhoneOS.platform/Developer/Library/Xcode/File Templates/Cocoa Touch Class/Objective-C Singleton class (iPhone).pbfiletemplate”

Files:

Usage:

Just choose the template “Objective-C Singleton class” when adding a new file to your project. To access the singleton object don’t allocate the object yourself. Use the static method “shared…” (e.g: sharedTestManager):

TestManager *myTestManager = [TestManager sharedTestManager];

Images:

Add SingletonAdded Singleton

Share:
  • Digg
  • del.icio.us
  • Facebook
  • Google Bookmarks
  • Blogplay
  • Twitter
Comment are closed.