Last active
November 6, 2016 07:18
-
-
Save shoheiyokoyama/e3e21b31e8fd09db794b to your computer and use it in GitHub Desktop.
デザインパターン「Singleton」 ref: http://qiita.com/shoheiyokoyama/items/c16fd547a77773c0ccc1
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Cretae Instance | |
instance1 and instance2 are same Instance |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Singleton { | |
private Singleton(){} | |
public static Singleton getInstace() { | |
return SingletonHolder.INSTANCE; | |
} | |
private static class SingletonHolder { | |
private static final Singleton INSTANCE = new Singleton(); | |
} | |
} | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
- (void)destructor | |
{ | |
sharedInstance = nil; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
- (void)destructor | |
{ | |
sharedInstance = nil; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Singleton { | |
static Singleton instance = null; | |
public static synchronized Singleton getInstace() { | |
if (instance == null) { | |
instance = new Singleton(); | |
} | |
return instance; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@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