第20条:クラス階層がラベルクラスより優れている

1943 ワード

2つ以上のスタイルのインスタンスを持つクラスに遭遇し、インスタンススタイルを表すラベルドメインが含まれる場合があります.Demo:
// Tagged class - vastly inferior to a class hierarchy!
class Figure {
    enum Shape { RECTANGLE, CIRCLE };

    // Tag field - the shape of this figure
    final Shape shape;

    // These fields are used only if shape is RECTANGLE
    double length;
    double width;

    // This field is used only if shape is CIRCLE
    double radius;

    // Constructor for circle
    Figure(double radius) {
        shape = Shape.CIRCLE;
        this.radius = radius;
    }

    // Constructor for rectangle
    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();
        }
    }
}
これらのラベルクラス(tagged class)には多くの利点があるが、可読性を破壊する.
ラベルクラスは冗長すぎて、エラーが発生しやすく、効率が低下します.
複数のスタイルオブジェクトを表すデータ型:サブタイプ化(subtyping).ラベルクラスは、クラス階層の簡単なシミュレーションです.
Demo:
// Class hierarchy replacement for a tagged class
abstract class Figure {
    abstract double area();
}
class Circle extends Figure {
    final double radius;

    Circle(double radius) { this.radius = radius; }

    double area() { return Math.PI * (radius * radius); }
}
class Rectangle extends Figure {
    final double length;
    final double width;

    Rectangle(double length, double width) {
        this.length = length;
        this.width  = width;
    }
    double area() { return length * width; }
}
class Square extends Rectangle {
    Square(double side) {
        super(side, side);
    }
}

ラベルクラスが適用されることはめったにありません.表示ラベルドメインを含むクラスを作成する場合は、このラベルが取り消されるかどうか、クラスがクラス階層で代用できるかどうかを考慮する必要があります.ラベルと既存のクラスに遭遇した場合は、階層に再構築することを考慮します.