racofix / architecture-for-android

🙈 Coroutines + flow + mvvm-clean / mvp
881 stars 239 forks source link

samples中为什么要加所有logic的代理 不明白 能解释下吗 #18

Closed yanbin92 closed 6 years ago

yanbin92 commented 6 years ago

public class LogicProxy { private static final LogicProxy m_instance = new LogicProxy();

public static LogicProxy getInstance() {
    return m_instance;
}

private LogicProxy() {
    m_objects = new HashMap<>();
}

private Map<Class, Object> m_objects;

public void init(Class... clss) {
    List<Class> list = new LinkedList<Class>();
    for (Class cls : clss) {
        if (cls.isAnnotationPresent(Implement.class)) {
            list.add(cls);
            for (Annotation ann : cls.getDeclaredAnnotations()) {
                if (ann instanceof Implement) {
                    try {
                        m_objects.put(cls, ((Implement) ann).value().newInstance());
                    } catch (InstantiationException e) {
                        e.printStackTrace();
                    } catch (IllegalAccessException e) {
                        e.printStackTrace();
                    }
                }
            }
        }
    }
}

public <T> T getProxy(Class cls) {
    return (T) m_objects.get(cls);
}

public <T> T bind(Class cls, BaseView o) {
    Object ret = m_objects.get(cls);
    ((BasePresenter) ret).attachView(o);
    return (T) ret;
}

}