Dirty data problem of the Java program
Out-of-date data, indicating the data. If you have dirty data in your Java program, you will bring more or less to the software system, such as: I can't apply a change in real-time, some inexplicable, difficult to reproduce, consequences Severe error, etc. Try to avoid the existence of dirty data is very valuable. This article hopes to help our peers in this regard.
Fragment 1. Dirty data of caching technology issues / ** * a report. * * @Version 1.0 9/9/2003 * @Author bill * / public class reportprinter {/ ** * Constructs a < ReportPrinter code> instance. * / public reportprinter () {// do something ...}
. / ** * Prints a printable * * @param printable the specified printable object * / public void print (Printable printable) {Graphics g = getGraphics (); g.setFont (getReportFont (printable.getFont ());
Printable.Print (g);
. / ** * Returns the corresponding report font of a java font * * @param javaFont the specified java font * @return the corresponding report font * / private Font getReportFont (font javaFont) {Font reportFont = fontMap.get (javaFont);
IF (ReportFont == Null) {reportFont = loadingfont (javafont); fontmap.put (javafont, reportfont);}
Return ReportFont;
. / ** * Loads the corresponding report font of a java font * * @param javaFont the specified java font * @param the corresponding report font * / protected static Font loadFont (Font javaFont) {Font reportFont = null;
// do something ...
Return ReportFont;
/ ** * The Font Map (Java font-> Report font). * / Private static hashmap fontmap = new hashmap ();
In Fragment 1, since the REPORT FONT overhead loaded with a Java Font is large, cache technology is used to avoid this overhead. This is a common way to improve performance, and well in general. However, the design and implementation of Fragment 1 may be incomplete because the Report Font corresponding to a Java Font will change after the system starts, after this change occurs, only restarting the software system can be loaded, which is often One of the complaints of end users. More terrible is that the presence of this dirty data may also bring other serious, unimaginable consequences. How to avoid using the dirty data problem brought by the cache technology? When designing, implementing, and testing, you should clearly define the update of cache data: i. Restarting the software system is a necessary manner regardless of the cache data; II. Cache data is not possible to become dirty without considering the update of cache data. Data (but in the software system, it is often "impossible" to become "possible" after another reconstruction; III. Update the cache data update, when the source data changes, real-time update cache data. Fragment 2. Singleton Mode Dirty Data Issues / ** * A Storage Usage Handler Is Used To Query The Storage Usage Of Users. * * @Version 1.0 9/9/2003 * @Author Bill * / Public Class StorageusageHandler {/ ** * Returns a StorageUsageHandler code> instance. * * @return the single
StorageUsageHandler code> instance * / public static StorageUsageHandler getStorageUsageHandler () {if (handler == null) {handler = new StorageUsageHandler ( }
Return Handler;
/ ** * Constructs a StorageusageHandler code> instance. * / Private storageusagehandler () {users = context.getallusers ();
/ ** * Returns The Storage Sizes of all the users. * * @Return the storage sizes * / public long [] getsizes () {long sizes [] = new long [users.size ()];
For (int i = 0; i / ** * Returns The Storage Size of a user. * * @Param User the specified user * @return the storage size * / protected long getsize (user user) {// do something ... returnography;} / ** * The / ** * the users. * / Private list users;} Do you see the problem? In Fragment 2, unnecessary overhead due to no need to instantiate StorageUsageHandler, using Singleton mode to ensure that StorageUsageHandler is only instantified once. When instantiation SotrageUsageHandler, the store member users of StorageUsagehandler will be assigned. Since there is no way to reinfinct value for users, users who have been residing in the software system will not change. After the software system is started, the operation of adding, deleting or modifying the user often occurs, and once this is happened, the user has become dirty data, and Fragment 2 will not work properly. How to avoid the dirty data problem brought by using the Singleton mode? For class members of the Singleton class: i. There is no such problem for class members who have no dependencies outside the Singleton class; II. For class members outside the Singleton class, and the members do not have update mechanisms, the best It is removed from the Singleton class directly to the outside of the Singleton class; if this approach is not feasible, the mechanism should be provided to ensure that the class member has been updated before using the member. Fragment 3. Problems dirty data classes used / ** * A storage usage handler is used to query the storage usage of users. * * @Version 1.0 9/9/2003 * @author Bill * / public class StorageUsageHandler implements AdminHandler {/ ** * Constructs a / ** * Returns The Storage Sizes of all Users. * * @Return the storage sizes * / public long [] getsizes () {long sizes [] = new long [users.size ()]; For (int i = 0; i Return 0;} / ** * Displays the storage usage of users. * * @Param req the http servlet request * @param res the http servlet response * * @throws IOException * @throws ServletException * / public void process (HttpServletRequest req, HttpServletResponse res) throws IOEXCEPTION, servletexception { Res.SetContentType ("Text / HTML"); Res.SetHeader ("cache-control", "no-cache"); Res.SetHeader ("Pragma", "no-cache"); res.SetDateHeader ("expires" , 0); PrintWriter Writer = New PrintWriter (res. gitputstream ()); long sizes [] = getSizes (); Writer.println (" StorageUsageHandler code> Singleton. * / Private static storageusagehandler handler;
StorageUsageHandler code> instance. * / Private storageusagehandler () {users = context.getallusers ();