PDA

View Full Version : Logic into Appearance ?



DavGut
3 Jul 2015, 6:40 AM
Hello, I have a design question about the appearance design pattern.

By exemple I would like a cell which display a String in 2 ways depending of the model (bold or not bold if the model=true or false).

I have 2 possibilities:


put the "logic" in the cell and call 2 different appearances.
put the "logic" in only one appearance with 2 different displays depending of the model

Code as example :

Solution 1 : "logic" in the cell



public class MyCell extends AbstractCell<String> {

// (...)

@Override
public void render(Context context, String value, SafeHtmlBuilder sb) {

boolean isImportant = getModel().isImportant();

if (isImportant){
appearanceImportant.render(sb, value);
} else {
appearance.render(sb, value);
}

}

}

Solution 2 : "logic" in the appearance



public class MyCell extends AbstractCell<String> {

// (...)

@Override
public void render(Context context, String value, SafeHtmlBuilder sb) {

boolean isImportant = getModel().isImportant();

// the appearance will choose how to display the string depending of isImportant
appearance.render(sb, value, isImportant);

}
}



Which is the best way ?