This week's book giveaways are in the Java EE and JavaScript forums.
We're giving away four copies each of The Java EE 7 Tutorial Volume 1 or Volume 2(winners choice) and jQuery UI in Action and have the authors on-line!
See this thread and this one for details.
The moose likes OO, Patterns, UML and Refactoring and the fly likes Effective Java: use function objects to represent strategies Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Engineering » OO, Patterns, UML and Refactoring
Bookmark "Effective Java: use function objects to represent strategies" Watch "Effective Java: use function objects to represent strategies" New topic
Author

Effective Java: use function objects to represent strategies

sharath kumar reddy
Greenhorn

Joined: May 14, 2013
Posts: 2
Hi All,

effective java book, what difference between these 2 programs.

Program 1: SINGLETON PATTERN



Program 2:


As per the book 'Program 2' is best strategy to implement function object but I didn't understand what is the difference it is making keeping 'Program 1' inside another class and making this into private static??

Thanks in advance.

Regards,
Sharath Karnati.
Scott Shipp
Ranch Hand

Joined: Mar 31, 2013
Posts: 121
    
    6

This is Item 21 in Effective Java. I have the "2nd Edition" of this book. From my reading of the chapter, I am not sure that there is a huge difference between what you have posted, except here are some possibilities for why program 2 would be recommended:

1. The String Length Comparator in #2 has an arguably more readable, descriptive name (You call Host.STRING_LENGTH_COMPARATOR vs. calling StringLengthComparator.instance).
2. It is part of class Host so it is seen in Host's API.

I don't really understand the point he makes about allowing a second interface (Serializable) to be implemented, because as you have shown, you can still do that in program 1.
sharath kumar reddy
Greenhorn

Joined: May 14, 2013
Posts: 2
Scott,

Even in singleton we can use same name similar to "Program 2"



in this case we need to create instance with StringLengthComparator.STRING_LENGTH_COMPARATOR

I didn't understand what you are saying about point# 2 Host's API ???

Thanks,
Sharath Karnati.
Scott Shipp
Ranch Hand

Joined: Mar 31, 2013
Posts: 121
    
    6

Consider the real life, practical example where the comparator you are creating is probably for some custom object you created. This custom class is the "host class" that Bloch is talking about.

Let's say the host class is called City, a class which has City, State, and Country properties. You want to provide a couple options for sorting City. You can sort City the normal way which is alphabetical by city name. For example:

Delhi, Delhi, India
Los Angeles, California, United States
Mumbai, Maharashtra, India
New York City, New York, United States
Osaka, Settsu, Japan


But some people want the option to sort first by country, then by city. For example:

Delhi, Delhi, India
Mumbai, Maharashtra, India
Osaka, Settsu, Japan
Los Angeles, California, United States
New York City, New York, United States

Imagine if you then make two comparator classes using your method. There will be one comparator class for each sort method. Along with the City class, you now have three classes.

City
CityNameComparator
CityCountryComparator


Some other person using your code now will have to code it like this:



But if you do what Bloch says, and make the comparators part of the host class (City), the code will be cleaned up quite a bit:


This second way there are a few advantages. First the two extra import statements are gone. Second it is clear that the comparators being passed in are meant for ordering City objects because they are part of the City class. Many people would say this is more readable and understandable this way. Third, when someone looks at the JavaDoc for the City class, they will see the two comparators there (that's what was meant by host's API), rather than having to go find them wherever else they are in the package.
 
Consider Paul's rocket mass heater.
 
subject: Effective Java: use function objects to represent strategies