Guest User

Untitled

a guest
Jul 19th, 2018
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.17 KB | None | 0 0
  1. @interface MySingleton : NSObject
  2. {
  3. SomeClass* _someClass;
  4. }
  5.  
  6. @property (class, readonly, strong) Class_B* shared;
  7.  
  8. @end
  9.  
  10. @protocol SomeProtocol
  11. @end
  12.  
  13. @interface SomeClass : NSObject <SomeProtocol>
  14. @end
  15.  
  16. @interface MySingleton : NSObject
  17. {
  18. id<SomeProtocol> _someClass;
  19. }
  20. @property (class, readonly, strong)MySingleton* shared;
  21.  
  22. - (instancetype)initWithSomeClass:(id<SomeProtocol>)someClass;
  23. @end
  24.  
  25. @implementation MySingleton
  26.  
  27. + (MySingleton*)shared {
  28.  
  29. static MySingleton* mySingleton;
  30. static dispatch_once_t predicate;
  31.  
  32. dispatch_once(&predicate, ^{
  33. SomeClass* someClass = [[SomeClass alloc] init];
  34. mySingleton = [[MySingleton alloc] initWithSomeClass:someClass];
  35. });
  36.  
  37. return mySingleton;
  38. }
  39.  
  40. - (instancetype)initWithSomeClass:(id<SomeProtocol>)someClass {
  41. self = [super init];
  42. if (self) {
  43. _someClass = someClass;
  44. }
  45. return self;
  46. }
  47. @end
  48.  
  49. @interface someClassFake : NSObject <SomeProtocol>
  50. @end
  51.  
  52. SomeClassFake* someFakeClass = [[SomeClassFake alloc] init];
  53. MySingletone* mySingleton = [[MySingleton alloc] initWithObject: someClassFake];
  54.  
  55. @interface MySingleton : NSObject
  56. {
  57. id<SomeProtocol> _someClass;
  58. }
  59. @property (class, readonly, strong)MySingleton* shared;
  60.  
  61. @end
  62.  
  63. @implementation MySingleton
  64.  
  65. + (MySingleton*)shared {
  66.  
  67. static MySingleton* mySingleton;
  68. static dispatch_once_t predicate;
  69.  
  70. dispatch_once(&predicate, ^{
  71. mySingleton = [[MySingleton alloc] init];
  72. });
  73.  
  74. return mySingleton;
  75. }
  76.  
  77. - (void)setSomeClass:(id<SomeProtocol>)someClass {
  78. _someClass = someClass;
  79. }
  80.  
  81. @end
  82.  
  83. SomeClass* someClass = [[SomeClass alloc] init];
  84. [MySingleton.shared setSomeClass:someFakeClass];
  85.  
  86. SomeClassFake* someFakeClass = [[SomeClassFake alloc] init];
  87. [MySingleton.shared setSomeClass:someFakeClass];
  88.  
  89. @interface MySingleton (Shared)
  90. @property (class, readonly, strong)MySingleton* shared;
  91. @end
  92.  
  93. @implementation MySingleton (Shared)
  94.  
  95. + (MySingleton*)shared {
  96.  
  97. static MySingleton* mySingleton;
  98. static dispatch_once_t predicate;
  99.  
  100. dispatch_once(&predicate, ^{
  101. SomeClass* someClass = [[SomeClass alloc] init];
  102. mySingleton = [[MySingleton alloc] initWithSomeClass:someClass];
  103. });
  104.  
  105. return mySingleton;
  106. }
  107. @end
Add Comment
Please, Sign In to add comment