![](https://www.opentextbooks.org.hk/sites/opentextbooks/files/book_profile/cc_icon/by-sa.png)
The most secure way to hide the helper visitor is to move it inside of the main visitor and make it a private static class.
Hiding Named Helper Visitor inside of ToString1 |
Comments |
package listFW.visitor; import listFW.*; public class ToString1WithHiddenHelper implements IListAlgo { public static final ToString1WithHiddenHelper Singleton = new ToString1WithHiddenHelper(); private ToString1WithHiddenHelper() { } |
Singleton Pattern |
private static class HiddenHelper implements IListAlgo { public static final HiddenHelper Singleton = new HiddenHelper(); private HiddenHelper() { } 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()); } } |
The helper visitor has a name, HiddenHelper, and is defined privately and globally (static) inside of the main visitor ToString1WithHiddenHelper. |
public Object emptyCase(IMTList host, Object... nu) { return "()"; } public Object nonEmptyCase(INEList host, Object... nu) { return host.getRest().execute(HiddenHelper.Singleton, "(" + host.getFirst()); } } |
The main visitor calls on its hidden helper singleton to help complete the job. |
Hiding Named Helper Visitor inside of ToString2 |
Comments |
package listFW.visitor; import listFW.*; public class ToString2WithHiddenHelper implements IListAlgo { public static final ToString2WithHiddenHelper Singleton = new ToString2WithHiddenHelper(); private ToString2WithHiddenHelper() { } |
|
private static class HiddenHelper implements IListAlgo { public static final HiddenHelper Singleton = new HiddenHelper(); private HiddenHelper() { } 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()); } } |
|
public Object emptyCase(IMTList host, Object... nu) { return "()"; } public Object nonEmptyCase(INEList host, Object... nu) { return host.getRest().execute(HiddenHelper.Singleton, host.getFirst().toString()); } } |
Hiding Named Helper Visitor inside of ToString3 |
Comments |
package listFW.visitor; import listFW.*; public class ToString3WithHiddenHelper implements IListAlgo { public static final ToString3WithHiddenHelper Singleton = new ToString3WithHiddenHelper(); private ToString3WithHiddenHelper() { } |
|
package listFW.visitor; import listFW.*; public class ToString3WithHiddenHelper implements IListAlgo { public static final ToString3WithHiddenHelper Singleton = new ToString3WithHiddenHelper(); private ToString3WithHiddenHelper() { } |
|
public Object emptyCase(IMTList host, Object... nu) { return "()"; } public Object nonEmptyCase(INEList host, Object...bu) { return "(" + host.getFirst() + host.getRest().execute(HiddenHelper.Singleton); } } |
- 1311 reads