Skip to content

Commit fa82332

Browse files
Yalun Qineaglesky
authored andcommitted
Add builder example
1 parent 30c46fa commit fa82332

4 files changed

Lines changed: 138 additions & 0 deletions

File tree

Ellipse.java

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
public class Ellipse extends Shape {
2+
public final float majorRadius;
3+
public final float minorRadius;
4+
5+
private Ellipse(Color color, String name, float majorRadius, float minorRadius) {
6+
super(color, name);
7+
this.majorRadius = majorRadius;
8+
this.minorRadius = minorRadius;
9+
}
10+
11+
public static class Builder extends Shape.Builder<Ellipse.Builder> {
12+
private float majorRadius;
13+
private float minorRadius;
14+
15+
public Builder majorRadius(float majorRadius) {
16+
this.majorRadius = majorRadius;
17+
return this;
18+
}
19+
20+
public Builder minorRadius(float minorRadius) {
21+
this.minorRadius = minorRadius;
22+
return this;
23+
}
24+
25+
@Override
26+
public Ellipse build() {
27+
return new Ellipse(
28+
this.color,
29+
this.name,
30+
this.majorRadius,
31+
this.minorRadius);
32+
}
33+
}
34+
35+
@Override
36+
public String toString() {
37+
return "Ellipse: name = " + name
38+
+ ", color = " + color
39+
+ ", major radius = " + majorRadius
40+
+ ", minor radius = " + minorRadius;
41+
}
42+
43+
44+
}

Rectangle.java

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
public class Rectangle extends Shape {
2+
public final float height;
3+
public final float width;
4+
5+
private Rectangle(Color color, String name, float height, float width) {
6+
super(color, name);
7+
this.height = height;
8+
this.width = width;
9+
}
10+
11+
public static class Builder extends Shape.Builder<Rectangle.Builder> {
12+
private float height;
13+
private float width;
14+
15+
public Builder height(float height) {
16+
this.height = height;
17+
return this;
18+
}
19+
20+
public Builder width(float width) {
21+
this.width = width;
22+
return this;
23+
}
24+
25+
@Override
26+
public Rectangle build() {
27+
return new Rectangle(
28+
this.color,
29+
this.name,
30+
this.height,
31+
this.width);
32+
}
33+
}
34+
35+
@Override
36+
public String toString() {
37+
return "Rectangle: name = " + name
38+
+ ", color = " + color
39+
+ ", height = " + height
40+
+ ", width = " + width;
41+
}
42+
43+
}

Shape.java

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
public abstract class Shape {
2+
public final Color color;
3+
public final String name;
4+
5+
public static enum Color {
6+
RED, YELLOW, BLUE
7+
}
8+
9+
protected Shape(Color color, String name) {
10+
this.color = color;
11+
this.name = name;
12+
}
13+
14+
public static abstract class Builder<SB extends Builder<SB>> {
15+
protected Color color;
16+
protected String name;
17+
18+
public SB color(Color color) {
19+
this.color = color;
20+
return (SB) this;
21+
}
22+
23+
public SB name(String name) {
24+
this.name = name;
25+
return (SB) this;
26+
}
27+
28+
abstract Shape build();
29+
}
30+
}

ShapeBuildersTest.java

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
public class ShapeBuildersTest {
2+
3+
public static void main(String[] args) {
4+
Ellipse.Builder ellipseBuilder = new Ellipse.Builder();
5+
ellipseBuilder.majorRadius(3)
6+
.color(Shape.Color.BLUE)
7+
.name("Ellipse1")
8+
.minorRadius(1);
9+
Ellipse ellipse = ellipseBuilder.build();
10+
System.out.println(ellipse);
11+
12+
Shape.Builder<Rectangle.Builder> rectangleBuilder = new Rectangle.Builder();
13+
//Call the setters -- order matters, setters of Shape.Builder must come first!
14+
rectangleBuilder.color(Shape.Color.RED)
15+
.name("Rectangle1")
16+
.height(2.3f)
17+
.width(5.6f);
18+
Shape shapeRectangle = rectangleBuilder.build();
19+
System.out.println(shapeRectangle);
20+
}
21+
}

0 commit comments

Comments
 (0)