Design Pattern to apply conversion to multiple properties in multiple classes
-
05-07-2019 - |
Question
I am using the WMD markdown editor in a project for a large number of fields that correspond to a large number of properties in a large number of Entity classes. Some classes may have multiple properties that require the markdown.
I am storing the markdown itself since this makes it easier to edit the fields later. However, I need to convert the properties to HTML for display later on. The question is: is there some pattern that I can use to avoid writing markdown conversion code in all my entity classes?
I created a utility class with a method that accepts a markdown string and returns the HTML. I am using markdownj and this works fine.
The problem is for each property of each class that stores markdown I may need another method that converts to HTML:
public class Course{
private String description;
.
.
.
public String getDescription(){
return description;
}
public String getDescriptionAsHTML(){
return MarkdownUtil.convert(getDescription());
}
.
.
.
}
The problem there is that if the Course class has 2 more properties Tuition and Prerequisites say, that both need converters then I will have to write getTuitionAsHTML() and getPrerequisiteAsHTML().
I find that a bit ugly and would like a cleaner solution. The classes that require this are not part of a single inheritance hierarchy.
The other option I am considering is doing this in the controller rather than the model. What are your thoughts on this?
Thanks.
[EDIT]: New thoughts (Thanks Jasper). Since the project uses struts2 (I did not say this before) I could create a view component say that will convert the markdown for me. Then I use that wherever I need to display the value as HTML.
Solution
The classes that require this are not part of a single inheritance hierarchy.
They should at least implement a common interface, otherwise coming up with a clean generic solution is going to be cumbersome.
The other option I am considering is doing this in the controller rather than the model. What are your thoughts on this?
This clearly is a responsibility of the View. The #1 MVC rule is that the Model doesn't care about its representation, the markdown in this case.
However, I feel that there is to little detail about your current architecture to give a meaningful answer to your question.
OTHER TIPS
You do have one option for doing this if you can't use inheritance or an interface. I know, I know refactor but this is reality and *hit happens.
You can use reflection to iterate over your properties and apply the formatting to them. You could either tag them with an attribute or you could adopt a naming scheme (brittle, but still an option).
Ignoring the architectural problems, I think the simple answer could be:
public String getDescription(MarkDownUtil converter)
{
if (converter == null) return description;
else return MarkdownUtil.convert(description);
}
Even better would be to make MarkDownUtil implement IStringConverter, and you could have several different StringConverters for different jobs.