Dirty data problem of the Java program

zhaozj2021-02-12  143

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 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 instance. * * @return the single StorageUsageHandler instance * / public static StorageUsageHandler getStorageUsageHandler () {if (handler == null) {handler = new StorageUsageHandler ( }

Return Handler;

/ ** * Constructs a StorageusageHandler 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 StorageUsageHandler Singleton. * / Private static storageusagehandler handler;

/ ** * 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 StorageUsageHandler instance. * / Private storageusagehandler () {users = context.getallusers ();

/ ** * 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 (" storage usage </ title> <body>"); Writer.println (" <Table Width = '100%'> ");</p> <p>For (int i = 0; i <sizes.length; i ) {Writer.print ("<tr> <td align = 'center' NOWRAP>"); Writer.Print (users.get (i) ":" Sizes [I]); Writer.println ("</ TD> </ TR>");}</p> <p>Writer.println ("</ body> </ html>"); writer.flush (); Writer.close ();</p> <p>/ ** * the users. * / Private list users;}</p> <p>/ ** * An admin servlet as a http servlet to process the admin http servlet * request and response. * * @Version 1.0 9/9/2003 * @author Bill * / public class AdminServlet extends HttpServlet {/ ** * Initiates the . configuration * * @param config the servlet config * * @throws ServletException * / private void initConfig (ServletConfig config) throws ServletException {// do something ... handlerMap.put ( "__ storage_Usage__", new StorageUsageHandler ());}</p> <p>/ ** * Processes the http servlet request and response. * * @Throws IOException * @throws ServletException * / public void service (HttpServletRequest req, HttpServletResponse res) throws IOException, ServletException {</p> <p>Admin LeerMap.Get (Req.getParameter ("Handler");</p> <p>IF (Handler == Null) {// do something ...</p> <p>Return;}</p> <p>Handler.Process (REQ, RES);</p> <p>/ ** * The Admin Handler Map (Handler Name-> Handler). * / Private hashmap handlermap = new hashmap ();}</p> <p>You must see the problem! In Fragment 3, since StorageUsageHandler does not follow the Singleton mode, although StorageUsageHandler's class member users can only assign values ​​when instantizing StorageUsageHandler, but in single-threading mode, as long as the StorageUsageHandler instance used is newly instantiated, It is basically no problem. The problem is that the StorageUsageHandler is instantiated and stored in the process of initializing the adminservlet. Thereafter, the StorageusageHandler will not be re-instantified unless servlet container is reloaded, and the StorageusageHandler will not be able to update the class members of StorageUsageHandler. In this way, the user will become dirty data after an increase, delete or modify the user's operation. How to avoid the dirty data problem brought about by class? I. There is no such problem for class members that have no dependencies with the outside of the class; II. For class members dependent on the class, and this class does not have an update mechanism. It is best to remove it, it is directly obtained from the outside of the class; if this method is not feasible, the mechanism should be provided to ensure that before using this class, the member has been updated; if this method is not feasible, Please clearly explain the way the class is used to prevent improper classes. The three examples were listed in three examples, and three common dirty data issues were listed. In fact, the form of dirty data in the Java program has a variety of forms. Therefore, in the process of design, implementation, testing and reconstruction, Keep In Mind avoids the existence of dirty data, we can , Subsystems, classes, and class members, etc. to check the Java program. "Only do one thing" is the best interpretation of the simplicity. This sentence is also best interprets the orthogonality of software systems in functionality. However, during the object-oriented software development process, it is not enough to ensure that orthogonality is not enough, and orthogonality should be guaranteed to be guaranteed in data storage. Of course, considering the performance and other factors, it is difficult to ensure that orthogonality is difficult in data storage, and the mechanism should be provided to ensure the real-time performance of the data used.</p></div><div class="text-center mt-3 text-grey"> 转载请注明原文地址:https://www.9cbs.com/read-6242.html</div><div class="plugin d-flex justify-content-center mt-3"></div><hr><div class="row"><div class="col-lg-12 text-muted mt-2"><i class="icon-tags mr-2"></i><span class="badge border border-secondary mr-2"><h2 class="h6 mb-0 small"><a class="text-secondary" href="tag-2.html">9cbs</a></h2></span></div></div></div></div><div class="card card-postlist border-white shadow"><div class="card-body"><div class="card-title"><div class="d-flex justify-content-between"><div><b>New Post</b>(<span class="posts">0</span>) </div><div></div></div></div><ul class="postlist list-unstyled"> </ul></div></div><div class="d-none threadlist"><input type="checkbox" name="modtid" value="6242" checked /></div></div></div></div></div><footer class="text-muted small bg-dark py-4 mt-3" id="footer"><div class="container"><div class="row"><div class="col">CopyRight © 2020 All Rights Reserved </div><div class="col text-right">Processed: <b>0.072</b>, SQL: <b>9</b></div></div></div></footer><script src="./lang/en-us/lang.js?2.2.0"></script><script src="view/js/jquery.min.js?2.2.0"></script><script src="view/js/popper.min.js?2.2.0"></script><script src="view/js/bootstrap.min.js?2.2.0"></script><script src="view/js/xiuno.js?2.2.0"></script><script src="view/js/bootstrap-plugin.js?2.2.0"></script><script src="view/js/async.min.js?2.2.0"></script><script src="view/js/form.js?2.2.0"></script><script> var debug = DEBUG = 0; var url_rewrite_on = 1; var url_path = './'; var forumarr = {"1":"Tech"}; var fid = 1; var uid = 0; var gid = 0; xn.options.water_image_url = 'view/img/water-small.png'; </script><script src="view/js/wellcms.js?2.2.0"></script><a class="scroll-to-top rounded" href="javascript:void(0);"><i class="icon-angle-up"></i></a><a class="scroll-to-bottom rounded" href="javascript:void(0);" style="display: inline;"><i class="icon-angle-down"></i></a></body></html><script> var forum_url = 'list-1.html'; var safe_token = 'sZNTIdKiGxV2YdJyz7hcOjmYoFRE_2BMDViIG_2Bizuw5U_2FhqFT2GVQFGwENt_2B2WExgo8isq3_2Fttw3S1NFnw'; var body = $('body'); body.on('submit', '#form', function() { var jthis = $(this); var jsubmit = jthis.find('#submit'); jthis.reset(); jsubmit.button('loading'); var postdata = jthis.serializeObject(); $.xpost(jthis.attr('action'), postdata, function(code, message) { if(code == 0) { location.reload(); } else { $.alert(message); jsubmit.button('reset'); } }); return false; }); function resize_image() { var jmessagelist = $('div.message'); var first_width = jmessagelist.width(); jmessagelist.each(function() { var jdiv = $(this); var maxwidth = jdiv.attr('isfirst') ? first_width : jdiv.width(); var jmessage_width = Math.min(jdiv.width(), maxwidth); jdiv.find('img, embed, iframe, video').each(function() { var jimg = $(this); var img_width = this.org_width; var img_height = this.org_height; if(!img_width) { var img_width = jimg.attr('width'); var img_height = jimg.attr('height'); this.org_width = img_width; this.org_height = img_height; } if(img_width > jmessage_width) { if(this.tagName == 'IMG') { jimg.width(jmessage_width); jimg.css('height', 'auto'); jimg.css('cursor', 'pointer'); jimg.on('click', function() { }); } else { jimg.width(jmessage_width); var height = (img_height / img_width) * jimg.width(); jimg.height(height); } } }); }); } function resize_table() { $('div.message').each(function() { var jdiv = $(this); jdiv.find('table').addClass('table').wrap('<div class="table-responsive"></div>'); }); } $(function() { resize_image(); resize_table(); $(window).on('resize', resize_image); }); var jmessage = $('#message'); jmessage.on('focus', function() {if(jmessage.t) { clearTimeout(jmessage.t); jmessage.t = null; } jmessage.css('height', '6rem'); }); jmessage.on('blur', function() {jmessage.t = setTimeout(function() { jmessage.css('height', '2.5rem');}, 1000); }); $('#nav li[data-active="fid-1"]').addClass('active'); </script>