有条件地更改对象类Java

有条件地更改对象类Java,java,class,Java,Class,因此,我有一段现有的代码,如下所示: Metrics metrics = null; try { metrics = metricsService.getCurrentMetrics(); } catch (SQLException e) { e.printStackTrace(); } //Do some stuff with the metrics Metrics metrics = null; APIMetrics apiMetrics = null; try {

因此,我有一段现有的代码,如下所示:

Metrics metrics = null;

try {
    metrics = metricsService.getCurrentMetrics();
} catch (SQLException e) {
    e.printStackTrace();
}

//Do some stuff with the metrics
Metrics metrics = null;
APIMetrics apiMetrics = null;

try {
    if(user.isAPI())
        apiMetrics = metricsService.getCurrentAPIMetrics();

    else
        metrics = metricsService.getCurrentMetrics();
} catch (SQLException e) {
    e.printStackTrace();
}

//Do some stuff with the metrics
我最近实现了一个单独的机制,作为在单独的类(巧妙地称为APIMetrics)下跟踪API度量的一种方法

因此,通过此更改,代码将如下所示:

Metrics metrics = null;

try {
    metrics = metricsService.getCurrentMetrics();
} catch (SQLException e) {
    e.printStackTrace();
}

//Do some stuff with the metrics
Metrics metrics = null;
APIMetrics apiMetrics = null;

try {
    if(user.isAPI())
        apiMetrics = metricsService.getCurrentAPIMetrics();

    else
        metrics = metricsService.getCurrentMetrics();
} catch (SQLException e) {
    e.printStackTrace();
}

//Do some stuff with the metrics
问题在于,下面的代码“使用度量做一些事情”都是根据使用度量对象编写的。有没有一种方法可以将其设置为“metrics”对象不只是指metrics类型的对象,而是指我们想要使用的对象

因此,例如,如果我们有一个非API用户,我们希望度量的类型为metrics,并且是metricsService.getCurrentMetrics()的结果。但是,如果我们有一个API用户,我们希望度量的类型为APIMetrics,并且是metricsService.getCurrentAPIMetrics()的结果


有什么好办法吗?Metrics和APIMetrics类共享所有方法。我知道可能有一种方法可以使用继承或多态性来实现这一点,但我不知道该怎么做。谢谢

使两个类
Metrics
APIMetrics
实现相同的接口。然后,声明一个变量,其类型将是此接口,相应地初始化它并通过代码使用它。

使两个类
Metrics
APIMetrics
实现相同的接口。然后,声明一个变量,其类型将是此接口,相应地初始化它并通过代码使用它。

是的,根据上述答案,您可以使两个类实现一个声明所有方法的接口

public interface MetricsInt(){
// declare common methods
}

在代码中,您可以引入一个工厂方法,该方法将用户作为输入,并返回预期类型的度量实例。由于变量的类型为MetricInt,因此两种类型的对象都可以设置为MetricInt

MetricsInt metrics = null;

try {
    metrics = metricsService.getCurrentMetricsForUser(user);
} catch (SQLException e) {
    e.printStackTrace();
}

是的,根据上面的回答,您可以使这两个类实现一个声明所有方法的接口

public interface MetricsInt(){
// declare common methods
}

在代码中,您可以引入一个工厂方法,该方法将用户作为输入,并返回预期类型的度量实例。由于变量的类型为MetricInt,因此两种类型的对象都可以设置为MetricInt

MetricsInt metrics = null;

try {
    metrics = metricsService.getCurrentMetricsForUser(user);
} catch (SQLException e) {
    e.printStackTrace();
}

好吧,你击中了他头上的钉子。。。继承(或接口,更具体地说)在这里是一个很好的解决方案,你不知道怎么做吗?我想这主要是我的脑袋绕着整个代码库会发生什么变化的问题。这是相当模糊的。我建议您阅读一些关于如何实现接口/继承的教程,并尝试一下。您不需要做太多更改。您将拥有
IMetrics Metrics
对象,而不是
Metrics Metrics
。然后,您将按照您的实际情况分配给metrics变量,如果您的代码中有人这样想的话,您不必做太多更改。您可以执行'List List=new ArrayList();`列表变量不关心它是ArrayList还是任何其他类型的列表。您的代码将只使用在
列表
界面中可以找到的内容,因此您不必更改代码。好吧,您一针见血。。。继承(或接口,更具体地说)在这里是一个很好的解决方案,你不知道怎么做吗?我想这主要是我的脑袋绕着整个代码库会发生什么变化的问题。这是相当模糊的。我建议您阅读一些关于如何实现接口/继承的教程,并尝试一下。您不需要做太多更改。您将拥有
IMetrics Metrics
对象,而不是
Metrics Metrics
。然后,您将按照您的实际情况分配给metrics变量,如果您的代码中有人这样想的话,您不必做太多更改。您可以执行'List List=new ArrayList();`列表变量不关心它是ArrayList还是任何其他类型的列表。您的代码将只使用
列表
界面中可以找到的内容,因此您不必更改代码。谢谢,我现在意识到这实际上是一个相当愚蠢的问题。我知道答案,只是不知道它会如何影响代码库。谢谢,我现在意识到这实际上是一个相当愚蠢的问题。我知道答案,只是不知道它会如何影响代码库。