2

坦率地说,我离成为泛型专家还很远,我必须重构一些代码。我有三个子类:D类、R类和S类。这些类中的每一个都扩展类AbstractA子类的目标非常相似,每个子类在方法m()中都有相似的代码。所以我认为如果我可以从子类中提取方法 m() 并将其替换为上一层(到类 AbstractA 中),那也不错。

但是有一些区别:

D 类适用于:

Map<String, Map<BigInteger, BigInteger>> counts = new HashMap<String, Map<BigInteger,BigInteger>>();
Map<String, Map<BigDecimal, Set<BigInteger>>> sums = new HashMap<String, Map<BigDecimal, Set<BigInteger>>>();

R类适用于:

Map<String, Map<String, BigInteger>> counts = new HashMap<String, Map<String,BigInteger>>();
Map<String, Map<BigDecimal, Set<String>>> sums = new HashMap<String, Map<BigDecimal, Set<String>>>();

S 类适用于:

Map<String, Map<BigInteger, BigInteger>> counts = new HashMap<String, Map<BigInteger,BigInteger>>();
Map<String, Map<BigDecimal, BigInteger>> sums = new HashMap<String, Map<BigDecimal, BigInteger>>();

欢迎任何关于上述泛型的可能修改的提示,这可能会导致更“通用”的版本。(可以移动到类 AbstractA 并且仍然可以由三个子类使用。)

4

4 回答 4

3
 class AbstractA<T,U> {
    Map<String, Map<T, BigInteger>> counts = new HashMap<String, Map<T,BigInteger>>();
    Map<String, Map<BigDecimal, U>> sums = new HashMap<String, Map<BigDecimal, U>>();
 }
 class AbstractB<T> extends AbstractA<T, Set<T>> {
 }
 class D extends AbstractB<BigInteger> {
 }
 class R extends AbstractB<String> {
 }
 class S extends AbstractA<BigInteger, BigInteger> {
 }
于 2013-01-07T13:04:09.813 回答
2

您只需要两个通用参数:

public abstract class AbstractA<K, V> {
    private Map<String, Map<K, BigInteger>> counts = new HashMap<String, Map<K, BigInteger>>();
    private Map<String, Map<BigDecimal, V>> sums = new HashMap<String, Map<BigDecimal, V>>();

    public void m() {
    }
}


public class D extends AbstractA<BigInteger, Set<BigInteger>> {
}

public class R extends AbstractA<String, Set<String>> {
}

public class S extends AbstractA<BigInteger, BigInteger> {
}
于 2013-01-07T13:08:56.797 回答
0

您需要做的第一件事是在String和之间创建一些共性BigInteger。我的建议是创建一个composed属于这两种类型的新类:

public class Special  
{  
    private String string;  
    private BigInteger bigInt;  
    ...
}  

现在你可以改变你Map喜欢的参数:

Map<String, Map<Special, BigInteger> ...  

现在,您可以将调用保留在每个子类中,并将m函数标记AbstractAabstract或者提供一个可以被覆盖的实现。

于 2013-01-07T12:55:31.073 回答
0

下面的实现呢?

public abstract class AbstractA<K1, V1, K2, V2> {

    protected Map<String, Map<K1, V1>> counts;
    protected Map<String, Map<K2, V2>> sums;

    public void m() {
        // your factorized code using counts and sums
    }

}

public class D extends AbstractA<BigInteger, BigInteger, BigDecimal, Set<BigInteger>> {

    public D() {
        counts = new HashMap<String, Map<BigInteger, BigInteger>>();
        sums = new HashMap<String, Map<BigDecimal, Set<BigInteger>>>();
    }

}

public class R extends AbstractA<String, BigInteger, BigDecimal, Set<String>> {

    public R() {
        counts = new HashMap<String, Map<String, BigInteger>>();
        sums = new HashMap<String, Map<BigDecimal, Set<String>>>();
    }

}

public class S extends AbstractA<BigInteger, BigInteger, BigDecimal, BigInteger> {

    public S() {
        counts = new HashMap<String, Map<BigInteger, BigInteger>>();
        sums = new HashMap<String, Map<BigDecimal, BigInteger>>();
    }

}
于 2013-01-07T13:00:37.840 回答