这一章节就针对上一章节暴露的问题,提出的了解决方法:
访问者模式
ShishD
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 OnlyOnionsV {
boolean forSkewer() {
return true;
}
boolean forOnion(ShishD s) {
return s.onlyOnions();
}
boolean forLamb(ShishD s) {
return false;
}
boolean forTomato(ShishD s) {
return false;
}
}
class IsVegetarianV {
boolean forSkewer() {
return ture;
}
boolean forOnion(ShishD s) {
return s.IsVegetarian();
}
boolean forLamb(ShishD s) {
return false;
}
boolean forTomato(ShishD s) {
return s.IsVegetarian();
}
}
|
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 | // 使用访问者模式的 ShishD
// 这里可以对比一下第二章节的 ShishD 代码
abstract class ShishD { // 羊肉
OnlyOnionsV ooFn = new OnlyOnionsV();
IsVegetarianV ivFn = new IsVegetarianV();
abstract boolean onlyOnions();
abstract boolean IsVegetarian();
}
class Skewer extends ShishD { // 串
boolean onlyOnions() {
return ooFn.forSkewer();
}
boolean IsVegetarian() {
return ivFn.forSkewer();
}
}
class Onion extends ShishD { // 洋葱
ShishD s;
Onion (ShishD _s) {
s = _s;
}
boolean onlyOnions() {
return ooFn.forOnion(s);
}
boolean IsVegetarian() {
return ivFn.forOnion(s);
}
}
class Lamb extends ShishD { // 羔羊肉
ShishD s;
Lamb (ShishD _s) {
s = _s;
}
boolean onlyLambs() {
return ooFn.forLamb(s);
}
boolean IsVegetarian() {
return ivFn.forLamb(s);
}
}
class Tomato extends ShishD { // 西红柿
ShishD s;
Tomato (ShishD _s) {
s = _s;
}
boolean onlyTomatos() {
return ooFn.forTomato(s);
}
boolean IsVegetarian() {
return ivFn.forTomato(s);
}
}
|
第四条建议
当需要给多个来自于同一个自引用的数据类型的变体类型编写方法时,使用访问者模式,这样所有的方法就会集中一个类中。
PizzaD
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 | abstract class PizzaD { // 披萨饼
RemAV remFn = new RemAV();
TopAwCV topFn = new TopAwCV();
SubAbCV subFn = new SubAbCV();
abstract PizzaD remA();
abstract PizzaD topAwC();
abstract PizzaD subAbC();
}
class Crust extends PizzaD { // 面包皮
PizzaD remA() {
return remFn.forCrust();
}
PizzaD topAwC() {
return topFn.forCrust();
}
PizzaD subAbC() {
return subFn.forCrust();
}
}
class Cheese extends PizzaD { // 奶酪
PizzaD p;
Cheese(PizzaD _p) {
p = _p;
}
PizzaD remA() {
return remFn.forCheese(p);
}
PizzaD topAwC() {
return topFn.forCheese(p);
}
PizzaD subAbC() {
return subFn.forCheese(p);
}
}
Classr Olive extends PizzaD { // 橄榄
PizzaD p;
Olive(PizzaD _p) {
p = _p;
}
PizzaD remA() {
return remFn.forOlive(p);
}
PizzaD topAwC() {
return topFn.forOlive(p);
}
PizzaD subAbC() {
return subFn.forOlive(p);
}
}
class Anchovy extends PizzaD { // 凤尾鱼
PizzaD p;
Anchovy(PizzaD _p) {
p = _p;
}
PizzaD remA() {
return remFn.forAnchovy(p);
}
PizzaD topAwC() {
return topFn.forAnchovy(p);
}
PizzaD subAbC() {
return subFn.forAnchovy(p);
}
}
class Sausage extends PizzaD { // 香肠
PizzaD p;
Sausage(PizzaD _p) {
p = _p;
}
PizzaD remA() {
return remFn.forSausage(p);
}
PizzaD topAwC() {
return topFn.forSausage(p);
}
PizzaD subAbC() {
return subFn.forSausage(p);
}
}
|
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 | class RemAV {
PizzaD forCrust() {
return new Crust();
}
PizzaD forCheese(PizzaD p) {
return new Cheese(p.remA());
}
PizzaD forOlive(PizzaD p) {
return new Olive(p.remA());
}
PizzaD forAnchovy(PizzaD p) {
return p.remA();
}
PizzaD forSausage(PizzaD p) {
return new Sausage(p.remA());
}
}
class TopAwCV {
PizzaD forCrust() {
return new Crust();
}
PizzaD forCheese(PizzaD p) {
return new Cheese(p.topAwC());
}
PizzaD forOlive(PizzaD p) {
return new Olive(p.topAwC());
}
PizzaD forAnchovy(PizzaD p) {
return new Cheese(new Anchovy(p.topAwC()));
}
PizzaD forSausage(PizzaD p) {
return new Sausage(p.topAwC());
}
}
class SubAbCV {
PizzaD forCrust() {
return new Crust();
}
PizzaD forCheese(PizzaD p) {
return new Cheese(p.subAbC());
}
PizzaD forOlive(PizzaD p) {
return new Olive(p.subAbC());
}
PizzaD forAnchovy(PizzaD p) {
return new Cheese(p.subAbC());
}
PizzaD forSausage(PizzaD p) {
return new Sausage(p.subAbC());
}
}
|
comments powered by Disqus