Behavioural Design Pattern: Visitor Pattern - BunksAllowed

BunksAllowed is an effort to facilitate Self Learning process through the provision of quality tutorials.

Community

Behavioural Design Pattern: Visitor Pattern

Share This





Source code of NodeVisitor.java
package com.t4b.test.java.dp.bp.vp; interface NodeVisitor { public void visit(TreeNode n); }
Source code of TreeNode.java
package com.t4b.test.java.dp.bp.vp; class TreeNode { private String name; public TreeNode(String name) { this.name = name; } public String getName() { return name; } public void accept(NodeVisitor v) { v.visit(this); } }
Source code of EmailVisitor.java
package com.t4b.test.java.dp.bp.vp; class EmailVisitor implements NodeVisitor { @Override public void visit(TreeNode n) { System.out.println("email:" + n.getName()); } }
Source code of ConsoleVisitor.java
package com.t4b.test.java.dp.bp.vp; class ConsoleVisitor implements NodeVisitor { @Override public void visit(TreeNode n) { System.out.println("console:" + n.getName()); } }
Source code of TestMain.java
package com.t4b.test.java.dp.bp.vp; public class TestMain { public static void main(String[] args) { TreeNode node = new TreeNode("tutorials4beginner.com"); node.accept(new ConsoleVisitor()); node.accept(new EmailVisitor()); } }

Happy Exploring!

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.