OnJava8-Examples/reuse/CADSystem.java

113 lines
2.4 KiB
Java
Raw Permalink Normal View History

2015-09-07 11:44:36 -06:00
// reuse/CADSystem.java
// (c)2021 MindView LLC: see Copyright.txt
2015-11-15 15:51:35 -08:00
// We make no guarantees that this code is fit for any purpose.
2016-09-23 13:23:35 -06:00
// Visit http://OnJava8.com for more book information.
2016-01-25 18:05:55 -08:00
// Ensuring proper cleanup
2016-07-28 12:48:23 -06:00
// {java reuse.CADSystem}
2015-06-15 17:47:35 -07:00
package reuse;
class Shape {
2016-01-25 18:05:55 -08:00
Shape(int i) {
System.out.println("Shape constructor");
}
void dispose() {
System.out.println("Shape dispose");
}
2015-06-15 17:47:35 -07:00
}
class Circle extends Shape {
Circle(int i) {
super(i);
2015-11-03 12:00:44 -08:00
System.out.println("Drawing Circle");
2015-06-15 17:47:35 -07:00
}
@Override void dispose() {
2015-11-03 12:00:44 -08:00
System.out.println("Erasing Circle");
2015-06-15 17:47:35 -07:00
super.dispose();
}
}
class Triangle extends Shape {
Triangle(int i) {
super(i);
2015-11-03 12:00:44 -08:00
System.out.println("Drawing Triangle");
2015-06-15 17:47:35 -07:00
}
@Override void dispose() {
2015-11-03 12:00:44 -08:00
System.out.println("Erasing Triangle");
2015-06-15 17:47:35 -07:00
super.dispose();
}
}
class Line extends Shape {
private int start, end;
Line(int start, int end) {
super(start);
this.start = start;
this.end = end;
2015-12-02 09:20:27 -08:00
System.out.println(
"Drawing Line: " + start + ", " + end);
2015-06-15 17:47:35 -07:00
}
@Override void dispose() {
2015-12-02 09:20:27 -08:00
System.out.println(
"Erasing Line: " + start + ", " + end);
2015-06-15 17:47:35 -07:00
super.dispose();
}
}
public class CADSystem extends Shape {
private Circle c;
private Triangle t;
private Line[] lines = new Line[3];
public CADSystem(int i) {
super(i + 1);
for(int j = 0; j < lines.length; j++)
lines[j] = new Line(j, j*j);
c = new Circle(1);
t = new Triangle(1);
2015-11-03 12:00:44 -08:00
System.out.println("Combined constructor");
2015-06-15 17:47:35 -07:00
}
@Override public void dispose() {
2015-11-03 12:00:44 -08:00
System.out.println("CADSystem.dispose()");
2015-06-15 17:47:35 -07:00
// The order of cleanup is the reverse
// of the order of initialization:
t.dispose();
c.dispose();
for(int i = lines.length - 1; i >= 0; i--)
lines[i].dispose();
super.dispose();
}
public static void main(String[] args) {
CADSystem x = new CADSystem(47);
try {
// Code and exception handling...
} finally {
x.dispose();
}
}
2015-09-07 11:44:36 -06:00
}
/* Output:
2015-06-15 17:47:35 -07:00
Shape constructor
Shape constructor
Drawing Line: 0, 0
Shape constructor
Drawing Line: 1, 1
Shape constructor
Drawing Line: 2, 4
Shape constructor
Drawing Circle
Shape constructor
Drawing Triangle
Combined constructor
CADSystem.dispose()
Erasing Triangle
Shape dispose
Erasing Circle
Shape dispose
Erasing Line: 2, 4
Shape dispose
Erasing Line: 1, 1
Shape dispose
Erasing Line: 0, 0
Shape dispose
Shape dispose
2015-09-07 11:44:36 -06:00
*/