简体   繁体   English

在利用多态性的同时避免类的创建

[英]avoiding class creation while making use of polymorphism

I don't really have a lot of practical experience with either java or oop in general so now I'm stuck with a problem that's probably really easy to work around but where I'm not sure at all how an elegant, oop oriented solution might look like. 一般而言,我对Java或oop并没有太多的实践经验,所以现在我遇到了一个可能很容易解决的问题,但是我根本不确定如何以一种优雅的,面向oop的解决方案可能看起来像。

So here's a simplified rundown: 因此,这是一个简化的清单:

Say I wanted to write some sort of calculating application which first of all contains several methods like: 说我想编写某种计算应用程序,它首先包含几种方法,例如:

static double sine(double x){...}
static double cosine(double x){...}

and so forth. 等等。 Some other static method would then perform some sort of calculation that involves the derivative of one of these functions. 然后,某些其他静态方法将执行涉及这些函数之一的导数的某种计算。 If we pretend there was no way to approximate that derivative, the easiest solution that came to mind for me was to wrap each of the method above in a class and to let those classes implement an interface 'Differentiable' that defines the method 'evaluateDerivative', eg: 如果我们假装没有办法近似该导数,那么我想到的最简单的解决方案是将上述每个方法包装在一个类中,并让这些类实现定义方法“ evaluateDerivative”的接口“ Differentiable” ,例如:

interface Differentiable {
    double evaluateDerivative(double x);
}

class sine implements Differentiable {          
    static double evaluate(double x){
        return...;
    }

    public double evaluateDerivative(double x) {
        return cosine.evaluate(x);
    }
}

so if I needed the derivative of any method for another calculation I could simply do something like this: 因此,如果需要任何方法的派生进行另一次计算,我可以简单地执行以下操作:

static double returnDerivativePlusOne(Differentiable d, double x){          
    return d.evaluateDerivative(x) + 1;
}

Okay, now the problem is this: when I actually want to call the method above, I need an instance of the sine class, eg: 好的,现在的问题是这样的:当我实际上要调用上面的方法时,我需要一个正弦类的实例,例如:

DerivativePlusOne(new sine(), 1);

which doesn't really make sense because the sine class only contains static methods (and maybe some final fields) so creating an object seems strange to me. 这实际上没有任何意义,因为正弦类仅包含静态方法(可能还包含一些最终字段),因此创建对象对我来说似乎很奇怪。

So, is there a different approach that would produce the same outcome in a more elegant way ? 那么,是否有其他方法可以更优雅地产生相同的结果? Any help would be appreciated. 任何帮助,将不胜感激。

Why not make evaluateDerivative function static as well. 为什么也不要使evaluateDerivative函数也为静态。 There is no need of interface. 不需要接口。

To make use of polymorphism, we can do the following. 要利用多态性,我们可以执行以下操作。 Suppose we are doing the following: we have two class Sine and Cosine, and an interface Differentiable. 假设我们正在执行以下操作:我们有两个Sine和Cosine类,以及一个Differentiable接口。

interface Differentiable {
    double evaluateDerivative(double x);
}

class Sine implements Differentiable {
    static double evaluate(double x){return...}
    public double evaluateDerivative(double x) {return somevalue;}
}

class Cosine implements Differentiable {        
    static double evaluate(double x){return...}
    public double evaluateDerivative(double x) {return somevalue;}
}

In that case, to make use of polymorphism, what you can do is: 在这种情况下,要利用多态性,您可以做的是:

Differentiable d = new Sine();
double derivative = d.evaluateDerivate();
d = new Cosine();
derivative = d.evaluateDerivate();

Hope it helps. 希望能帮助到你。

Why do you want that methods to be static? 为什么要让这些方法是静态的? And if there is no particular reason, maybe your application should just create an instance of a calculating class at startup, and then use it? 如果没有特殊原因,也许您的应用程序应该在启动时创建一个计算类的实例,然后使用它?

In my opinion, if you really insist on creating objects that provide typical functions and you want to have derivatives of those functions as well, you may hand-code those derived methods (in another or even the same class). 在我看来,如果您真的坚持要创建提供典型功能的对象,并且还希望拥有这些功能的派生类,则可以手工编码这些派生的方法(在另一个甚至同一类中)。 I actually can't see where you'd use polymorphism with such case, as this is not a typical oo app (because your objects are just bundles of calculating methods). 我实际上看不到在这种情况下要在哪里使用多态,因为这不是典型的oo应用程序(因为您的对象只是一堆计算方法)。

What's even more, if you really wanted to create derived classes to calculate derivatives, your evaluateDerivative method should return an object of the derived class, and not a numer. 更重要的是,如果您真的想创建派生类以计算派生类,则您的evaluateDerivative方法应返回派生类的对象,而不是数字。

An elegant solution in this case would be, in my opinion, to create a kind-of-library containing the methods you want. 我认为,在这种情况下,一种优雅的解决方案是创建一个包含所需方法的类库。 Just an easy-to-use bundle of methods, as your classes does not seem to provide anything more than calculating methods (of typical maths functions, for which there are already written functions as well). 只是一个易于使用的方法包,因为您的类似乎只提供计算方法(典型的数学函数的计算方法,为此已经编写了函数)。 I'd still say that created bundle (which may be even a static class) fulfills Single Responsibility Principle (as it only provides some maths functions), but even this does not appear to be so important here. 我仍然要说,创建的包(甚至可能是静态类)满足“单一职责原则”(因为它仅提供一些数学函数),但是即使在这里看起来也没有那么重要。 The rules of creating elegant OO solutions (like SOLID rules, for example) are there to help you write code that is easier to manage and handy to build on top of. 创建优雅的OO解决方案的规则(例如SOLID规则)可帮助您编写易于管理且易于构建的代码。 I can't see how would you build a bigger class hierarchy actually based on your calculating class, so the simplest solution may be the best. 我看不到您将如何实际基于您的计算类构建更大的类层次结构,因此最简单的解决方案可能是最好的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM