A common practice is to have arguments in the method signature for the sole purpose of executing the contract

I am a little curious to find out some code that I saw at school, and whether this is common practice in this area or just a bad design.

Consider the following interface and two classes that implement it ...

public abstract interface Anchor
{
    public abstract double x(double x);

    public abstract double y(double y);
}

Note that the Cycle class actually uses the arguments in x () and y () ...

public class Cycle implements Anchor
{
    public Anchor anchor;
    public double radius;
    public double period;
    public double phase = 4.0D;

    public Cycle(Anchor anchor, double radius, double period) {
        this.anchor = anchor;
        this.radius = radius;
        this.period = period;
    }

    public double angle(double day) {
        return this.phase * 3.141592653589793D * (day / this.period) / 2.0D;
    }

    public double x(double x) {
        return this.anchor.x(x) + Math.cos(angle(x)) * this.radius;
    }

    public double y(double y) {
        return this.anchor.y(y) + Math.sin(angle(x)) * this.radius;
    }
}

But here, in the Center class, the arguments in x () and y () exist solely to make contact with the Anchor interface and are not actually used in the method ...

public class Center implements Anchor
{
    public double x;
    public double y;

    public Center(double x, double y) {
        this.x = x;
        this.y = y;
    }

    public double x(double x) { return this.x; }
    public double y(double y) { return this.y; }
}

Is this something you usually see in production Java code? Is this an acceptable practice or a bad job?

+5
source share
3

, .

, , . - , , , , , .

+11

adpalumbo , , , , . .

interface Waffle {
    void iron(int a, int b, int c, int d, int e);
}

class Belgian implements Waffle {
    void iron(int a, int b, int c, int d, int e) {
        doSomethingWith(a);
    }
}

class American implements Waffle {
    void iron(int a, int b, int c, int d, int e) {
        doSomethingElseWith(b);
    }
}

class Scandinavian implements Waffle {
    void iron(int a, int b, int c, int d, int e) {
        andYetAgainWith(c);
    }
}

// etc.

, .

, , , , .

+1

2 , .

  • , , .

  • , 1 . .

  • Public , , , .

  • , .

+1

All Articles