简体   繁体   English

如果实现不可用,如何实例化接口?

[英]How can I instantiate an interface if implementation is not available?

If I have the interface accessible from all modules, but implementation cannot be accessible, how can I instantiate this from other modules? 如果我可以从所有模块访问该接口,但是无法访问实现,那么如何从其他模块实例化该接口?

If I have 3 modules, one module is accessible to all which has my interface, and the other 2 modules have no access to each other. 如果我有3个模块,那么拥有我的接口的所有模块都可以访问一个模块,而其他2个模块则无法相互访问。 How can I instantiate this interface? 如何实例化此接口?

How to access an implementation without adding Maven dependency? 如何在不添加Maven依赖项的情况下访问实现?

Add a factory class alongside the interface. 在界面旁边添加一个工厂类。 This factory can either be instantiated easily or provide a static method such as public static StuffFactory getInstance() { 该工厂可以轻松实例化,也可以提供静态方法,例如public static StuffFactory getInstance() {

This factory is in charge of creating new instances of your interface. 该工厂负责创建接口的新实例。 Example: 例:

public Stuff newStuff() {
  return new StuffImpl();
}

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

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