如果唯一的区别是for循环中的方法调用,请避免重复代码

我有两种用Java编写的方法:

npm cache clean --force
public void fillRect(float x,float y,float width,float height,Color color) {
        int xi = mapX(x);
        int yi = mapY(y);

        int heightf =  mapHeight(height);
        int widthf  = mapWidth(width);


        if (xi + widthf > pixelWidth){
            widthf -= xi + widthf - pixelWidth;
        }
        if (yi + heightf > pixelHeight){
            heightf -= yi + heightf - pixelHeight;
        }


        if (xi < 0) {
            widthf += xi;
            xi = 0;

        }
        if (yi < 0) {
            heightf += yi;
            yi = 0;
        }

        for (int xx = xi; xx < xi + widthf; xx++){
            for (int yy = yi; yy < yi + heightf; yy++){
                // here is the difference between the other method
                setPixel(xx,yy,color);

            }
        }
    }

我习惯于编写类似validateBoundary(float * x,float * y,float * width,float * height)这样的方法:void包含'if-statements'并调用它,但显然这会成功'不会发生在Java中。 此类问题的解决方案是什么?我们可以编写一个methode validateBoundaryWidthf(xi,widhtf,pixelWitdth)来返回widthf的新值。但类似这样:

public void fillRect(float x,float transparency,Color color) {
        int xi = mapX(x);
        int yi = mapY(y);

        int heightf =  mapHeight(height);
        int widthf  = mapWidth(width);

        if (xi + widthf > pixelWidth){
            widthf -= xi + widthf - pixelWidth;
        }
        if (yi + heightf > pixelHeight){
            heightf -= yi + heightf - pixelHeight;
        }


        if (xi < 0) {
            widthf += xi;
            xi = 0;

        }
        if (yi < 0) {
            heightf += yi;
            yi = 0;
        }

        for (int xx = xi; xx < xi + widthf; xx++){
            for (int yy = yi; yy < yi + heightf; yy++) {
                // here is the difference between the other method
                // this Method is slower then setPixel() 
                plot(xx,transparency,color);
            }
        }
    }

无法通过此方法解决,因为只有一个返回值。当然,我可以创建一个具有widthf和xi属性的POJO,而不是返回this,但是我认为这对于cpu /内存而言代价很高。那么解决此重复代码问题的正确方法是什么?

iCMS 回答:如果唯一的区别是for循环中的方法调用,请避免重复代码

您可以使用使用者来处理for循环内的不同处理。定义一个新的功能接口,该接口以xxyy值作为参数:

@FunctionalInterface
public interface PointConsumer {
    void accept(int x,int y);
}

然后,添加一个新方法performOnPoints,其中包含所有需要的参数和一个PointConsumer参数。可能看起来像这样:

public void performOnPoints(float x,float y,float width,float height,PointConsumer consumer) {
    int xi = mapX(x);
    int yi = mapY(y);

    int heightf =  mapHeight(height);
    int widthf  = mapWidth(width);


    if (xi + widthf > pixelWidth){
        widthf -= xi + widthf - pixelWidth;
    }
    if (yi + heightf > pixelHeight){
        heightf -= yi + heightf - pixelHeight;
    }


    if (xi < 0) {
        widthf += xi;
        xi = 0;

    }
    if (yi < 0) {
        heightf += yi;
        yi = 0;
    }

    for (int xx = xi; xx < xi + widthf; xx++){
        for (int yy = yi; yy < yi + heightf; yy++){
            consumer.accept(xx,yy);
        }
    }
}

然后,您可以像这样重写现有的fillRect方法:

public void fillRect(float x,Color color) {
    performOnPoints(x,y,width,height,(xx,yy) -> setPixel(xx,yy,color));
}

public void fillRect(float x,float transparency,yy) -> plot(xx,transparency,color);
}

如您所见,它们都对所有特殊的if()语句使用相同的循环代码,但是您只有一次此代码。对于不同的参数,您将使用不同的使用者对象,一个将调用setPixel(),另一个将调用plot()

,

您可以定义public void fillRect(bool plot,float x,Color color),其中plot指示应使用哪种实现。如果为false,则使用第一个选项。如果为true,则使用第二个选项。

本文链接:https://www.f2er.com/2240066.html

大家都在问