繁体   English   中英

如何创建更高效​​的版本?

[英]How do I create a more efficient version of this?

我刚刚开始学习Java。 我的大部分知识都是Python和html / css。 我想通过从我的工作中取一个例子来创建一个自动计数系统来练习。 基本上,太阳能电池板连接到“逆变器”,并且每种逆变器类型最多只能容纳一定数量的太阳能电池板(模块)。 3800h逆变器最多可容纳18个-如果超出该范围,我们将需要使用一个可容纳37个逆变器的7600h逆变器。为此,我创建了一个非常冗余的文本版本,主要目的是在各个模块之间平均分配模块如果我有超过10000h的逆变器可以容纳两个逆变器。 基本上将两个7600h逆变器之间的模块数量分开。

有没有人有更好的冗余等式的指针/想法? 而不是所有的if / else if行? 也许更好的变量存储? 我显然是个初学者,所以答案很可能是我不知道从哪里开始的最简单的事情。 感谢您的任何意见。

class autoInverter {
    public static void main(String[] args) {

        int moduleCount = 53;
        int moduleRemainder;
        int secondInverter;

        int se3800hMax = 18;
        int se7600hMax = 37;
        int se10000hMax = 49;

        String se3800h = "SE300H Inverter";
        String se7600h = "SE7600H Inverter";
        String se10000h = "SE10000H Inverter";

        System.out.println("Total modules: " + moduleCount);

        if (moduleCount <= se3800hMax) {
            System.out.println(se3800h + ": " + moduleCount + " modules");
        } else if (moduleCount <= se7600hMax) {
            System.out.println(se7600h + ": " + moduleCount + " modules");
        } else if (moduleCount <= se10000hMax) {
            System.out.println(se10000h + ": " + moduleCount + " modules");
        } else if (moduleCount > se7600hMax * 2) {
            moduleRemainder = moduleCount % 2;
            secondInverter = moduleCount / 2 + moduleRemainder;
            System.out.println(se10000h + ": " + moduleCount / 2 + " modules");
            System.out.println(se10000h + ": " + secondInverter + " modules");
        } else if (moduleCount > se10000hMax) {
            moduleRemainder = moduleCount % 2;
            secondInverter = moduleCount / 2 + moduleRemainder;
            System.out.println(se7600h + ": " + moduleCount / 2 + " modules");
            System.out.println(se7600h + ": " + secondInverter + " modules");
        }


    }
}

这将打印出来:

总模块:53

SE7600H逆变器:26个模块

SE7600H逆变器:27个模块

如果你创建了一个具有numberOfModules和price的“逆变器”类,你可能会创建一个“最适合”算法,它绕过它们并选择给定数量的逆变器的最低价格 - 但我猜这是过度的。 我认为你所拥有的可能是最容易阅读和维护的(大多数代码最重要的方面)。

如果您只是想让现有的解决方案更加模块化,那么您可以创建更简化的内容......

创建一组5个“解决方案”对象。 每种解决方案都有最多可处理的模块和可以处理它们的逆变器配置。 迭代这些解决方案,直到你找到一个足以处理当前模块数量的解决方案。

然后,您将获得可用的逆变器的数量和类型,因此只需将模块划分为它们(而不仅仅是编码1或2,我会找到一种方法将模块划分为n个逆变器,以便将来扩展。)

那有意义吗? 可以从文本配置文件创建5个解决方案对象,以便您可以轻松添加新解决方案而无需重建。

“高效”是一个非常宽松的术语。 如果您的意思是性能,我认为还可以。 如果你的意思是模块化,那么我会做这样的事情:

public class Main {
    public static void main(String[] args) {

        InverterFactory factory = new InverterFactory();
        CountingSystem countingSystem = new CountingSystem(53);
        Inverter myInverter = factory.create("SE300H");

        countingSystem.calculate(myInverter);

    }
}

public class InverterFactory {

    public static Inverter create(String inverterType) {

        if (inverterType.equals("SE300H")) {
            return new InverterTypeA();
        } else if (inverterType.equals("SE7600H")) {
            return new InverterTypeB();
        } else if (inverterType.equals("SE1000H")) {
            return new InverterTypeC();
        }
    }
}

public interface Inverter {
}

public class InverterTypeA implements Inverter {

    private final int max;

    public InverterTypeA() {
        this.max = 18;
    }

    public int getMax() {
        return this.max;
    }

    @Override
    public String toString() {
        return "SE300H Inverter";
    }
}

public class CountingSystem {

    private final int moduleCount;

    public CountingSystem(int moduleCount) {
        this.moduleCount = moduleCount;
    }

    public void calculate(Inverter inverter) {

        if (moduleCount  <= inverter.getMax()) {
            System.out.println(inverter.toString() + ": " + moduleCount + " modules");
        }
        //etc
    }
}

显然,在您的用例中,应该更改方法/类名称。 此外,3种不同类型的逆变器类可能过度,但它确实使您的代码在以后需要添加更多类型时更具可扩展性; 工厂也使测试变得更加容易。

暂无
暂无

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

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