Daily Archives: January 23, 2009

Avoiding multiple “if-else if”s using functors Part 1

Scenario: Sometimes, we need to add a behavior to several classes, where we cannot modify the classes. For example, imagine wanting to add prettyToString() to Strings, Numbers, Booleans, Collections and Maps. A first stab at the code may look like this: public static String prettyToString(Object input) { if (input instanceof String) { return prettyToString((String) input); [...]

Follow

Get every new post delivered to your Inbox.