'Interface and Implementations with Separate Templates

I have a Java interface with several implementing classes. Each implementation class needs its own associated (sub)template. At runtime I add a collection (List) of objects implementing my interface to my ST. How can I have each object in that list rendered by the appropriate template?

Example code:

public interface foo {...}
public class bar implements foo {...}
public class baz implements foo {...}

I would like to have a top-level ST for the interface foo, and each concrete/implementing class has its own ST as follows,

foo.st
bar.st
baz.st

I would like to pass a List<foo>, which contains a mix of bar and baz instances, to the foo ST and have the foo ST call the appropriate sub-template (bar or baz, depending on the subtype) for each element of the list.

I suppose I could create a Map<String /* template name */, foo /* either bar or baz */>, but it feels like by doing so I'm leaking the fact that I'm using StringTemplate into my data model design.

Any suggestions?



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source