I have 2 separate classes, but they both need to do one thing repetitively, in other words there is a common method. This is the method:
private String createButton (String cls, String value) {
return "<input type=\"button\" class=\"" + cls +
"\" value=\"" + value + "\" key=\"" + this.id + "\" />";
}
so this is really just a one line method, so I could copy it into both classes. But I was wondering if there’s a better way of doing that. I really don’t want to have a super class with just that method, obviously. Also, I think it’s silly creating another class just for the button and do: new Button(cls,value), isn’t?
Another option I thought about is to have a utility class for the package, with a mix of helper functions. Does that make sense? Is it being done?
You could use a utility class… but it might become incohesive.
This is the kind of code that leads down the dark road to a God Object.
Instead, consider scoping the purpose and intent of your “utility” class: consider making it a factory – specifically an Abstract Factory so that it retains cohesiveness.