Created
May 20, 2019 06:45
-
-
Save hackjutsu/ff7937049fac404b58b8ab6c9df050d1 to your computer and use it in GitHub Desktop.
[medium snippets] #medium #designPattern #BuilderPattern
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 class StreetMap { | |
private final Point origin; | |
private final Point destination; | |
private final Color waterColor; | |
private final Color landColor; | |
private final Color highTrafficColor; | |
private final Color mediumTrafficColor; | |
private final Color lowTrafficColor; | |
public static class Builder { | |
private final Point origin; | |
private final Point destination; | |
private Color waterColor = Color.BLUE; | |
private Color landColor = new Color(30, 30, 30); | |
private Color highTrafficColor = Color.RED; | |
private Color mediumTrafficColor = Color.YELLOW; | |
private Color lowTrafficColor = Color.GREEN; | |
public Builder(Point origin, Point destination) { | |
this.origin = origin; | |
this.destination = destination; | |
} | |
public Builder waterColor(Color color) { | |
waterColor = color; | |
return this; | |
} | |
public Builder landColor(Color color) { | |
landColor = color; | |
return this; | |
} | |
public Builder highTrafficColor(Color color) { | |
highTrafficColor = color; | |
return this; | |
} | |
public Builder mediumTrafficColor(Color color) { | |
mediumTrafficColor = color; | |
return this; | |
} | |
public Builder lowTrafficColor(Color color) { | |
lowTrafficColor = color; | |
return this; | |
} | |
public StreetMap build() { | |
return new StreetMap(this); | |
} | |
} | |
private StreetMap(Builder builder) { | |
origin = builder.origin; | |
destination = builder.destination; | |
waterColor = builder.waterColor; | |
landColor = builder.landColor; | |
highTrafficColor = builder.highTrafficColor; | |
mediumTrafficColor = builder.mediumTrafficColor; | |
lowTrafficColor = builder.lowTrafficColor; | |
} | |
public static void main(String args[]) { | |
Point origin = new Point(10, 10); | |
Point destination = new Point(50, 50); | |
StreetMap map = new StreetMap.Builder(origin, destination) | |
.landColor(Color.GRAY) | |
.waterColor(Color.BLUE.brighter()) | |
.build(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment