简体   繁体   English

创建10s / 100s的Guice注射器是否被认为是不好的做法/设计?

[英]Is it considered bad practice/design to create 10s/100s of Guice Injectors?

Consider a hypothetical application: 考虑一个假设的应用程序:

  • That has a need to create tens or even hundreds of small groups of objects 需要创建数十甚至数百个小型对象组
    • (Group A: Class A instance, Class D instance, Class M instance. Properties P') (组A:A类实例,D类实例,M类实例。属性P')
    • (Group B: Class B instance, Class D instance) (B组:B类实例,D类实例)
    • (Group C: Class B instance, Class D instance, Class F instance. Properties P'') (组C:B类实例,D类实例,F类实例。属性P'')
  • Each of these objects are created by its own Provider that is registered with the parent/root Injector using MapBinder or MultiBinder (typical "plugin" usecase) 这些对象均由自己的提供者创建,并使用MapBinder或MultiBinder在父/根Injector中注册(典型的“插件”用例)
  • To create each group, a new child Injector is created that uses the Providers from the root and a few other providers that are local to this child Injector - like configuration/properties bindings 要创建每个组,将创建一个新的子注入器,该子注入器使用根中的提供程序以及该子注入器本地的其他一些提供程序(例如配置/属性绑定)
  • This tight group of objects is created together to perform some task and then discarded together - after a few hours 紧密创建一组对象以执行某些任务,然后一起丢弃-几个小时后
  • The group has some unique parameters 该组具有一些独特的参数
    • Meaning, that each object created within the group has distinct configuration properties 意思是,组内创建的每个对象都具有不同的配置属性
    • They are different from another instance of that class in a different group 它们不同于该类在另一个组中的另一个实例

Question: Assuming that for some reason the problem described above cannot be accomplished by using run of the mill design patterns like Factory, Builder, Creator etc: 问题:假设由于某种原因,无法通过使用工厂,构建器,创建者等工厂设计模式来解决上述问题:

  • Is there anything wrong with the approach described above? 上述方法有什么问题吗?
    • Performance, memory foot print 性能,内存占用量
    • Readability (vs using plain old Java new ) 可读性(与使用普通的旧Java new
  • Does it sound too complicated and relying on libraries to do something simple? 听起来太复杂了,依靠库来做一些简单的事情吗?

At the level of abstraction you described, that sounds like a reasonable solution, and conducive to some good design patterns (like loose coupling and instance immutability). 在您描述的抽象级别上,这听起来像是一个合理的解决方案,并且有助于某些良好的设计模式(例如,松散耦合和实例不变性)。 To the best of my knowledge Guice will not pose any greater threat of memory leaks or other performance problems than any comparable solution for those complex needs—just keep an eye on which references you keep so the GC can do its job. 据我所知,Guice不会比针对那些复杂需求的任何同类解决方案带来更大的内存泄漏或其他性能问题的威胁-只需留意所保留的引用,GC就可以完成工作。

To that point you may want to be careful about creating singleton objects in Guice, though, if you rely on them being garbage collected. 到那时,如果您依赖于对对象进行垃圾收集,则可能要在Guice中创建单例对象时要小心。 Objects declared as @Singleton (or asEagerSingleton or toInstance ) cannot be garbage-collected as long as the Injector is reachable, because the Injector is obligated to return the exact same instance if it is ever asked for again. 声明为对象@Singleton (或asEagerSingletontoInstance )不能作为垃圾回收,只要注射器可达,因为注射器有义务返回完全相同的情况下,如果它是不断询问一次。 In desktop VMs it may be cheaper to get multiple instances of a stateless object so the GC can collect them all, rather than declaring the object as a singleton that can never be collected. 在台式机VM中,获取无状态对象的多个实例可能会比较便宜,以便GC可以收集所有实例,而不是将对象声明为永远无法收集的单个实例。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM