Skip to content

Latest commit

 

History

History
119 lines (99 loc) · 1.99 KB

File metadata and controls

119 lines (99 loc) · 1.99 KB

Factories

Another interesting use of static methods is what we would call a "factory" method.

Say you have a class like the following.

class Position {
    int x;
    int y;

    Position(int x, int y) {
        this.x = x;
        this.y = y;
    }
}
~void main() {}

It would be reasonable to want to add an overloaded constructor for when y is 0.

class Position {
    int x;
    int y;

    Position(int x, int y) {
        this.x = x;
        this.y = y;
    }

    Position(int x) {
        this.x = x;
        this.y = 0;
    }
}
~void main() {}

But now it is impossible to add a constructor that works for when x is 0.

class Position {
    int x;
    int y;

    Position(int x, int y) {
        this.x = x;
        this.y = y;
    }

    Position(int x) {
        this.x = x;
        this.y = 0;
    }

    Position(int y) {
        this.x = 0;
        this.y = y;
    }
}
~void main() {}

Using a static method to create a Position - i.e. as a "factory" - is a way around the issue.1

class Position {
    int x;
    int y;

    Position(int x, int y) {
        this.x = x;
        this.y = y;
    }

    static Position fromX(int x) {
        return new Position(x, 0);
    }

    static Position fromY(int y) {
        return new Position(0, y);
    }
}
~class Position {
~    int x;
~    int y;
~
~    Position(int x, int y) {
~        this.x = x;
~        this.y = y;
~    }
~
~    static Position fromX(int x) {
~        return new Position(x, 0);
~    }
~
~    static Position fromY(int y) {
~        return new Position(0, y);
~    }
~}
~
class Main {
    void main() {
        var p1 = new Position(1, 2);
        var p2 = Position.fromX(4);
        var p3 = Position.fromY(5);

        IO.println(p1.x + ", " + p1.y);
        IO.println(p2.x + ", " + p2.y);
        IO.println(p3.x + ", " + p3.y);
    }
}

Footnotes

  1. This won't work if you defined Position inside the anonymous main class. I'll tell you why later.