Wednesday 19 March 2014

Singleton Object Creation

Singleton Object Creation


Defn:


In computer programming a singleton variable is a variable that is referred to only once.
Examples of where a variable might only be referenced once is as a dummy argument in a function call, or when its address is assigned to another variable which subsequently accesses its allocated storage.
Singleton variables sometimes occur because a mistake has been made – such as assigning a value to a variable and forgetting to use it later, or mistyping one instance of the variable name. Some compilers and lint-like tools flag occurrences of singleton variables.

Creating Singleton Object File:

In Sample Project we can add the file in Xcode tool.

File --> New --> File --> Choose Objective-C class --> Next --> 

class Name is "singleton" 
subclass is NSObject is Selected.

Next --> Finish.


singleton.h :

 #import <Foundation/Foundation.h>

@interface Singleton : NSObject

//Create the variables or objects.
@property(nonatomic,assign) float ad_height;
@property(nonatomic,assign) BOOL ads_removed,is_ipad;
@property(nonatomic, assign) CGRect banner_land;
@property(nonatomic, assign) CGRect banner_port;
@property(nonatomic,assign) UIInterfaceOrientation last_orient;


@property(nonatomic,assign) NSString *lastsss;

+(Singleton *)singleObj;  //class method


//-(void)Objsin;   //instance method

@end

singleton.m :

 #import "Singleton.h"

@implementation Singleton

@synthesize lastsss;

+(Singleton *)singleObj     //class Method
{
    
    static Singleton * single=nil;
    
    @synchronized(self)
    {
        if(!single)
        {
            single = [[Singleton alloc] init];
        }
    }
    return single;
}


-(void)Objsin      //instance method
{
    lastsss = @"aaa";

}


@end


SomeFile.h :


#import "Singleton.h"

@interface SomeFile : UIViewController
{
    Singleton *sobj;
}


SomeFile.m :

  //public method. Access from any page. Deallocate when app is closed.
-(void) viewwillappear method
{
   //reference to allocate
   sobj = [Singleton singleObj];    

   //Method calling
   [sobj Objsin];

    //Assign the values
    NSString *str= @"bb";
    sobj.lastsss = str;

    sobj.last_orient = [UIApplication sharedApplication].statusBarOrientation;

    sobj.banner_land = _bannerView.frame;   //CGRect(0, 0, 0, 0)
    sobj.banner_port = _bannerView.frame;   //CGRect(0, 0, 0, 0)

    sobj.ad_height = 40.0;

    sobj.is_ipad = (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad);    //YES or NO

    sobj.ads_removed = NO;
}

               (or)

 //private method. Access only this page. Deallocate when page is moved.
-(void) viewwillappear method
{
    //initiate memory allocation
    sobj = [[Singleton alloc]init];

    //Method calling
    [sobj Objsin]; 


    //Assign the values
    NSString *str= @"bb";
    sobj.lastsss = str;

    sobj.last_orient = [UIApplication sharedApplication].statusBarOrientation;

    sobj.banner_land = _bannerView.frame;   //CGRect(0, 0, 0, 0)
    sobj.banner_port = _bannerView.frame;   //CGRect(0, 0, 0, 0)

    sobj.ad_height = 40.0;

    sobj.is_ipad = (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad);    //YES or NO

    sobj.ads_removed = NO;
}




No comments: