This question is somewhat inspired by a previous question I've asked.
Most of the code I write has to do with an API, which by its nature has to be easily extendible. A common problem that I and the rest of my team wrestle with is how to handle plugins, i.e classes that may extend our own interfaces and in the final implementation should feel like an atomic part of the solution. I'm thinking in particular of the following kind of use case (to use a car analogy). Interface:
提供的实现:
第三方实现:
我们通常要求类
Most of the code I write has to do with an API, which by its nature has to be easily extendible. A common problem that I and the rest of my team wrestle with is how to handle plugins, i.e classes that may extend our own interfaces and in the final implementation should feel like an atomic part of the solution. I'm thinking in particular of the following kind of use case (to use a car analogy). Interface:
Car
提供的实现:
Ford
Audi
第三方实现:
Toyota
我们通常要求类
CarDealer
意识到所有现有的 Car
实现,而不需要第三方强制在配置文件中明确声明 Car
。 我一直在考虑的一个想法是让 Car
的实现自己向 CarDealer
注册,但这会引起新的问题,因为在显式引用之前,Toyota
(或任何其他实现)不会被初始化,这是个进退两难。 对实现 Car
进行注释,然后通过代码并初始化具有相关注释的任何类也是一种方法,但我担心这可能会非常重资源消耗。
我认为我们不可能是第一个遇到这个问题的人,那么有没有已知的设计模式来解决这个问题呢?