How to write the best possible Java code for a similar Ruby functionality? -


i have experience in java , learning ruby. encountered ruby program below:

class tree   attr_accessor :children, :node_name   def initialize(name, children=[])     @children = children     @node_name = name   end   def visit_all(&block)     visit &block     children.each {|c| c.visit_all &block}   end   def visit(&block)     block.call self   end end ruby_tree = tree.new( "ruby" ,                       [tree.new("reia" ),                        tree.new("macruby" )] ) puts "visiting node" ruby_tree.visit {|node| puts node.node_name} puts puts "visiting entire tree" ruby_tree.visit_all {|node| puts node.node_name} 

when looked @ power of ruby language, thought write similar code in java below:

public class tree {      private string name;     private tree[] children;      tree(string name, tree[] children) {         this.name = name;         this.children = children;     }      public string getname() {         return name;     }      public void setname(string name) {         this.name = name;     }      public tree[] getchildren() {         return children;     }      public void setchildren(tree[] children) {         this.children = children;     }      public static void main(string[] args) {         tree mytree = new tree("ruby", new tree[] {                 new tree("reia", new tree[] {}),                 new tree("macruby", new tree[] {}) });         mytree.visit();         mytree.visit_all();     }      public void visit() {         system.out.println(getname());     }      public void visit_all() {         visit();         (tree tree : children) {             tree.visit();         }     } } 

question: know java version here not flexible ruby.is there similar in java can achieve level of flexibility ruby provides?

first, word of caution: code absolutely horrible. provides no encapsulation, leaks implementation details left , right, there's no way tree object can maintain own invariants or state. secondly, doesn't integrate at all ruby's collection framework.

as consequence, java translation also equally horrible, , also doesn't integrate java's collection framework.

the 2 biggest drawbacks java code has compared ruby are

  • in java version, element type hard-coded string, whereas in ruby version, can any object, , mixture of objects within same tree, ,
  • in java version, iterators hard-coded printing name(s), whereas in ruby version, iterators take block argument code execute.

the first problem cannot solved in java. can make collection generic, can hold elements of type, making heterogeneous (i.e. being able hold elements of different types in same collection) going a lot of work. so, stuck partial solution: making tree generic.

the second problem can solved having iterators take object contains code. after all, first-class subroutine same object 1 method. (java 8 going take of pain away, included examples in code.)

import java.util.collection; import java.util.arraylist;  interface consumer<t> {     void accept(t e); } // in java 8, interface part of jre. // replace 3 lines above import: //import java.util.function.consumer;  class tree<t> {     private string nodename;     private collection<tree<t>> children = new arraylist<>();      tree(string name, collection<tree<t>> children) {         nodename = name;         this.children = children;     }      tree(string name) {         nodename = name;     }      public string getnodename() { return nodename; }     public void setnodename(string name) { nodename = name; }      public collection<tree<t>> getchildren() { return children; }     public void setchildren(collection<tree<t>> children) { this.children = children; }      void visitall(consumer<tree<t>> block) {         visit(block);         (tree<t> tree : children) tree.visitall(block);     }      void visit(consumer<tree<t>> block) {         block.accept(this);     }      public static void main(string... args) {         arraylist<tree<string>> children = new arraylist<>();         children.add(new tree<string>("reia"));         children.add(new tree<string>("macruby"));         tree<string> rubytree = new tree<>("ruby", children);          system.out.println("visiting node");         rubytree.visit(new consumer<tree<string>>() {             public void accept(tree<string> node) {                 system.out.println(node.getnodename());             }         });         // in java 8, can use lambda.         // replace 5 lines above line:         //rubytree.visit(node -> system.out.println(node.getnodename()));          system.out.println();          system.out.println("visiting entire tree");         rubytree.visitall(new consumer<tree<string>>() {             public void accept(tree<string> node) {                 system.out.println(node.getnodename());             }         });         // in java 8, can use lambda.         // replace 5 lines above line:         //rubytree.visitall(node -> system.out.println(node.getnodename()));     } } 

Comments

Popular posts from this blog

html - How to style widget with post count different than without post count -

How to remove text and logo OR add Overflow on Android ActionBar using AppCompat on API 8? -

javascript - storing input from prompt in array and displaying the array -