Java High-quality code notes

Source: Internet
Author: User

"Try to write unit tests."

There are many other different test frameworks for Android, which uses JUnit in general.

See the importance of unit testing: http://www.csdn.net/article/2012-04-10/2804366

"" It is strongly recommended to use UTF-8 encoding

"" Performance considerations, arrays are preferred

"" Thread-safe Singleton mode

public final class doublecheckedsingleton   {        private static DoubleCheckedSingletonsingObj = null;           private doublecheckedsingleton () {        }          public static  Doublecheckedsingleton getsingleinstance () {            if (null == singobj )  {                synchronized (Doublecheckedsingleton.class) {                       if (null = = singobj)                        &nbsP;     singobj = new doublecheckedsingleton ();                }           }        return singObj;      }   }

Another way: http://www.it165.net/pro/html/201403/11212.html

Package com.troy.young.thread;/*** Thread-Safe Singleton class * * @author Administrator**/public class SingleTon {/*** defines his method of construction, There is no way outside to construct the object with new */private SingleTon () {}/*** defines an inner class to instantiate the Singleton. * First the inner class is static, guaranteeing that the class has only one copy in memory, and that his member variable is final static, again guaranteeing his uniqueness. * At the same time because the JLS will guarantee the thread safety of the class. * @author administrator**/private Static class Singletonholder {Public final static SingleTon instance = new SingleTon ();} /*** provides a way to get an instance of * @return */public static SingleTon getinstance () {//Get the variables of the inner class directly return singletonholder.instance;}}

"" Multithreading using vectors or HashTable

"" using enumerations to implement factory method patterns is more concise

UML diagrams show Fordcar and Buickcar:

DEMO:

 public class test {  enum carfactory {  fordcar {    public car create ()  {    return new fordcar ();    }  },  buickcar {   public car create ()  {    return new buickcar ();   }  };   Public car create1 ()  {   switch  (this)  {   case  Fordcar:    return new fordcar ();   case ... ...    default:    throw new assertionerror ("Invalid parameter");    }   }  public abstract car create (); }  public void  Test ()  {  carfactory.buickcar.create ();   carfactory.buickcar.create1 ();  } }

Java High-quality code notes

Related Article

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.