Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

How to implement HashMap for lock/unLock?

 
Sean Gildea
Ranch Hand
Posts: 81
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Everyone,

First real question here as I hate bothering you all. I have searched all thru this forum for this answer but cannot find it.

I understand I must implement a hashmap in order to check/keep track of which records are locked or not. However, I am not clear on how to implement it. I have a FileIO class which does all the updates, deletes and reads.

My Question is: Should I declare a private static hashmap in the FileIO class that Data makes calls on? And in FileIO create my lock/unlock methods which data will call?

Thanks in advance!
[ November 11, 2004: Message edited by: Sean Gildea ]
 
Dieskun Koper
Ranch Hand
Posts: 85
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There are many ways to implement the locking mechanism.
Although I use a HashMap to store my records, I keep a flag in the records themselves that indicates whether it is locked or not.

If you make your hashmap static, think about the consequences. Think about what you or your successor has to do when they decide to add support for another database and want to reuse your code.
Make a list of all alternatives you can come up with, and choose the way you feel most comfortable with.
I do not know the details of your design, but to me locking code in a FileIO class would make sense if you are locking a file against concurrent access.

Regards,
Dies
 
Andrew Monkhouse
author and jackaroo
Marshal Commander
Pie
Posts: 12014
220
C++ Firefox Browser IntelliJ IDE Java Mac Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Sean,

My Question is: Should I declare a private static hashmap in the FileIO class that Data makes calls on? And in FileIO create my lock/unlock methods which data will call?


I might be misunderstanding your question, but from what I understood in that paragraph, you have decided to make your Data class a Fa�ade, with the real work being done by various delegate classes (such as your FileIO class). This, in my opinion, is a very good idea, as it ensures that the classes doing the real work only have one primary responsibility.

So, if I have understood your current implementation correctly, then your question is which of the delegate classes should you put the locking code into.

I think you need to consider what functionality your FileIO class should be providing. Then consider whether the locking functionality logically fits in with the existing functionality.

To put it another way, think about what you write as your first line of JavaDoc in each class. Normally you would want to write a very short, simple, description: "This class IS/PROVIDES ...". For example "This class provides all File I/O", "This class provides a lock manager", "This class is a Fa�ade to the database routines". If you cannot write that simple description without using the words "AND" or "OR" then you might want to stop and consider whether your class is doing too much work.

Given that, I personally don't think it belongs with the FileIO class. At the moment, the FileIO has a very clear responsibility: File I/O. If you add locking, then your FileIO class then has responsibility for two things. This means that it is harder to maintain, and can be harder for a programmer to find your locking code. (And it would even raise the question of what the Fa�ade gives you since nearly everything seems to be delegated to the one class).

You might want to consider adding a LockManager class (if you search in this forum for LockManager you will find it is discussed many times). This will help you keep the responsibilities clear, and will make future enhancements easier (consider adding time out code / recursive deadlock checking / ... Would you really want all that in your FileIO class?)

Of course, if I have misunderstood your question, or you are not using the Fa�ade pattern for your Data class (there is no requirement to use it for this assignment), then feel free to ignore my complete post .

Regards, Andrew
 
Sean Gildea
Ranch Hand
Posts: 81
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dear Andrew,

Thank you for your reply. I did not know what a facade class was until you described it, so you taught me alot just now! I'm glad to know I have been implementing this the correct way so far.

I'm going to assume a private static hashmap will be acceptable in my new LockManager class based on both of your replies. So that every client is using the same hashmap.

Thanks again!
 
Billy Tsai
Ranch Hand
Posts: 1304
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
just read the javadoc on the java.util.HashMap
it teaches u everything in there
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic