Android Development Java Design Pattern Basic article

Source: Internet
Author: User
Tags foreach dot net

Today we do some basic work on some of the design patterns in Android development, and we analyze the architecture design of the Android project:

  1. Static Factory method

Static Factory method can be regarded as a factory method plus the integration of a single example mode on the Android platform, because the Android context can pass a good example, static Factory method can refer to the traditional class constructor, for some logical service provider classes can consider such a design, such as file download, Pictures, such as cropping operations.

  2. Java Class access rights

For the reliability of the program, the member variables are as private as possible, access to these private members by means of a burst disclosure method, providing a method like GetXXX and setxxx, not only Java, this C # for the concept of the operation of attributes in dot net has been deeply rooted, The advantage is that you can prevent inherited access problems.

  3. Use enumerations instead of constants

Java has joined the Enum enumeration class in JDK 1.5, and it is simpler and safer to define some constants than the regular final int, after all, constants are a bunch of plastic-like numbers that don't print too much, and the clarity of the enumeration for the inherited access can be a good way to prevent the pitfalls from happening.

  4. Using a list takes precedence over an array

The Java Collection class is handy, and the list of lists is more expensive than an array of object [], but it's more powerful for generics support. You can also avoid some unnecessary mistakes, such as

Cwjobject [] obj= new int[1];

OBJ[1] = "Android Development network Test"; This throws an exception like arraystoreexception at run time.

  And the use list is:

List<cwjobject> obj=new arraylist<int> ();

Obj.add ("android123 test"); Because the incoming list is a string, unlike a constructed-time type int, an error is prompted at compile time to avoid unnecessary occurrences.

  5. Java foreach instead for

Java foreach still uses a for to write, which is a bit different from C # directly using the foreach keyword, but the same method is used, except for the simpler, in fact, the foreach is more optimized than the traditional for, such as the traditional for the second limit bit, the general access property or method, For example

for (int x=0;x<obj.size (); x + +)//The restriction symbol for this sentence executes the obj.size () method every time, and of course the Android Development Network believes that the size () method accesses the length property of an array and is alive

for (int y=0;y<obj.length;y++)//Here's the same every loop. Obj.length the cost of the Java VM is largely determined by the length of this obj, and the Android SDK documentation is recommended

int nsize=obj.size () or int nsize=obj.length

for (int z=0;z<nsize;z++), but this is not the optimal method, and here's a better alternative to the foreach approach: Android123

for (SmartObject Singleobj:smartobjectarray)

{

Singleobj.setname ("CWJ"); or singleobject.strname= "CWJ";

}

About the Java design pattern in Android development techniques, I hope that the domestic Android developers to play a good Java Foundation, do not get together to achieve the overwhelming application, currently do not say malware problems, most of the software design quality is worrying, There are many applications that are basically the porting version of Java or J2SE open source projects.

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.