之前的章节每次都是生成新的实例,然后再对该实例进行操作。
这一章节主要讲到如果修改实例中的属性,然后再进行操作。
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 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 | interface PiemanI {
int addTop(Object t);
int remTop(Object t);
int substTop(Object n, Object o);
int occTop(Object o);
}
class PiemanM implements PiemanI {
PieD p = new Bot();
public int addTop(Object t) {
p = new Top(t, p);
return occTop(t);
}
public int remTop(Object t) {
p = (PieD)p.accept(new RemV(t));
return occTop(t);
}
public int substTop(Object n, Object o) {
p = (PieD)p.accept(new SubstV(n o));
return occTop(n);
}
public int occTop(Object o) {
return ((Integer)p.accept(new OccursV(o))).intValue();
}
}
interface PieVisitorI {
Object forBot();
Object forTop(Object t, PieD r);
}
abstract class PieD {
abstract Object accept(PieVisitorI ask);
}
class Bot extends PieD {
Object accept(PieVisitorI ask) {
return ask.forBot();
}
}
class Top extends PieD {
Object t;
PieD r;
Top(Object _t, Object _r) {
t = _t;
r = _r;
}
Object accept(PieVisitorI ask) {
return ask.forTop(t, r);
}
}
class OccursV implements PieVisitorI {
Object a;
OccursV(Object _a) {
a = _a;
}
public Object forBot() {
return new Integer(0);
}
public Object forTop(Object t, PieD r) {
if (t.equals(a))
return new Integer(((Integer)(r.accept(this))).intValue() + 1);
else
return r.accept(this);
}
}
class SubstV implements PieVisitorI {
Object n;
Object o;
SubstV(Object _n, Object _o) {
n = _n;
o = _o;
}
public Object forBot() {
return new Bot();
}
public Object forTop(Object t, PieD r) {
if (o.equals(t))
return new Top(n, (PieD)r.accept(this));
else
return new Top(t, (PieD)r.accept(this));
}
}
class RemV implements PieVisitorI {
Object o;
RemV(Object _o) {
o = _o;
}
public Object forBot() {
return new Bot();
}
public Object forTop(Object t, PieD r) {
if (o.equals(t))
return r.accept(this);
else
return new Top(t, (PieD)r.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 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 | interface PieVisitorI {
Object forBot(Bot that);
Object forTop(Top that);
}
abstract class PieD {
abstract Object accept(PieVisitorI ask);
}
class Bot extends PieD {
Object accept(PieVisitorI ask) {
return ask.forBot(this);
}
}
class Top extends PieD {
Object t;
PieD r;
Top(Object _t, Object _r) {
t = _t;
r = _r;
}
Object accept(PieVisitorI ask) {
return ask.forTop(this);
}
}
class OccursV implements PieVisitorI {
Object a;
OccursV(Object _a) {
a = _a;
}
public Object forBot(Bot that) {
return new Integer(0);
}
public Object forTop(Top that) {
if (that.t.equals(a))
return new Integer(((Integer)(that.r.accept(this))).intValue() + 1);
else
return that.r.accept(this);
}
}
class SubstV implements PieVisitorI {
Object n;
Object o;
SubstV(Object _n, Object _o) {
n = _n;
o = _o;
}
public Object forBot(Bot that) {
return new Bot();
}
public Object forTop(Top that) {
if (o.equals(that.t))
return new Top(n, (PieD)(that.r).accept(this));
else
return new Top(that.t, (PieD)(that.r).accept(this));
}
}
class RemV implements PieVisitorI {
Object o;
RemV(Object _o) {
o = _o;
}
public Object forBot(Bot that) {
return new Bot();
}
public Object forTop(Top that) {
if (o.equals(that.t))
return that.r.accept(this);
else
return new Top(that.t, (PieD)(that.r).accept(this));
}
}
|
接下来,咱们通过彻底地修改实例的属性,而不是重新生成新的实例,来实现一个访问者。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 | class SubstV implements PieVisitorI {
Object n;
Object o;
SubstV(Object _n, Object _o) {
n = _n;
o = _o;
}
public Object forBot(Bot that) {
return that;
}
public Object forTop(Top that) {
if (o.equals(that.t))
that.t = n;
that.r.accept(this);
return that;
else
that.r.accept(this);
return that;
}
}
|
第十条建议
当必须修改一个对象时,使用一个类来隐藏修改操作,否则它就会对你的整个流程造成影响。
咱们再一个例子,加深一下理解。
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 42 43 44 45 46 47 48 49 50 51 | abstract class PointD{
int x;
int y;
PointD(int _x, int _y){
x = _x;
y = _y;
}
boolean closerTo0(PointD p) {
return distanceTo0() <= p.distanceTo0();
}
PointD minus(PointD p) {
return CartesianPt(x - p.x, y - p.y);
}
int moveBy(int tx, int ty) {
x = x + tx;
y = y + ty;
return distanceTo0();
}
abstract int distanceTo0();
}
class CartesianPt extends PointD{ // 笛卡尔坐标
CartesianPt(int _x, int _y){
super(_x, _y);
}
int distanceTo0(){
return (int)Math.sqrt(x * x + y * y);
}
}
class ManhattanPt extends PointD{ // 曼哈顿坐标
ManhattanPt(int _x, int _y){
super(_x, _y);
}
int distanceTo0(){
return x + y;
}
}
class ShadowedManhattanPt extends ManhattanPt{ // 曼哈顿坐标
int tx;
int ty;
ManhattanPt(int _x, int _y, int _tx, int _ty){
super(_x, _y);
tx = _tx;
ty = _ty;
}
int distanceTo0(){
return super.distanceTo0() + tx + ty;
}
}
|
comments powered by Disqus