Objective C Singleton : An Efficient Design Pattern

1402433427_tmp_shutterstock_151461563Objective C was designed in the 1980s by two independent developers. Their goal was to improve the functionality of the C language. They wanted to add Small Talk capabilities to C, which would make C code reusable and object oriented. The result was the earliest version of Objective C. Today, Objective C is a very popular programming language, especially within the Apple community. It is the language that is used to make Apple operating systems: OS X and iOS. Apple’s Cocoa framework also runs on Objective C, which makes known the basics of Objective C necessary if you want to build apps for iOS users.

Objective C is backwards compatible with C- programs written in C can be used in Objective C without little to no modifications. The syntax of the language is simple and easy to remember. If you have any experience with programming, you will find it easy to learn Objective C. It is an object oriented language, and follows the same principles as other object oriented languages. If you have learned C before, you won’t have a problem with Objective C. Take this beginners course on Objective C to get started.

In this tutorial, we’re going to take a look a the Singleton design patterns or Singleton classes in Objective C. You need to be familiar with the basics of Objective C to understand this tutorial.

Singleton Patterns in Objective C

The Singleton pattern is a design pattern found in Objective C. What are design patterns? A design pattern is a pattern implemented to get an application to work more efficiently or as a solution to an existing problem. The Objective C Singleton pattern lets you enforce only one instance for a given class. This class can be accessed globally through a global access point. The first time this class is accessed, a new instance of the class will be created. For example, if you are building an application and declare a singleton class, the same instance of the class will be accessible by all the elements in your application. A new instance of the class will never be created. This helps you save on system memory and space. It also prevents redundant code. Learn more about singleton patterns in Objective C with this course.

The Singleton pattern is very popular with Apple’s iOS developers. An example of the Singleton class is the UI application class. The sharedApplication method found in this class returns the same UI application instance every time it is called. The singleton (found in the Cocoa library) looks like this:

UIApplication *applicationSingleton = [UIApplication sharedApplication];

The UIApplication instance, if you didn’t know, shows the application that’s currently running on your iOS. The sharedApplication method can be called from anywhere – it is truly a shared method.

While some consider it bad programming practice to use the Singleton pattern, it can be useful if used in the right circumstances. For example, your app will probably have a single log file and you won’t need several instances of the Logger class in your app. You can use the Singleton pattern here. For more information on design patterns and the when you should use them, we recommend that you can take this C++ course that talks about them in detail. Note that the basic OOPs concepts are similar in both Objective C and C++.

Creating the Singleton Pattern in Objective C

Some modern languages, like Ruby, have singleton patterns included in their standard libraries. In Objective C, however, you have to declare your own Singleton class, including its methods and its limitations.

Creating and implementing a singleton pattern is an advanced concept. You need to be familiar with the basics of Objective C before you try it. We’ll create a simple singleton pattern and attempt to explain it to you.

Let’s try to create a Singleton pattern that will allow a TV tuner card that has to be shared. First, let’s create an Objective C project and call it “TVAccess” (you can call it whatever you want to call it. We have to create this Singleton pattern keeping in mind that, when called, it should let you get the current channel (whatever is playing) and allow you to change it (set a new channel). For that, you have to create a reference to the singleton, which can be called later.

Take a look at the program below:

@interface TVAccess : NSObject {
NSDecimalNumber *currentChannel;
+ (TVAccess *)sharedTVAccess;
- (void)setChannel:(NSDecimalNumber *)channel;
- (NSDecimalNumber *)currentChannel;

We have assumed here that our computer contains a single TV tuner card that has to be shared by different processes. We created a method called +sharedTVAccess which allows you to reference the TVAccess class later. The –currentChannel method will return the channel number (in decimal numbers) to you when called. The –setChannel method will let you set a channel number (must be specified in decimals). To learn more about Cocoa naming conventions, take this Objective C course.

This was a simple example of the Singleton class. Remember, you should only create a Singleton pattern when you’re sure that you are only ever going to need one instance of the class. The Logger class is a good example of this concept.

Instantiating a Singleton Pattern in Two Ways

In Objective C, there are two ways to instantiate a Singleton pattern. You can instantiate it before using the Singleton pattern, or it can be instantiated automatically after declaring it. In the example above, we instantiated the Singleton pattern after declaring it.

Instantiating a Singleton pattern after you declare it, it may slow your application down. The time to load a Singleton pattern (if it’s large) is the same whether you declare it before or after. However, it might be better to instantiate it before- it’s better than your app slowing down while it loads the pattern.

The only way to better understand the Singleton design pattern is to try writing one yourself. If you’re game, try writing your own iOS app to see how well you can do. If you get stuck, this course can help you write your own iOS app with Objective C.