使用Java接口作为参数来加强严格(更好)类型安全性的想法 [英] Ideas to enforce strict (better) type safety with a Java Interface as the parameter

查看:122
本文介绍了使用Java接口作为参数来加强严格(更好)类型安全性的想法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

更新 基于以下输入.一次又一次,我感到类型安全使用Java接口的努力不那么简单.

Update based on inputs below. Now and again I feel efforts for type-safe use of Java interfaces less than straightforward.

这是我们想要做的一个简单示例:设计,紧随其后.经过多次尝试以获得类型安全性的实现时,我(再次 )发现我们需要使用Java进行思考,以鼓励Java支持我们的需求.我对原始示例进行了改进.我将问题悬而未决,因为可能有更好的方法来解决问题.

Here's a simple example of what we'd like to do: a design, following. When it comes to implementation after several attempts to get type safety, I'm (again) discovering that we need to think in Java to encourage Java to support our needs. I'm made improvements on the original example. I'm leaving the question open because there's probably a better way to go about things.

意图:

  1. 考虑一些我们想放入哈希表的不同类.
  2. 我们希望将这些作为 ServiceProvider 接口实例的集合进行管理.
  3. 想法是进行这样的OO设计:

服务类型:

public interface DisplaySettings    extends     ServiceInterface { ... }

提供者类型:

public interface ServiceProvider    extends     ServiceInterface { ... }

实施:

    public class DisplayConfig extends Config  
                               implements DisplaySettings, ServiceProvider 
    { ... }

  1. 当我们想注册"实现时,挑战就来了.这是 所需 方法的方法原型:

  1. The challenge comes when we want to 'register' an implementation. This is a method prototype for the desired method:

public boolean registerService( final ServiceProvider newProvider )
{ ... }

  • 由newProvider实现的服务由以下方式提供:newProvider.getServiceInterface()
  • 我们想按如下方式调用这种方法:

    And we'd like to call this kind of methods as follows:

    ServicesMgr.registerServiceProvider( this );
    

  • 由于下面的建议,要求注册提供者的模式是这样指定接口:

  • Thanks to suggestions below, the schema to ask for the a registered provider is to specify the interface as so:

    DisplaySettings videoSettings = ServicesMgr.getProvider( DisplaySettings.class );
    

  • 当然,您不能这样做唯一的编译方法是使用" .class "进行注册和查找提供程序实例对象.以及对以上整洁定义的必要更改...

    Of course, you can't do that ! The only alternative that compiles is to use the ".class" to register and look-up the provider instance object. Along with requisite changes to the tidy definitions above ...

        DisplaySettings videoSettings = ServicesMgr.getProvider( DisplaySettings.class );
    

    哪种类型更安全.

    @immibis所建议的get provider调用...

    The get provider call as suggested by @immibis ...

      public <InterfaceType extends ServiceInterface> ServiceProvider getProvider( Class<InterfaceType> interfaceClass )
      { ... }
    

    可悲的是,该解决方案不允许HashMap在查找提供程序时"查找"提供程序.哈希表似乎将使用"Class",它不能标识实现类.我尝试了一些变体,例如:

    Sadly that solution won't allow the HashMap to 'find' the provider when it comes time to look it up. It looks like the hash map will use the "Class" which doesn't identify the implementation class. I have tried a few variations like:

      public Class<ServiceInterface>  getServiceType(){ ... }
    

    并使用上面的InterfaceType.我对正在发生的事情的理解是,这是具有挑战性的,因为只有单个继承. Class不是:Class,其中:

    And using the InterfaceType from above. My understanding of what's happening is that this is challenging because there's only single inheritance. Class is not: Class, where:

      <InterfaceType extends ServiceInterface>
    

    此刻,我将使用:public Class getServiceType(){...}作为一致键.当然,由于我们只将"Class"放为一种类型,因此它使类型的安全性无效.通过将getServiceType包装在一个方法中可以起到一定的保护作用.

    At the moment I'm left with using: public Class getServiceType(){...}, as the consistent key. Of course since we are devolved to just "Class" as a type it kind of voids type safety. There's some protection by wrapping the getServiceType in a method.

    继续努力从相关的示例中提取类型保护:

    Continuing to working on extracting type-protection from relevant looking examples:

    • Type-safe Hetrogenous Container Pattern
    • Enforcing parameter types on dependent interfaces?
    • In Java HashMap, how to enforce a generic type to be a subclass

    看起来都不像我们正在寻找的东西().这有可能吗?我正在寻找如何做的想法?非常感谢您的宝贵时间.

    Neither look like what we are looking for (yet). Is this or something close possible? I'm looking for ideas on how-to-do it? Many thanks for your time.

    推荐答案

    DisplaySettings videoSettings = ServicesMgr.getProvider( DisplaySettings.class );
    

    是有效的代码,并且如果这样定义getProvider,则是类型安全的:

    is valid code, and type-safe, if getProvider is defined like this:

    public <InterfaceType extends ServiceInterface> InterfaceType getProvider(Class<InterfaceType> interfaceClass) {
        ...
    }
    

    这应该很容易实现.

    您的registerServiceProvider方法在概念上也看起来很复杂,但是我看不到您对此有任何疑问.

    Your registerServiceProvider method also looks conceptually complex, but I don't see any questions you are asking about it.

    这篇关于使用Java接口作为参数来加强严格(更好)类型安全性的想法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆