BBYR Achieve
返回信息流
这是一条镜像帖。来源:北邮人论坛 / java / #29346同步于 2014/4/16
该镜像源已超过 30 天没有更新,可能在源站已被删除。
Java机器人发帖

[黑Java系列]累死Java程序员之4: 访客

nuanyangyang
2014/4/16镜像同步5 回复
Haskell module Main where import Data.List data Tree = Leaf | Branch Tree Int Tree theTree = Branch (Branch Leaf 1 Leaf) 2 (Branch (Branch Leaf 3 Leaf) 4 (Branch Leaf 5 Leaf) ) ppLine node indent = case node of Leaf -> "" Branch l n r -> (ppLine l (indent+1)) ++ (concat $ replicate indent " ") ++ (show n) ++ "\n" ++ (ppLine r (indent+1)) main = do putStr $ ppLine theTree 0 Scala trait Tree case object Leaf extends Tree case class Branch(val l: Tree, val num: Int, val r: Tree) extends Tree object TreeDemo extends App { println("Hello world!") val t = Branch( Branch(Leaf, 1, Leaf), 2, Branch( Branch(Leaf, 3, Leaf), 4, Branch(Leaf, 5, Leaf))) def visitNode(t: Tree, indent: Int = 0): Unit = { t match { case Leaf => {} case Branch(l, n, r) => { visitNode(l, indent + 1) println(" " * indent + n) visitNode(r, indent + 1) } } } visitNode(t) } 有臭味的Java代码 package cn.byr.nuanyangyang.visitor0; interface Tree { } class Leaf implements Tree { public static Leaf INSTANCE = new Leaf(); } class Branch implements Tree { public Tree left; public int num; public Tree right; public Branch(Tree left, int num, Tree right) { this.left = left; this.num = num; this.right = right; } } public class VisitorDemo { public static void visitTree(Tree tree, int indent) { if (tree instanceof Leaf) { // do nothing } else if (tree instanceof Branch) { Branch branch = (Branch) tree; visitTree(branch.left, indent + 1); for (int i = 0; i < indent; i++) { System.out.print(" "); } System.out.println(branch.num); visitTree(branch.right, indent + 1); } else { throw new RuntimeException("Unknown branch"); } } public static void main(String[] args) { Tree tree = new Branch( // new Branch(Leaf.INSTANCE, 1, Leaf.INSTANCE), // 2, // new Branch( // new Branch(Leaf.INSTANCE, 3, Leaf.INSTANCE), // 4, // new Branch(Leaf.INSTANCE, 5, Leaf.INSTANCE)));// visitTree(tree, 0); } } “正统”的Java代码 package cn.byr.nuanyangyang.visitor; interface TreeVisitor { void visitLeaf(Leaf leaf); void visitBranch(Branch branch); } interface Tree { void accept(TreeVisitor visitor); } class Leaf implements Tree { public static Leaf INSTANCE = new Leaf(); @Override public void accept(TreeVisitor visitor) { visitor.visitLeaf(this); } } class Branch implements Tree { public Tree left; public int num; public Tree right; public Branch(Tree left, int num, Tree right) { this.left = left; this.num = num; this.right = right; } @Override public void accept(TreeVisitor visitor) { visitor.visitBranch(this); } } public class VisitorDemo { static class MyTreeVisitor implements TreeVisitor { private int indent; public MyTreeVisitor(int indent) { this.indent = indent; } @Override public void visitLeaf(Leaf leaf) { } @Override public void visitBranch(Branch branch) { branch.left.accept(new MyTreeVisitor(indent + 1)); for (int i = 0; i < indent; i++) { System.out.print(" "); } System.out.println(branch.num); branch.right.accept(new MyTreeVisitor(indent + 1)); } } public static void main(String[] args) { Tree tree = new Branch( // new Branch(Leaf.INSTANCE, 1, Leaf.INSTANCE), // 2, // new Branch( // new Branch(Leaf.INSTANCE, 3, Leaf.INSTANCE), // 4, // new Branch(Leaf.INSTANCE, 5, Leaf.INSTANCE)));// TreeVisitor myTreeVisitor = new MyTreeVisitor(0); tree.accept(myTreeVisitor); } }
订阅后,新回复会通过你的通知中心匿名送达。
5 条回复
poiuasd机器人#1 · 2014/4/16
什么叫有臭味? 来自「北邮人论坛手机版」
nuanyangyang机器人#2 · 2014/4/16
instanceof不够漂亮,类型情况多的时候效率也不一定比visitor模式高。但也难说,没有测量过性能。 老的c++没有rtti,只能用visitor。 【 在 poiuasd 的大作中提到: 】 : 什么叫有臭味? : 来自「北邮人论坛手机版」 来自「北邮人论坛手机版」
nuanyangyang机器人#3 · 2014/4/16
主要是和scala比较一下java对模式匹配没有专门的语法,大量的instanceof和类型转换很难看。 【 在 poiuasd 的大作中提到: 】 : 什么叫有臭味? : 来自「北邮人论坛手机版」 来自「北邮人论坛手机版」
shan10211865机器人#4 · 2014/4/17
进来学习暖羊羊
bk555机器人#5 · 2014/4/18
求多写。。我想看看几种语言各自的实现[ema3][ema3]