Design pattern for applying a transform to multiple properties in multiple classes

I use the WMD label 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 several markdown properties.

I keep markdowns as it makes editing fields easier later. However, I need to convert the properties to HTML for display later. The question is: is there some kind of template that I can use to avoid writing markdown conversion code in all my entity classes?

I created a utility class with a method that takes a markdown string and returns HTML. I am using markdownj and it works fine.

The problem is every property of every class that stores markdowns. 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 is that if the Course class has 2 more properties, then regarding learning requirements and prerequisites, both require converters, then I will have to write getTuitionAsHTML () and getPrerequisiteAsHTML ().

I find this a bit ugly and would like a cleaner solution. Classes that require this are not part of a single inheritance hierarchy.

Another option that I am considering is doing this in the controller, not in the model. What do you think about this?

Thank.

[EDIT]: ( ). struts2 ( ), , , . , HTML.

+3
3

, , .

, , , .

, , , . ?

View. # 1 MVC , , .

, , , .

+2

, . , , , * .

. , (, ).

+1

Ignoring architectural issues, I think a simple answer might 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.

+1
source

Source: https://habr.com/ru/post/1698979/


All Articles