Java 类型安全的异构容器

it2022-05-05  137

转载自:http://blog.csdn.net/sh_c1991/article/details/45965743

 

我们的想法是用key自身的class 类型作为key。因为Class 是参数化的类型,它可以确保我们使Context方法是类型安全的,而无需诉诸于一个未经检查的强制转换为T。这种形式的一个Class 对象称之为类型令牌(type token)。

 

[java]  view plain  copy   public class Context {       private final Map<Class<?>, Object> values = new HashMap<>();       public <T> void put( Class<T> key, T value ) {      values.put( key, value );    }       public <T> T get( Class<T> key ) {      return key.cast( values.get( key ) );    }       [...]  }  

请注意在Context#get 的实现中是如何用一个有效的动态变量替换向下转型的。客户端可以这样使用这个context:

 

 

[java]  view plain  copy   Context context = new Context();  Runnable runnable ...  context.put( Runnable.class, runnable );     // several computation cycles later...      Executor executor = ...  context.put( Executor.class, executor );     // even more computation cycles later...  Runnable value = context.get( Runnable.class );   这次客户端的代码将可以正常工作,不再有类转换的问题,因为不可能通过一个不同的值类型来交换某个键值对。

Bloch指出这种模式有两个局限性。“首先,恶意的客户端可以通过以原生态形式(raw form)使用class对象轻松地破坏类型安全。”为了确保在运行时类型安全可以在Context#put中使用动态转换(dynamic cast)。

 

 

[java]  view plain  copy   public <T> void put( Class<T> key, T value ) {    values.put( key, key.cast( value ) );  }  

第二个局限在于它不能用在不可具体化(non-reifiable )的类型中

 

换句话说,你可以保存Runnable 或Runnable[],但是不能保存List<Runnable>。

这是因为List<Runnable>没有特定class对象,所有的参数化类型指的是相同的List.class 对象。因此,Bloch指出对于这种局限性没有满意的解决方案。

多条同类型容器条目

为了能够存储多条同类型容器条目,我们可以用自定义key改变Context 类。这种key必须提供我们类型安全所需的类型信息,以及区分不同的值对象(value objects)的标识。一个以String 实例为标识的、幼稚的key实现可能是这样的:

 

[java]  view plain  copy   public class Key<T> {       final String identifier;    final Class<T> type;       public Key( String identifier, Class<T> type ) {      this.identifier = identifier;      this.type = type;    }  }  

我们再次使用参数化的Class作为类型信息的钩子,调整后的Context将使用参数化的Key而不是Class。

 

 

[java]  view plain  copy   public class Context {       private final Map<Key<?>, Object> values = new HashMap<>();       public <T> void put( Key<T> key, T value ) {      values.put( key, value );    }       public <T> T get( Key<T> key ) {      return key.type.cast( values.get( key ) );    }       [...]  }  

客户端将这样使用这个版本的Context:

 

 

[java]  view plain  copy   Context context = new Context();     Runnable runnable1 = ...  Key<Runnable> key1 = new Key<>( "id1", Runnable.class );  context.put( key1, runnable1 );     Runnable runnable2 = ...  Key<Runnable> key2 = new Key<>( "id2", Runnable.class );  context.put( key2, runnable2 );     // several computation cycles later...  Runnable actual = context.get( key1 );     assertThat( actual ).isSameAs( runnable1 );  

虽然这个代码片段可用,但仍有缺陷。在Context#get中,Key被用作查询参数。用相同的identifier和class初始化两个不同的Key的实例,一个用于put,另一个用于get,最后get操作将返回null 。这不是我们想要的……

[java]  view plain  copy   Context context = new Context();     Runnable runnable1 = ...  Key<Runnable> key1 = new Key<>( "same-id", Runnable.class );  Key<Runnable> key2 = new Key<>( "same-id", Runnable.class );  context.put( key1, runnable1 );//一个用于put     context.get(key2); //另一个用于get --> return null;  

幸运的是,为Key设计合适的equals 和hashCode 可以轻松解决这个问题,进而使HashMap 查找按预期工作。最后,你可以为创建key提供一个工厂方法以简化其创建过程(与static import一起使用时有用):

[java]  view plain  copy   public static  Key key( String identifier, Class type ) {    return new Key( identifier, type );  }  

转载于:https://www.cnblogs.com/huyong0401/p/7084867.html


最新回复(0)