|
|
|
1000 Java Tips ebook
|
|
 

Free "1000 Java Tips" eBook is here! It is huge collection of big and small Java
programming articles and tips. Please take your copy here.
Take your copy of free "Java Technology Screensaver"!. |
|
Synchronized is implementation detail
|
JavaFAQ Home » General Java

Although the synchronized keyword can appear in a method header, it
does not form a part of a method's API or contract. Synchronization is
part of the implementation, not part of the interface.
Note: some early Sun do*****entation (javadoc) included the synchronized
keyword in the description of methods. This error was later corrected.
Example
package myapp.data;
import myapp.business.Device;
/**
* The business layer talks to the data layer about storage of Device objects
* through a DeviceDAO reference.
*
* DataAccessException is a wrapper class, which exists only to wrap
* low-level exceptions specific to each storage mechanism (for example,
* SQLException and IOException). When an implementation class throws
* an exception, it is caught, wrapped in a DataAccessException, and then
* rethrown. This protects the business layer from ripple effects caused by
* changes to the datastore implementation.
*/
public interface DeviceDAO {
Device fetch( String aId ) throws DataAccessException;
void add( Device aDevice ) throws DataAccessException;
void change( Device aDevice ) throws DataAccessException;
void delete( Device aDevice ) throws DataAccessException;
}
The following valid implementation of DeviceDAO has some methods
declared as synchronized, even though the interface has nothing to say
regarding synchronization.
package myapp.data;
import myapp.business.Device;
import java.net.InetAddress;
import javax.servlet.ServletConfig;
/**
* An implementation of DeviceDAO which is specific to a MySql database.
*
* This class must be package-private, to ensure that the business layer
* remains unaware of its existence.
*
* Any or all of these methods can be declared as synchronized. It all depends
* on the details of your implementation.
*
* Note that it is often possible to use properties files (or ResourceBundles) to
* keep SQL out of compiled code, which is often advantageous.
*/
final class DeviceDAOMySql implements DeviceDAO {
DeviceDAOMySql( ServletConfig aConfig ) {
//..elided
}
public Device fetch( String aId ) throws DataAccessException {
//create a SELECT using aId, fetch a ResultSet, and parse it into a Device
return null; //toy implementation
}
synchronized public void add( Device aDevice ) throws DataAccessException{
//parse aDevice into its elements, create an INSERT statement
}
synchronized public void change( Device aDevice ) throws DataAccessException{
//parse aDevice into its elements, create an UPDATE statement
}
synchronized public void delete( Device aDevice ) throws DataAccessException {
//extract the Id from aDevice, create a DELETE statement
}
}
republished from "Collected Java Practices" site (license)
Printer Friendly Page
Send to a Friend
..
Search here again if you need more info!
|
|
|