아이템23 태그 달린 클래스보다는 클래스 계층구조를 활용하라
Effective Java 3e 아이템 23를 요약한 내용 입니다.
class Figure P
enum Shape { RECTANGLE, CIRCLE };
...
// 원용 생성자
Figure(double radius) {
shape = Shape.CIRCLE;
this.radius = radius;
}
// 사격형용 생성자
Figure(double length, double width) {
shape = Shape.RECTANGLE;
this.length = length;
this.width = width;
}
double area() {
switch(shape) {
case RECTANGLE:
return length * width;
case CIRCLE:
return Math.PI * (radius * radius);
default:
throw new AssertionError(shape);
}
}태그 달린 클래스에는 단점이 한가득이다.
단점이 한가득이다.서브타이핑
정리
Last updated