Created
August 15, 2019 02:29
-
-
Save phamducminh/18e3388ca94e42ba69cc5e29fafe8d2d to your computer and use it in GitHub Desktop.
Builder pattern sample code
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public final class Coordinate { | |
final int x; | |
final int y; | |
public static Builder builder() { | |
return new Builder(); | |
} | |
private Coordinate(Builder builder) { | |
this.x = builder.x; | |
this.y = builder.y; | |
} | |
public int getX() { | |
return x; | |
} | |
public int getY() { | |
return y; | |
} | |
public static final class Builder { | |
int x; | |
int y; | |
private Builder() { | |
} | |
public Builder setX(int x) { | |
this.x = x; | |
return this; | |
} | |
public Builder setY(int y) { | |
this.y = y; | |
return this; | |
} | |
public Coordinate build() { | |
return new Coordinate(this); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment