A Preliminary Study on Android development Singleton mode and a preliminary study on android Development

Source: Internet
Author: User
Tags web ssh

A Preliminary Study on Android development Singleton mode and a preliminary study on android Development

What is the singleton mode? It is one of the models in Java 23 and is also widely used in Android development. For example, the Calander class does not use new Calander but Calander when creating objects. getInstnce () is an application of the singleton mode. What are the application scenarios? In many cases, we only need one object, but we do not need multiple objects. For example, the ancient emperor, a mountain cannot be a tiger. If there is more tigers, the only harm is not good. It is used to ensure that there is only one and only one instance in the entire application. one is all!

The Singleton mode is divided into two types: the hungry and the lazy. Let's take a look at what the hunger and lazy modes are!

Let's take a look at the true sense of the hunger model!

Public class Singleton {// 1. privatize the constructor and do not allow external users to directly create the object private Singleton () {}// 2. create a unique instance of the class and use private static to modify private static Singleton instance = new Singleton (); // 3. provides a method for getting an instance, using public static to modify public static Singleton getInstance () {return instance ;}}
In order not to allow this class to construct multiple objects, the first step of the hungry Chinese mode is to privatize the constructor, and external objects cannot be directly created. Step 2: create a unique instance of the class and use the private static modification to return the instance through getInstance (). This ensures that the class cannot be modified at will. The biggest feature of the hunger mode is that objects are created as long as the class starts to load.

Let's take a look at the lazy model.

Public class Singleton2 {// 1. privatize the constructor and do not allow external users to directly create the private Singleton2 () {}// 2. the unique instance of the Declaration class. Use private static to modify private static Singleton2 instance; // 3. provides a method to obtain an instance. Use public static to modify public static Singleton2 getInstance () {if (instance = null) {instance = new Singleton2 ();} return instance ;}}

We can see that the lazy and ELE. Me modes are roughly the same. The only difference is that only declaring classes in step 2 does not create objects.

Finally, we will summarize the differences between the two:

The hungry Chinese mode is characterized by slow loading of classes, but fast object retrieval speed and thread safety during running.
The lazy mode is characterized by fast loading of classes, but slow speed of getting objects during running, and unsafe threads. Pay attention to multithreading!



When the android Singleton mode is used, the object that is killed in the singleton mode disappears.

Let me add the following:
When the process is shut down:
1. Use Process. kill or shell to kill the Process.
2. The system uses the memory policy to kill background processes.
Let's talk about the second method. When the program exits by pressing the Home or Back key, it becomes a background process.
In addition, when the program starts a new process. The new process enters the foreground mode, and the program also becomes a background process.
The current process exits, and the background process is re-presented according to the stack structure. It is probably a process of restarting the process. restarting the process means that the singleton object is reinitialized. This should be especially cautious.

What should I do if I develop an android project?

Does android have similar things? Do you also need to consider frameworks and other things? Please help.
------ Solution --------------------------------------------------------
If the database is used locally, it is embedded with sqlite3. If you use a remote database, you can use it as needed.
I personally think the architecture is basically mvc, but it is best to discard the web ssh framework and re-learn the beautiful design pattern of Android.
------ Solution --------------------------------------------------------
Focus on the reusability of components.
------ Solution ---------------------------------------------------------- reference:
First, let's talk about the design concept:
1. First, design the database as needed, because almost all operations should refer to the data model. 1 million people/times per day
SQL Server or mysql.
2. Secondly, design the android interface, and design the business logic to be consistent.
3. create and implement the android object class again.
4. Finally, return the request sent by the front-end server design and processing.
The above four steps are generally involved.
As for the design pattern and framework:
Some tool classes on android can be used in a single example or factory.
The android end does not need the framework (not yet ...), In the background, we recommend that you use struts2 to control and process requests.
I think so much for the moment. If you don't understand it, ask again.

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.