下面是我认为的这两个类代码的相关部分。首先,TreePointer (原始源here):
public abstract class TreePointer<T extends TreeNode>
implements Iterable<TokenResolver<T>>
{
//...
/**
* What this tree can see as a missing node (may be {@code null})
*/
private final T missing;
/**
* The list of token resolvers
*/
protected final List<TokenResolver<T>> tokenResolvers;
/**
* Main protected constructor
*
* <p>This constructor makes an immutable copy of the list it receives as
* an argument.</p>
*
* @param missing the representation of a missing node (may be null)
* @param tokenResolvers the list of reference token resolvers
*/
protected TreePointer(final T missing,
final List<TokenResolver<T>> tokenResolvers)
{
this.missing = missing;
this.tokenResolvers = ImmutableList.copyOf(tokenResolvers);
}
/**
* Alternate constructor
*
* <p>This is the same as calling {@link #TreePointer(TreeNode, List)} with
* {@code null} as the missing node.</p>
*
* @param tokenResolvers the list of token resolvers
*/
protected TreePointer(final List<TokenResolver<T>> tokenResolvers)
{
this(null, tokenResolvers);
}
//...
/**
* Tell whether this pointer is empty
*
* @return true if the reference token list is empty
*/
public final boolean isEmpty()
{
return tokenResolvers.isEmpty();
}
// .iterator(), .equals(), .hashCode(), .toString() follow
}然后是JsonPointer,它包含这个我想在这里分解的.parent()方法(原始源here
public final class JsonPointer
extends TreePointer<JsonNode>
{
/**
* The empty JSON Pointer
*/
private static final JsonPointer EMPTY
= new JsonPointer(ImmutableList.<TokenResolver<JsonNode>>of());
/**
* Return an empty JSON Pointer
*
* @return an empty, statically allocated JSON Pointer
*/
public static JsonPointer empty()
{
return EMPTY;
}
//...
/**
* Return the immediate parent of this JSON Pointer
*
* <p>The parent of the empty pointer is itself.</p>
*
* @return a new JSON Pointer representing the parent of the current one
*/
public JsonPointer parent()
{
final int size = tokenResolvers.size();
return size <= 1 ? EMPTY
: new JsonPointer(tokenResolvers.subList(0, size - 1));
}
// ...
}正如主题中提到的,我这里遇到的问题是JsonPointer的.parent()方法。事实上,这个方法背后的逻辑适用于所有TreeNode实现。除了我必须使用构造函数,当然这样的构造函数是依赖于实现的:/
有没有办法实现.parent(),这样TreeNode的每个实现都会收到自己的一个实例,而不是TreeNode,或者这只是一个白日梦?
发布于 2013-06-30 00:38:43
当然,您可以将abstract方法parent添加到TreePointer超类中。真正的问题是,这有意义吗?我们真的没有足够的上下文来知道它是否有意义。
如果它对TreePointer的所有情况都有意义,那么继续向TreePointer添加一个abstract方法parent,并在所有子类中添加一个具体的实现。或者,您可以在TreePointer中放置一个默认实现,并在您希望在子类中提供不同行为的地方覆盖它。
另外,我如何确保对于TreeNode的实现X,返回类型是X而不是TreeNode?归根结底,这是我的问题。
在TreePointer类中,您只需将方法设置为public TreePointer<T> parent()。您应该能够使所有的代码在那里泛型,除了new new JsonPointer(...)部件。您可能必须将其重构为一个单独的方法(可能是protected),您可以在每个具体的子类中覆盖该方法。
既然您希望在子类中返回JsonPointer而不是TreePointer<T>,并且还存在需要(非静态)工厂方法的问题,我认为您最初的怀疑是非常正确的。您可以在父类中使用抽象实现来实现这一点,但您需要覆盖它以在子类中提供更具体的返回类型,我认为这不值得麻烦。只需在TreePointer中声明一个public abstract TreePointer<T> parent(),并在JsonPointer中用public JsonPointer parent()覆盖它。
发布于 2013-06-30 00:25:59
您可以在JsonPointer上创建工厂方法,这样您就不必使用构造函数,并且可以独立于实现。
https://stackoverflow.com/questions/17382227
复制相似问题