This week's book giveaway is in the OO, Patterns, UML and Refactoring forum. We're giving away four copies of Refactoring for Software Design Smells: Managing Technical Debt and have Girish Suryanarayana, Ganesh Samarthyam & Tushar Sharma on-line! See this thread for details.
This passage was copied from the code supplied from the SCJD Exam book by Monkhouse and Camerlengo. I did a search and got no result. Obviously the code and comment are contradictory.
* A structure that keeps track of the locked Dvd records. Note that we
* have not made this static, as doing so would mean that only one set
* of reservations could exist at any given time. We rely on the class
* which uses this ReservationManager to ensure that only one instance
* exists for any reservations.
private static Map<String, DvdDatabase> reservations
= new HashMap<String, DvdDatabase>();
My feeling is that this should be static as only one Map will ever be necessary to keep track of the DVD record locks but I am interested in any replies on the code and comment used here.
It all depends on your design. If your Data class for example is a singleton, no need to mark it as static. My map to keep locked records is just an instance member, not a class member (so non-static, because otherwise my Data class would not be reusable at all)
I was looking through the forum as I had the same question as Greg. In your response to him, you said that your locked records map is non-static because otherwise your Data class would not be reusable. I'm not sure what you mean by saying that a static map would make your Data class not reusable. Is your static Map found in your Data class? Would you mind clarifying?
Matt Pavlovich wrote:Is your static Map found in your Data class? Would you mind clarifying?
My Data class contains a map to keep the locked records, but it's not a static one, because that would make your Data class not reusable. You would have a few instances of Data class (1 for rooms, 1 for hotels, 1 for customers,...), but just 1 map with locked records (because you made it static). So if 3 different users lock records of different types but with the same record number (which is possible, because they are in different files) you'll be in trouble with just 1 single map to keep locked records.