首页 > 解决方案 > 如何在定义后立即按顺序重用代码?

问题描述

我想连续执行许多简单的操作。在java中,我偶尔会使用方法作为过程,像这样:

doA(x);
doB(x, 1);
doB(x, 2);
doC(x);

它做了我想做的一切,但很难看,而且事件的顺序也不是很清楚。我经常这样做:

(A part I)
(A part II)
(B part I)
(B part II)
(B part III)
(C part I)
(C part II)
(C part III)
(C part IV)

这对于理解代码非常有用,因为如果操作很简单(例如,带有一点流控制的字符串操作),您可以准确地看到计算机执行的事件序列。
当我想做 B 然后做一些与 B 类似但有点不同的事情时,问题就来了。我想重用代码,以防我需要对 B 的两个实例进行更改。在其他语言中,我经常将该代码留在中间,但将其放入一个小的 lambda 并使用不同的参数执行两次。它可能看起来像这样:

(A part I)
(A part II)
define makeB:
    (B part I)
    (B part II)
    (B part III)
x = makeB(true);
y = makeB(false);
(C part I)
(C part II)
(C part III)
(C part IV)

This is readable top-to-bottom and it is clear how B is just a part of this one process and doesn't belong anywhere else.
Are java lambdas applicable for this? The grand goal behind lambdas in java seems to be something completely different from my goal. If they are applicable, how do I use them for simple code, like string manipulation? If they are not, are there any other tools that remedy the problem I expressed in the very first approach?

标签: java

解决方案


Reading about lambdas online, especially in tutorials (both official by Oracle and unofficial) gave me a wrong impression, since most were about making a new functional interface for a specialized task. Thanks to user kaya3 for questioning my conclusion.

I've found out that you can use Function<T,R> from java.util.function for use cases like mine and it doesn't require any further setup.

Function<Boolean, String> makeB = (Boolean bee) -> {...}
x = makeB.apply(true);
y = makeB.apply(false);

The comments asked for actual code and I owe it. Not final but here's how I have currently employed a lambda. It is not for String operations but it's the one example I have at hand. I do plan to actually use it for String operations later.

rootElement.addChildElement(StructureElement.newStructureElement("HiddenTag", false, false, 10f, null, null, MultiplicityEnum.ZERO_OR_MORE,
    Arrays.asList(areaOfUsageHiddenTag, stylisticHiddenTagPattern, otherHiddenTagPattern),
    null, trimCommaTransformers, (List) null)
    .addPrefixElement(nonMandatorySpace)
);

Function<Boolean, StructureElement> newMeaningBlock = (Boolean numberRequired) -> StructureElement.newStructureElementNoParse("MeaningBlock", MultiplicityEnum.ZERO_OR_MORE)
    .addChildElement(StructureElement.newStructureElement("MeaningNumber", true, false, 12f, null, null, numberRequired ? MultiplicityEnum.ONE : MultiplicityEnum.ZERO_OR_ONE, meaningNumberPattern, null, trimCommaTransformers).addPrefixElement(nonMandatorySpace))
    .addChildElement(meaningBlock2)

    .addChildElement(StructureElement.newStructureElementNoParse("AdditionalMeaningBlock", MultiplicityEnum.ZERO_OR_MORE)
            .addChildElement(StructureElement.newStructureElement("MeaningShade", false, false, 12f, null, null, MultiplicityEnum.ONE_OF_MANY, meaningShadePattern, null, trimCommaTransformers)
                    .addPrefixElement(nonMandatorySpace)
                    .addChildElement(shadeMeaningBlock)
            )
            .addChildElement(StructureElement.newStructureElement("BevGender", true, false, 12f, null, null, MultiplicityEnum.ONE_OF_MANY, headerPattern, null, trimCommaTransformers)
                    .addPrefixElement(nonMandatorySpace)
                    .addChildElement(StructureElement.newStructureElement("BevGenderTag", false, false, 10f, null, null, MultiplicityEnum.ONE, bevGenderTagPattern, null, trimCommaTransformers)
                            .addPrefixElement(nonMandatorySpace))
                    .addChildElement(nonMandatorySpace)
                    .addChildElement(StructureElement.newStructureElementNoParse("MeaningIllustrationBlock", MultiplicityEnum.ZERO_OR_MORE)
                            .addChildElement(StructureElement.newStructureElement("MeaningIllustration", false, true, 12f, null, null, MultiplicityEnum.ONE_OF_MANY, meaningIllustrationPattern, null, trimCommaTransformers)
                                    .addPrefixElement(nonMandatorySpace)
                                    .addChildElement(StructureElement.newStructureElement("MeaningIllustrationDef", false, false, 12f, null, null, MultiplicityEnum.ZERO_OR_ONE, meaningIllustrationDefPattern, null, null))
                            )
                            .addChildElement(StructureElement.newStructureElement("Tag", false, false, 10f, null, null, MultiplicityEnum.ONE_OF_MANY,
                                    Arrays.asList(areaOfUsageTag, stylisticTagPattern, otherTagPattern),
                                    null, trimCommaTransformers, (List) null)
                                    .addPrefixElement(nonMandatorySpace)
                                    .addChildElement(nonMandatoryDot)
                            )
                    )
            )
    );

rootElement.addChildElement(newMeaningBlock.apply(false));
rootElement.addChildElement(newMeaningBlock.apply(true));

There are a lot more rootElement.addChildElement(...) statements before and after this, all are just as important, especially when referring to it during debugging. I have left out all but a few of the statements to outline the important part. I could have alternatively used XML or JSON to encode this structure and read it in java but I have opted for this instead and it successfully avoids problems related to just XML or JSON. And this will work in the future for actual code that isn't just creating structures.


推荐阅读