1. シングルトンモード
グローバルに保証されている EventBus オブジェクトは 1 つだけです
public static EventBus getDefault() {
EventBus instance = defaultInstance;
if (instance == null) {
synchronized (EventBus.class) {
instance = EventBus.defaultInstance;
if (instance == null) {
instance = EventBus.defaultInstance = new EventBus();
}
}
}
return instance;
}
2. ビルダーモード
複雑なオブジェクトを構築するためのサードパーティのフレームワークで最も一般的なデザイン パターン
EventBusBuilder
public EventBusBuilder logSubscriberExceptions(boolean logSubscriberExceptions) {
this.logSubscriberExceptions = logSubscriberExceptions;
return this;
}
public EventBusBuilder logNoSubscriberMessages(boolean logNoSubscriberMessages) {
this.logNoSubscriberMessages = logNoSubscriberMessages;
return this;
}
3. 観察者パターン
全体的なフレームワークはオブザーバー パターンです
4. フライウェイトモード
オブジェクトを共有することで複数の新しいオブジェクトを回避する
private FindState prepareFindState() {
synchronized (FIND_STATE_POOL) {
for (int i = 0; i < POOL_SIZE; i++) {
FindState state = FIND_STATE_POOL[i];
if (state != null) {
FIND_STATE_POOL[i] = null;
return state;
}
}
}
return new FindState();
}