接口<类型>的适配器接口<子类型>子类型>类型>
考虑以下界面。
public interface Feed<T> {
public void put( T o );
}
我们有一个简单的 Java 类实现这个接口,它将对象写入特定的集合。
public class CollectionFiller<T> implements Feed<T> {
private final Collection<T> collection;
private CollectionFiller( Collection<T> collection ) {
this.collection = collection;
}
public void put( T o ) {
this.collection.add( o );
}
public static <T> CollectionFiller<T> of( Collection<T> collection ) {
return new CollectionFiller<T>( collection );
}
}
现在我们定义两个虚拟类,以使问题具体化。
public class Super {}
public class Sub extends Super {}
假设有一个方法 writeSubsTo( Feed f )
并且我们有一个 CollectionFiller
的实例 cf
。由于 cf 只将对象添加到其“Supers”集合中,因此将其传递给 writeSubsTo 是安全的。但是编译器不允许这样做。 (这种行为的原因我很清楚。)
我想编写一个方便的适配器,它包装 X 类型的 CollectionFiller
并充当特定子类型的 Feed
X 类型。我尝试了(以及许多其他事情)以下内容,但编译器给我带来了麻烦。
class SubFiller<T1, T2 extends T1> implements Feed<T2> {
private final CollectionFiller<T1> collectionFiller;
SubFiller( CollectionFiller<T1> collectionFiller ) {
this.collectionFiller = collectionFiller;
}
public void put( T2 o ) {
this.collectionFiller.put( o );
}
public static <T1, T2 extends T1> SubFiller<T1, T2> of( CollectionFiller<T1> c ) {
return new SubFiller<T1, T2>( c );
}
}
尽管此类没有任何问题,但编译器不会接受以下代码片段中的最后两条语句。
CollectionFiller<Super> cf = CollectionFiller.of( new HashSet<Super>() );
SubFiller<Super, Sub> sf = SubFiller.of( cf );
writeSubsTo( SubFiller.of( cf ) );
有人能想出解决这个问题的好方法吗?我不介意适配器是否包含繁琐的代码,只要使用它不太冗长(因此使用静态工厂方法)。当然,任何其他解决方案(不使用适配器)也可以(同样,只要使用它不太冗长)。
Consider the following interface.
public interface Feed<T> {
public void put( T o );
}
We have a straightforward Java class implementing this interface, that writes objects to a specific collection.
public class CollectionFiller<T> implements Feed<T> {
private final Collection<T> collection;
private CollectionFiller( Collection<T> collection ) {
this.collection = collection;
}
public void put( T o ) {
this.collection.add( o );
}
public static <T> CollectionFiller<T> of( Collection<T> collection ) {
return new CollectionFiller<T>( collection );
}
}
Now we define two dummy classes, in order to make the question concrete.
public class Super {}
public class Sub extends Super {}
Let's say there is some method writeSubsTo( Feed<Sub> f )
and we have an instance cf
of CollectionFiller<Super>
. Since cf
only adds objects to its collection of "Supers", it would be safe to pass it to writeSubsTo
. The compiler won't allow it however. (The reason for this behavior is clear to me.)
I wanted to write a convenience adapter that wraps around a CollectionFiller
of type X and poses as a Feed
of a specific subtype of type X. I tried (among many other things) the following, but the compiler gives me trouble.
class SubFiller<T1, T2 extends T1> implements Feed<T2> {
private final CollectionFiller<T1> collectionFiller;
SubFiller( CollectionFiller<T1> collectionFiller ) {
this.collectionFiller = collectionFiller;
}
public void put( T2 o ) {
this.collectionFiller.put( o );
}
public static <T1, T2 extends T1> SubFiller<T1, T2> of( CollectionFiller<T1> c ) {
return new SubFiller<T1, T2>( c );
}
}
Although there is nothing wrong with this class, the compiler won't accept both of the last two statments in the following code fragment.
CollectionFiller<Super> cf = CollectionFiller.of( new HashSet<Super>() );
SubFiller<Super, Sub> sf = SubFiller.of( cf );
writeSubsTo( SubFiller.of( cf ) );
Can anyone think of a nice way of solving this problem? I wouldn't mind if the adapter contained cumbersome code, as long as using it is not too verbose (hence the static factory methods). Of course, any other solution (not using an adapter) is also fine (again, as long as using it isn't too verbose).
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果您使用
writeSubsTo(Feed f)
,那么您可以传入Feed
。然后你就可以完全取消你的填充课程了。是的,逆变! :-P(还有一个协变版本,
? extends X
,适用于您获取值而不是放入值的情况。)If you use
writeSubsTo(Feed<? super Sub> f)
, then you can pass in aFeed<Super>
. Then you can do away with your filler classes altogether. Yay for contravariance! :-P(There's also a covariant version,
? extends X
, for cases where you're getting values out rather than putting values in.)