<< Chapter < Page Chapter >> Page >
Hiding Named Helper Visitor inside of ToString3 Comments
package listFW.visitor; import listFW.*;public class ToString3WithHiddenHelper implements IListAlgo { public static finalToString3WithHiddenHelper Singleton = new ToString3WithHiddenHelper();private ToString3WithHiddenHelper() { }
private static class HiddenHelper implements IListAlgo { public static final HiddenHelper Singleton= new HiddenHelper();private HiddenHelper() { }public Object emptyCase(IMTList host, Object... nu) {return ")"; }public Object nonEmptyCase(INEList host, Object... nu) { return ", " + host.getFirst()+ host.getRest().execute(this); }}
public Object emptyCase(IMTList host, Object... nu) { return "()";} public Object nonEmptyCase(INEList host, Object...bu) {return "(" + host.getFirst() + host.getRest().execute(HiddenHelper.Singleton);} }

3. anonymous helpers

Anonymous Helper Visitor inside of ToString1 Comments
package listFW.visitor; import listFW.*;public class ToString1WithAnonymousHelper implements IListAlgo { public static finalToString1WithAnonymousHelper Singleton = new ToString1WithAnonymousHelper();private ToString1WithAnonymousHelper() { }
private static final IListAlgo AnonymousHelper = new IListAlgo() { public Object emptyCase(IMTList host, Object... acc) {return acc[0] + ")";} public Object nonEmptyCase(INEList host, Object... acc) {return host.getRest().execute(this, acc[0]+ ", " + host.getFirst()); }}; // PAY ATTENTION TO THE SEMI-COLON HERE!
public Object emptyCase(IMTList host, Object... nu) { return "()";} public Object nonEmptyCase(INEList host, Object... nu) {return host.getRest().execute(AnonymousHelper, "(" + host.getFirst());} }
Anonymous Helper Visitor inside of ToString2 Comments
package listFW.visitor; import listFW.*;public class ToString2WithAnonymousHelper implements IListAlgo { public static finalToString2WithAnonymousHelper Singleton = new ToString2WithAnonymousHelper();private ToString2WithAnonymousHelper() { }
private static final IListAlgo AnonymousHelper = new IListAlgo() { public Object emptyCase(IMTList host, Object... acc) {return "(" + acc[0] + ")";} public Object nonEmptyCase(INEList host, Object... acc) {return host.getRest().execute(this, acc[0]+ ", " + host.getFirst()); }}; // PAY ATTENTION TO THE SEMI-COLON HERE!
public Object emptyCase(IMTList host, Object... nu) { return "()";} public Object nonEmptyCase(INEList host, Object... nu) {return host.getRest().execute(AnonymousHelper, host.getFirst().toString());} }
Anonymous Helper Visitor inside of ToString3 Comments
package listFW.visitor; import listFW.*;public class ToString3WithAnonymousHelper implements IListAlgo { public static finalToString3WithAnonymousHelper Singleton = new ToString3WithAnonymousHelper();private ToString3WithAnonymousHelper() { }
private static final IListAlgo AnonymousHelper = new IListAlgo() { public Object emptyCase(IMTList host, Object... nu) {return ")"; }public Object nonEmptyCase(INEList host, Object... nu) { return ", " + host.getFirst()+ host.getRest().execute(this); }}; // PAY ATTENTION TO THE SEMI-COLON HERE!
public Object emptyCase(IMTList host, Object... nu) { return "()";}public Object nonEmptyCase(INEList host, Object... nu) { return "(" + host.getFirst()+ host.getRest().execute(AnonymousHelper); }}

Get Jobilize Job Search Mobile App in your pocket Now!

Get it on Google Play Download on the App Store Now




Source:  OpenStax, Principles of object-oriented programming. OpenStax CNX. May 10, 2013 Download for free at http://legacy.cnx.org/content/col10213/1.37
Google Play and the Google Play logo are trademarks of Google Inc.

Notification Switch

Would you like to follow the 'Principles of object-oriented programming' conversation and receive update notifications?

Ask