method overloading - Hierarchy issue on Composite with Java -


i'm trying use composite pattern on java in order report, , i'm forgetting how hierarchy , method overload work.

let's have following models:

public class product {      public string get(){         return "product";     }  }  public class bproduct extends product {      public string getb() {         return "bbbbb";     }  }  public class cproduct extends product {      public string getc() {         return "ccccc";     }  } 

and following converters:

import java.util.list;  public class converter {      private list<converter> converters;      public converter() {      }      public converter(list<converter> converters) {         this.converters = converters;     }      public void execute(product product) {         (converter converter : converters) {             converter.execute(product);         }     }  }  public class bconverter extends converter {      @override     public void execute(product product) {         innerexecute(product);     }      public void innerexecute(product product) {         system.out.println(product.get() + " done on b normal.");     }      public void innerexecute(bproduct b) {         system.out.println(b.getb() + " done on b special.");     }  }  public class cconverter extends converter {      @override     public void execute(product product) {         system.out.println(product.get() + " done on c normal.");     }      public void execute(cproduct c) {         system.out.println(c.getc() + " done on c special.");     }  } 

testing following test:

import java.util.arraylist; import java.util.list;  import org.junit.test;  public class productconvertertest {      @test     public void test() {         list<converter> converters = new arraylist<>();         converters.add(new bconverter());         converters.add(new cconverter());         converter converter = new converter(converters);          list<product> list = new arraylist<>();         list.add(new product());         list.add(new bproduct());         list.add(new cproduct());          (product product : list) {             converter.execute(product);         }     } } 

gets output:

product done on b normal. product done on c normal. product done on b normal. product done on c normal. product done on b normal. product done on c normal. 

when want is:

product done on b normal. product done on c normal. bbbbb done on b special. product done on c normal. product done on b normal. ccccc done on c special. 

note: want without using instanceof. know how it. want know if can done without it.

the reason behaviour #execute method overloaded. choice of overloaded method invoke made @ compile time. in order make work want have use dynamic selection, lead either instanceof check or overwriting methods or pass/retrieve class of product , compare like:

public class converter {      private list<converter> converters;      public converter() {      }      public converter(list<converter> converters) {         this.converters = converters;     }      public void execute(product product) {         (converter converter : converters) {             converter.execute(product);         }     }  }  public class bconverter extends converter {      @override     public void execute(product product) {         if (product.getclass() == bproduct.class) {             innerexecute((bproduct)product);         } else {             innerexecute(product);         }     }      public void innerexecute(product product) {         system.out.println(product.get() + " done on b normal.");     }      public void innerexecute(bproduct b) {         system.out.println(b.getb() + " done on b special.");     }  }  public class cconverter extends converter {      @override     public void execute(product product) {         if (product.getclass() == cproduct.class) {             innerexecute((cproduct)product);         } else {             innerexecute(product);         }     }      public void innerexecute(product product) {         system.out.println(product.get() + " done on c normal.");     }      public void innerexecute(cproduct b) {         system.out.println(b.getc() + " done on c special.");     }  } 

Comments

Popular posts from this blog

javascript - jQuery: Add class depending on URL in the best way -

caching - How to check if a url path exists in the service worker cache -

Redirect to a HTTPS version using .htaccess -