Project Presentation copy Generation design

Source: Internet
Author: User

During the development process, it involves the action of the copywriting. These texts may need to be displayed from the persistence or content of the program's process. The most egg pain, because of the changes in the text or design problems, resulting in these copywriting in the various locations of the program, through a variety of stringbuffer, StringBuilder or even direct all kinds of "+", such as splicing, resulting in a program riddled with "elegance" does not say, The cost of the rear maintenance is simply out of the talk.

In fact, in most cases, if the copy does not involve the need to live data based on the persistence of data to display, in fact, it can be very simple to mention a configuration center, database, configuration files, or even similar zookeeper such as middleware can also. But if you need to replace the content in real-time based on some data, it will be laborious.

We know that Java provides a class called Messageformat, which is directly formatted in some fixed format. For example, we define a sentence called: full {0} minus {1} yuan, And then we pass it to you messageformat.format an array, the contents of the array is: {100,10}, so we can get the copy according to Messageformat: full 100 minus 10 yuan.

In fact, this is relatively simple and easy to understand, we just need to populate the program with dynamically generated content into an array, and then configure the copy in another location, and then constantly call Messageformat to get the required copy, this can be. However, there is a problem with this approach, that is, the meaning of the array is ambiguous, heavily dependent on the filling order, while the outside of the configuration of the copy is not clear when the 0 or 1 what is a thing.

Since we know where to fill what content, in fact, we tidy up the system all the text, and then the abstract and common keywords, through the Agreed keyword fill in the copy, at the same time, the program inside the structure of the array into a map structure, map key is these keywords, Value is the contents of the array just now. So we can get rid of the order of the array requirements, while the configuration of the copy can also be transferred from the development of students to PM or even run the hands of students.

However, we also need to determine the performance and availability of this scenario, so we can do an experiment:

ImportCom.google.common.base.Stopwatch;Importorg.apache.commons.lang3.StringUtils;ImportJava.text.MessageFormat;ImportJava.util.HashMap;ImportJava.util.Map;ImportJava.util.concurrent.TimeUnit;ImportJava.util.regex.Matcher;ImportJava.util.regex.Pattern;/*** Created by@author<a href= "Mailto:[email protected]" >shaogang cong</a> on 16/5/31.*/ Public classEffiencytest {Private StaticString AAA = "Full {sum}" minus {credit}, online payment minus {Cre1}, limited to {start} to {Endtime} "; Private StaticString Aaa1 = "Full {0} yuan minus {1} yuan, online payment minus {2}, limited to {3} to {4}"; StaticObject[] Aaa1array = {100, 19, 20, "23:30", "23:45"}; Staticmap<string, object> parametermap =NewHashmap<string, object>(); Static{parametermap.put ("Sum", 100); Parametermap.put ("Credit", 19); Parametermap.put ("Cre1", 20); Parametermap.put ("Start", "23:30"); Parametermap.put ("Endtime", "23:45"); }     Public Static voidMain (string[] args) {Stopwatch Stopwatch=stopwatch.createstarted ();  for(inti = 0; I < 100000; i++) {Messageformat.format (AAA1, Aaa1array); } System.out.println ("Messageformat" +stopwatch.elapsed (timeunit.milliseconds)); String patternstring= "\\{(" + Stringutils.join (Parametermap.keyset (), "|") + ")\\}";        System.out.println (patternstring);        Stopwatch.reset ();        Stopwatch.start (); Pattern Pattern=Pattern.compile (patternstring);  for(inti = 0; I < 100000; i++) {Matcher Matcher=Pattern.matcher (AAA); StringBuffer SB=NewStringBuffer ();  while(Matcher.find ()) {matcher.appendreplacement (SB, Parametermap.get (Matcher.group (1) . toString ());            } matcher.appendtail (SB);        System.out.println (Sb.tostring ()); } System.out.println ("Pattern" +stopwatch.elapsed (timeunit.milliseconds)); }}

Execution Result:

messageformat910\{(endtime|start|sum|cre1|  Credit) \}pattern229

We will find that the map is not only easy to understand, but also much more efficient than the way messageformat. At the same time, we can completely do the regular expression of the pattern in advance compile temporary storage, if there is a change, you can also refresh the cache by the way to update.

Finally, we say that the key in the map is unified into a constant class that is alive in other structures for development, product, or operation of child shoes.

Project Presentation copy Generation design

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.