Giter Club home page Giter Club logo

Comments (7)

drakeet avatar drakeet commented on July 29, 2024

Q: 局部中注册的时候是否是把所有全局中的类型全部注册到了局部中(虽然会有覆盖)?
A: 是的,但如果出现冲突,即有一样的 key 类型,则以局部的为准,舍弃来自全局的冲突 item class - provider. 这点在 wiki 中 https://github.com/drakeet/Effective-MultiType#使用-全局类型池 是有提及的。

Q: 那么如果我只是想使用全局中的部分类型,那么没用到的会不会造成浪费的情况?
A: 没用到可以说浪费,但这点浪费微乎其微。也是在 wiki 中有这么一段话可以回答你的问题:

类型池都只会持有 class 和非常轻薄的 provider 对象。我做过一个试验,就算拥有上万个类型和 provider,内存占用也是很少的,索引速度也很快,在主线程连续注册一万个类型花费不过 10 毫秒的时间,何况一般一个应用根本不可能有这么多类型,完全不必担心这方面的问题。

from multitype.

drakeet avatar drakeet commented on July 29, 2024

不过,全局类型池比较不推荐使用,原则是能不使用就不要使用。因为一个模块最好是能自包含、高内聚,如果依赖了全局类型池,那么可能就产生了一些耦合与不确定性,也不够直观。只有那些非常确定不会改变且简单的类型可以注册到全局去,比如 Line/Divider 等。

from multitype.

werbhelius avatar werbhelius commented on July 29, 2024

感谢!

突然还有一个疑问

  1. 既然是在局部中重新注册全局类型,那么如果我先调用 adapter. register() , 后调用 applyGlobalMultiTypePool(),那么如果有相同的key,全局的是不是同样会覆盖调局部的?
  2. 如果问题1成立的话,那么在 one2many 的情况下,是不是就会出现类型错误的情况(比如在IM中)?

这样想,是不是太极端了呀,上面的问题好像都可以人为控制不发生,我也觉得全局是一些 Line/Divider的类型

🙈🙈

from multitype.

drakeet avatar drakeet commented on July 29, 2024

回答 1:
你可以看看相关源码就可以彻底了解这个问题的答案了:

public void applyGlobalMultiTypePool() {
    for (int i = 0; i < GlobalMultiTypePool.getContents().size(); i++) {
        final Class<?> clazz = GlobalMultiTypePool.getContents().get(i);
        final ItemViewProvider provider = GlobalMultiTypePool.getProviders().get(i);
        if (!this.getContents().contains(clazz)) {
            this.register(clazz, provider);
        }
    }
}

从源码中可以看出,与顺序无关,一旦有冲突,永远以局部的为准。

from multitype.

drakeet avatar drakeet commented on July 29, 2024

至于 one2many(一对多) 问题,你可以查看 wiki 中关于此的章节,以及 one2many sample,另外,如果设计 IM,可以参考 TimeMachine:

鉴于有网友不解如何使用 MultiType 来构建一个 IM 消息 UI 架构,TimeMachine 做了一次示范,简单实现了我们在 MultiType wiki 中谈到的所谓最好的方案:"每一种消息都有左边和右边的样式,分别对应别人发来的消息和你发出的消息。如果左边算一种,右边又算一种,就是比较不好的设计了,会导致布局内容重复、冗余,修改操作都要做两遍。最好的方案是让他们视被为同一种类型,然后在 item 框层次进行左右边判断和框架相关数据绑定。"
https://github.com/drakeet/TimeMachine/pull/43

from multitype.

drakeet avatar drakeet commented on July 29, 2024

one2many sample:

https://github.com/drakeet/MultiType/tree/master/sample/src/main/java/me/drakeet/multitype/sample/one2many

from multitype.

werbhelius avatar werbhelius commented on July 29, 2024

😅懂了!
之前一直在思考 this.register(clazz, provider); , 忽略了 !this.getContents().contains(clazz) 中的 this.getContents()
还是太粗心了
感谢💪💪~

from multitype.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.