Skip to content

Instantly share code, notes, and snippets.

@shoheiyokoyama
Last active November 6, 2016 07:18
Show Gist options
  • Save shoheiyokoyama/e3e21b31e8fd09db794b to your computer and use it in GitHub Desktop.
Save shoheiyokoyama/e3e21b31e8fd09db794b to your computer and use it in GitHub Desktop.
デザインパターン「Singleton」 ref: http://qiita.com/shoheiyokoyama/items/c16fd547a77773c0ccc1
Cretae Instance
instance1 and instance2 are same Instance
class Singleton {
private Singleton(){}
public static Singleton getInstace() {
return SingletonHolder.INSTANCE;
}
private static class SingletonHolder {
private static final Singleton INSTANCE = new Singleton();
}
}
- (void)destructor
{
sharedInstance = nil;
}
- (void)destructor
{
sharedInstance = nil;
}
public static void main(String[] args) {
Singleton instance1 = Singleton.getInstance();
Singleton instance2 = Singleton.getInstance();
if (instance1 == instance2) {
System.out.println("instance1 and instance2 are same Instance");
} else {
System.out.println("instance1 and instance2 aren't same Instance");
}
}
class Singleton {
static Singleton instance = null;
public static synchronized Singleton getInstace() {
if (instance == null) {
instance = new Singleton();
}
return instance;
}
}
@implementation Singleton
static Singleton *sharedInstance = nil;
+ (Singleton *)getInstace
{
@synchronized(self){
if (!sharedInstance) {
sharedInstance = [Singleton new];
}
}
return sharedInstance;
}
- (instancetype)init
{
self = [super init];
if (self) {
//初期化処理
}
return self;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment