Android development-Basics of Java Design Patterns

Source: Internet
Author: User
Tags dot net

Today, we have some basic knowledge about some design patterns in Android development. This time, we will analyze the architecture design related to the Android project:

  1. Static factory Method

The static factory method can be regarded as the integration of the factory method and Singleton mode on the Android platform. Because the Android Context can deliver instances well, the static factory method can refer to the traditional class constructor, this design can be considered for some logic service providers, such as file downloading and image cropping.

  2. Java class Access Permissions

For program reliability, the member variables should be private as much as possible, and these private members should be accessed through brute-force or public methods. Methods like getXXX and setXXX are provided, not only Java, this C # concept of attribute operations has been deeply rooted in Dot Net, and the advantage is that it can prevent access chaos after inheritance.

  3. Use enumeration to replace Constants

Java has added the enum enumeration class in JDK 1.5, Which is simpler and safer than defining some constants like General final int. After all, constants are a bunch of integer-like values, printing does not make much sense. enumeration can effectively prevent hidden dangers for the definitions of inherited access.

  4. The list takes precedence over the array.

The Java Collection class is very convenient. The List of List classes has a higher overhead than the array like Object [], but it is more powerful for generic support. You can also avoid unnecessary errors, such

CwjObject [] obj = new int [1];

Obj [1] = "android Development Network Test"; // This will throw an exception like ArrayStoreException at runtime.

  The usage list is:

List <cwjObject> obj = new ArrayList <int> ();

Obj. add ("android123 test"); // an error is prompted during compilation because the input list is a string different from the int type during construction, this can avoid unnecessary situations.

  5. Java foreach instead of

Java foreach still uses for writing, which is a little different from C # using the foreach keyword directly, but the method is the same, except for the more concise, in fact, foreach is more optimized than the traditional for, such as the traditional for second limit bit, generally access attributes or methods, such

For (int x = 0; x <obj. size (); x ++) // The limit symbol of this sentence. obj is executed every time. the size () method. Of course, Android developer believes that the size () method accesses the length attribute of an array.

For (int y = 0; y <obj. length; y ++) // here, obj is also executed for each loop. the length overhead of the Java VM is mainly determined by the length of the obj, and the recommended method of Android SDK documentation is

Int nSize = obj. size () or int nSize = obj. length

For (int z = 0; z <nSize; z ++), but this is not the optimal method. The following Android123 provides a better alternative to foreach:

For (SmartObject singleObj: SmartObjectArray)

{

SingleObj. setName ("cwj"); or singleObject. strName = "cwj ";

}

I hope Android Developers in China can lay a solid foundation for Java design patterns and skills in Android development, so don't get together to implement overwhelming applications. If we don't talk about malware problems, the quality of most software designs is worrying, there are also many applications that are basically the porting version of the J2EE or J2SE open-source project.

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.