Originally posted by Jeff Song:
if(!localMode){
Hold it right there. This is what they call a code smell. A bad one.
There should be an interface -- call it DataInterface -- which is probably implemented by Data in local mode, and by something else in networked mode.That interface should have clearly defined semantics. A manual, if you like, saying what each method does in abstract terms. In abstract terms, because an Interface is an abstract entity.For example, JavaDoc for the close() method should not say "closes the database". That is what Data happens to do, but Data is but one implementation of the interface. It should not read "In local mode... and in networked mode...", because again you would be thinking in terms of implementations. The abstract meaning of the DataInterface.close() method is after this call, the DataInterface will no longer be used; release all database resources held on behalf of the client. In local mode, that happens to mean closing the database file. In remote mode, that means releasing any locks held on behalf of the client but certainly not closing the database file.Now try to think what the semantics (manual) are for the lock() and unlock() methods.The key point to watch out for is: once the client has a DataInterface object, it should not matter what implementation it happens to have. Whether it is a local implementation, a networked implementation, or an implementation with bad-tempered dwarves throwing axes at each other, it doesn't matter.[list]In an OO design, whenever you find yourself writing code like
There is probably something wrong with your interface or the semantics you defined for it. Do realise that your
test for localMode boils down to the very same thing.[/list]It should be clear by now that your client code will have to lock() and unlock() regardless of the mode. But that doesn't mean that the Data implementations of these methods cannot be empty!
- Peter
[ November 22, 2002: Message edited by: Peter den Haan ]