13. 典型的な実装 + (id)sharedInstance { static HogeManager *instance = nil; static dispatch_once_t onceToken; dispatch_once(&onceToken, ^{ instance = [[self alloc] init]; }); return instance; } このクラスメソッドでのみアクセスさせる
昨日(6/1)、はてな京都オフィスで開催されたCocoa勉強会関西で「ワタシは Singleton がキライだ」というタイトルで発表してきた。内容について興味のある方はスライドを SlideShare に載せたのでそちらを見てください。 (2013/6/8 追記: この発表で使ったサンプルとだいたい同じものを GitHub で公開しました。CoreDataBooks ではなく、Core Data アプリのテンプレートをベースにしたものになっています) 時間はだいたい30分くらいが基本なんだけど、今回はちょっと気合いを入れて45分枠をもらった。 今回発表したようなテーマには、個人的には10年近く前に Java プログラマをやっていた頃から取り組んでいて、当時からはてなダイアリーに書いたりしてた(この記事とかこの記事とか)。Java 方面では Spring やら Google Guice とい
Cocoaでユーティリティ的に使うクラスの定番パターンで、singletonパターンがあります。NSNotificationCenterがいい例で、アプリケーションワイドで使う機能をクラスにする場合に便利なパターン。こんな感じ。 [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(sayHello:) name:HelloWorldNotification object:nil]; これに従って、自前で作るシングルトンクラスもなんちゃらセンターと言う名前で作りがちです。僕の場合、 SupportCenter ActivityCenter DownloadCenter などなど。それぞれのクラスにクラスメソッドとしてsharedCenterというメソッドを定義して使ってます。こんな感じ。 [[Su
リリース、障害情報などのサービスのお知らせ
最新の人気エントリーの配信
処理を実行中です
j次のブックマーク
k前のブックマーク
lあとで読む
eコメント一覧を開く
oページを開く