Below are some code snippets which shows how to implement Singleton pattern.
Java
PHP
Objective-C
Now to make an object of FileWriting interface.
Java
public class FileWriting {
static private FileWriting _instance = null;
protected FileWriting(){
}
static public FileWriting getInstance() {
if(null == _instance) {
_instance = new FileWriting();
}
return _instance;
}
}
FileWriting javaObj = FileWriting.getInstance();
PHP
<?php
class FileWriting{
protected static $_instance;
protected function __construct()
{
}
public static function getInstance()
{
if(!self::$_instance)
{
self::$_instance = new FileWriting();
}
return self::$_instance;
}
}
phpObj = FileWriting::getInstance();
?>
Objective-C
@interface FileWriting : NSObject {
}
-(FileWriting*)getInstance;
@end
@implementation FileWriting
static FileWriting *_instance;
-(FileWriting*)getInstance{
if(_instance==nil){
_instance = [FileWriting alloc];
}
return _instance;
}
@end
Now to make an object of FileWriting interface.
FileWriting *objc_inst = [[FileWriting alloc] getInstance];