-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathShapeBuildersTest.java
More file actions
33 lines (28 loc) · 1.51 KB
/
ShapeBuildersTest.java
File metadata and controls
33 lines (28 loc) · 1.51 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
/**
* This example shows how to implement inheritable builders and their usage
*/
public class ShapeBuildersTest {
// Set common attributes shared by all the shape builders
public static void setShapeAttributes(Shape.Builder<? extends Shape.Builder> shapeBuilder) {
shapeBuilder.color(Shape.Color.YELLOW).name("Shape");
}
public static void main(String[] args) {
Ellipse.Builder ellipseBuilder = new Ellipse.Builder();
ellipseBuilder.majorRadius(3).color(Shape.Color.BLUE).name("Ellipse1").minorRadius(1);
Ellipse ellipse = ellipseBuilder.build();
// Ellipse: name = Ellipse1, color = BLUE, major radius = 3.0, minor radius = 1.0
System.out.println(ellipse);
Shape.Builder<Rectangle.Builder> rectangleBuilder = new Rectangle.Builder();
// Call the setters -- order matters, setters of Shape.Builder must come first!
rectangleBuilder.color(Shape.Color.RED).name("Rectangle1").height(2.3f).width(5.6f);
Shape shapeRectangle = rectangleBuilder.build();
// Rectangle: name = Rectangle1, color = RED, height = 2.3, width = 5.6
System.out.println(shapeRectangle);
setShapeAttributes(ellipseBuilder);
// Ellipse: name = Shape, color = YELLOW, major radius = 3.0, minor radius = 1.0
System.out.println(ellipseBuilder.build());
setShapeAttributes(rectangleBuilder);
// Rectangle: name = Shape, color = YELLOW, height = 2.3, width = 5.6
System.out.println(rectangleBuilder.build());
}
}