<< Chapter < Page | Chapter >> Page > |
Main Visitor
ToString3 |
Non tail-recursive helper
ToString3Help |
package listFW.visitor;
import listFW.*;public class ToString3 implements IListAlgo
{public static final ToString3
Singleton = new ToString3();private ToString3() {}
public Object emptyCase(IMTList host, Object... nu) {
return "()";}
public Object nonEmptyCase(INEList host, Object... nu) {
return "(" + host.getFirst()+ host.getRest().execute(
ToString3Help.Singleton);}
} |
/**
* Helps ToString3 compute the String* representation of the rest of the list.
*/class ToString3Help implements IListAlgo {
public static final ToString3HelpSingleton = new ToString3Help();
private ToString3Help() {}public Object emptyCase(
IMTList host, Object... nu) {return ")";
}public Object nonEmptyCase(
INEList host, Object... nu) {return ", " + host.getFirst() +
host.getRest().execute(this);}
} |
What makes each of the above different from one another is its helper visitor. Each helper defined in the above will only perform correctly if it is passed the appropriate parameter. In a sense, each helper is an implementation of the main visitor. We should hide each of them inside of its corresponding main visitor to ensure proper usage and achieve full encapsulation of the main visitor.
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 finalToString2WithHiddenHelper 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());
}} |
|
Notification Switch
Would you like to follow the 'Principles of object-oriented programming' conversation and receive update notifications?