For Android development, do not use Java as Javascript

Source: Internet
Author: User

In Android development, anonymous applications make development more flexible, while the abuse of anonymous classes makes programs messy and difficult to maintain. Programmers like to be lazy. We often see that an Activity class is full of anonymous classes, including Thread, Handler, Adapter, Listener, and final variables that pass data to anonymous classes, it makes people feel like they are not civilized.

After all, Java is not Javascript, and there are no closures and no anonymous functions. It is far-fetched to use functional programming to write object-oriented languages. Below is a piece of JS Code:

  MongoClient.connect('mongodb://127.0.0.1:27017/test', (err) 'test_custom_key', 'a':1}, '_id': ObjectID("aaaaaaaaaaaa")}).toArray(

Thanks to the closure, anonymous function, and function (variable) features of JS, JS developers can bring function nesting to an amazing level, and even if it is nested with many layers, it will not seem very bloated. Can you think about the effect of setting so many layers of Java? The editor may not display the longest indentation, but more Android Developers can use this interface. Let's look at an example:

  MyActivity   View getView( (arg1 == = LayoutInflater.from(MyActivity.  Handler handler =                                                    Thread(                                         handler.sendEmptyMessage(0

This is a real example. Many Android Developers are used to it. This onCreate function has nearly a thousand lines of code, and it has completely lost readability. If you leave someone to take over your job, you will surely be scolded if you see such code.

 

Next, I will provide my own alternative solutions for the common anonymous classes in Android development. This is for your reference only. Please verify the error.

Replace anonymous classes with inherited Interfaces

This method is suitable for various Listener types. Here we use OnClickListener as an example to describe it. The binding event is generally completed at onCreate, so there may be many anonymous classes of OnClickListener in onCreate, such:

findViewById(R.id.button).setOnClickListener( 

At this time, the onCreate method will become lengthy and contain many processing logics that should not appear in onCreate. If we want the Activity to implement OnClickListener, set all listener processed by clicks to this activity, in onClick. when getId () is used for branch processing, the program structure will be much more compact:

  DummyActivity  Activity  ( id : in                      

Listener for a single View, including onLongClickListener, can be operated by inheriting the interface of the Activity. If it is an ItemListener for the list items, it is recommended that the Adapter inherit the interface.

Use subclass instead of Anonymous class

If an Activity contains sub-classes of AdapterView, such as ListView and GridView, the corresponding Adapter will be set. In addition to the simplest cases, you can use SimpleAdapter and ArrayAdapter to directly map, in many cases, we need to inherit BaseAdapter and then rewrite some methods. When the logic is relatively simple, it is advisable to use an internal class. For example, many people prefer to write this in onCreate:

((ListView)findViewById(R.id.listView)).setAdapter( View getView(});

To avoid the irrelevant logic involved in onCreate and keep the code structure reasonable, you can try to handle it like this:

  DummyActivity     MyAdapter  View getView(

As for static and otherwise, it depends on whether you want to use dynamic member variables. This method makes parameter transmission more convenient: you only need to pass through the constructor, instead of using the final keyword.

Replace anonymous classes with external classes

Simple logic classes such as Adapter can be processed by internal classes. If more complex logic is involved, such as data processing, I/O, and database operations, we recommend that you use external classes to make the business attributes of each class as single as possible. For example, the Thread class has a low degree of relevance with the Activity class. It is obviously inappropriate to stick the Thread class to the Activity to be lazy, whether it is an anonymous class or a subclass form, here is no specific example. For external class naming, if you cannot think of a proper name, you can consider prefix naming. For example, if there is a DummyActivity and a MyThread is inserted in it, it is obviously inappropriate to call MyThread again, you can simply change the suffix name to DummyActivityThread.

 

Summary

Javascript is a very good language and has many features of functional programming. Reasonable flexible nesting can make the code compact and elegant, while Java is a strictly object-oriented language, the abuse of Anonymous class nesting will make your program bloated and messy. It is recommended that you do not write Java as a Javascript style, which will be difficult to maintain.

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.