Day 5 (implementing another 100 filters)

Mark has asked us to implement another 100 filters. This time, we have the flexibility and the support to accomplish this task, but we still need to write 100 strategies or classes for implementing the MelonPredicate for each selection criteria. Moreover, we have to create instances of these strategies and pass them to the filterMelons() method.

This means a lot of code and time. In order to save both, we can rely on Java anonymous classes. In other words, having classes with no names that are declared and instantiated at the same time will result in something like this:

List<Melon> europeans = Filters.filterMelons(
melons, new MelonPredicate() {
@Override
public boolean test(Melon melon) {
return "europe".equalsIgnoreCase(melon.getOrigin());
}
});

There is some progress being made here, but this is not very significant because we still need to write a lot of code. Check the highlighted code in the following diagram (this code repeats for each implemented behavior):

Here, the code is not friendly. Anonymous classes seem complex and they somehow look incomplete and weird, especially to novices.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.227.26.217