EventBus의 디자인 패턴

1. 싱글톤 모드

전역적으로 보장되는 EventBus 개체는 하나만 있습니다.

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();
}

추천

출처blog.csdn.net/Android_yh/article/details/130451406