January Star
  • Home
  • Categories
  • Tags
  • Archives

Good Visitor


 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
class ShadowedCartesianPt extend CartesianPt {
    int tx;
    int ty;
    ShadowedCartesianPt(int _x, int _y, int _tx, int _ty) {
        super(_x, _y);
        tx = _tx;
        ty = _ty;
    }
    int distanceTo0() {
        return super.distanceTo0()
               +
               (int)Math.sqrt(tx * tx + ty * ty);
    }
}
 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
34
35
36
37
38
39
40
41
interface ShapeVisitorI {
    boolean forCircle(int r);
    boolean forSquare(int s);
    boolean forTrans(PointD q, ShapeD s);
}

abstract class ShapeD {
    abstract boolean accept(ShapeVisitorI ask);
}

class Circle extend ShapeD { // 圆心在坐标原点的圆 
    int r;
    Circle(int _r) {
        r = _r;
    }
    boolean accept(ShapeVisitorI ask) {
        return ask.forCircle(r);
    }
}

class Square extend ShapeD { // 左上角在坐标原点的正方形 
    int r;
    Square(int _r) {
        r = _r;
    }
    boolean accept(ShapeVisitorI ask) {
        return ask.forSquare(r);
    }
}

class Trans extend ShapeD { // 在指定位置的图形,
    PointD q;
    ShapeD s;
    Trans(PointD _q, ShapeD _s) {
        q = _q;
        s = _s;
    }
    boolean accept(ShapeVisitorI ask) {
        return ask.forTrans(q, s);
    }
}
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
// 检查某个点是否在图形内部 
class HasPtV implements ShapeVisitorI {
    PointD p;
    HasPtV(PointD _p) {
        p = _p;
    }
    public boolean forCircle(int r) {
        return p.distanceTo0() <= r;
    }
    public boolean forSquare(int s) {
        return p.x <= s && p.y <= s;
    }
    public boolean forTrans(PointD q, ShapeD s) {
        return s.accept(new HasPtV(p.minus(q)));
    }
}
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
class Union extends ShapeD {
    ShapeD s;
    ShapeD t;
    Union(ShapeD _s, ShapeD _t) {
        s = _s;
        t = _t;
    }
    boolean accept(ShapeVisitorI ask) {
        ((UnionVisitorI)ask).forUnion(s, t);
    }
}

interface UnionVisitorI extends ShapeVisitorI {
    boolean forUnion(ShapeD s, ShapeD t);
}

class UnionHasPtV extends HasPtV implements ShapeVisitorI {
    UnionHasPtV(PointD _p) {
        super(_p);
    }
    public boolean forUnion(ShapeD s, ShapeD t) {
        return s.accept(this) || t.accept(this);
    }
}
 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
class HasPtV implements ShapeVisitorI {
    PointD p;
    HasPtV(PointD _p) {
        p = _p;
    }
    ShapeVisitorI newHasPt(PointD p) {
        return new HasPtV(p);
    }
    public boolean forCircle(int r) {
        return p.distanceTo0() <= r;
    }
    public boolean forSquare(int s) {
        return p.x <= s && p.y <= s;
    }
    public boolean forTrans(PointD q, ShapeD s) {
        return s.accept(newHasPtV(p.minus(q)));
    }
}

class UnionHasPtV extends HasPtV implements UnionVisitorI {
    UnionHasPtV(PointD _p) {
        super(_p);
    }
    ShapeVisitorI newHasPt(PointD p) {
        return new UnionHasPtV(p);
    }
    public boolean forUnion(ShapeD s, ShapeD t) {
        return s.accept(this) || t.accept(this);
    }
}

第九条建议

如果一个类必须被扩展, 为了前瞻性,使用一个类似于构造方法的方法,这样访问者类被扩展时该类能够被重载。

Comments
comments powered by Disqus

Published

Sep 25, 2014

Category

java

Tags

  • java 13
  • oop 13

Contact

  • Powered by Pelican. Theme: Elegant by Talha Mansoor