Analysis of Android singleton simple instance design pattern

Analysis of singleton simple instance design pattern

preface

Today, I'd like to comprehensively summarize the most commonly used design pattern in Android Development - singleton pattern.

catalogue

1. Introduction

1.1 what problems are solved

As mentioned before, design pattern = a solution to a specific problem, so what kind of problem is singleton pattern a solution to?

Meaning: single instance = one instance;

Problem solved: reduce the coupling between objects

Solution: Singleton mode, that is, only one instantiated object of a class is implemented, and a global access point is provided

1.2 example introduction

Next, I use an example to introduce the singleton pattern

Background: Xiaocheng has a plastic factory, but there is only one warehouse in it.

Purpose: I want to use code to realize warehouse management

Existing practice: establish warehouses and workers

Where, quantity in warehouse class = commodity quantity; There are handling methods movein (int i) and moveout (int i) in the worker class.

Problems: through the test, it is found that a new warehouse will be built for each worker handling operation, that is, the goods are not placed in the same warehouse. What's the matter? (see the code below)

result:

2. Introduction to single example mode

2.1 problems solved (application scenario)

Conflict: from the above results, it can be seen that the workers do not operate the same warehouse instance.

Objective: all workers operate the same warehouse instance

Singleton mode is the solution to solve this kind of problem: only one instantiated object is implemented for a class, and a global access point is provided. 2.2 working principle

In Java, we operate these classes by using objects (after class instantiation). Class instantiation is carried out through its construction method. If you want to realize that a class has only one instantiated object, you must work hard on the construction method of the class:

General implementation of singleton mode: (including use steps)

Well, you should understand the introduction and principle of singleton mode? Now let's solve the problem that "the warehouse is not a" in Xiaocheng above!

2.3 example introduction

Xiaocheng uses the singleton mode to improve the code of the above example:

result:

From the result analysis, after using the single instance mode, the warehouse class has only one warehouse instance, and there is no need to worry about the porters entering the wrong warehouse!!!

2.4 advantages

2.5 disadvantages

3. Implementation of singleton mode

3.1 general conditions

Hungry Han style (the simplest single instance Implementation)

Application scenario:

Lazy style

The biggest difference between lazy type and hungry type is the timing of initialization of singleton:

Application scenario:

3.2 implementation of singleton mode under multithreading

In the case of multithreading:

Solution 1: synchronization lock

Use the synchronization lock synchronized (singleton. Class) to prevent multiple threads from entering at the same time, resulting in instance being instantiated multiple times.

Solution 2: double check lock

A layer of if is added on the basis of synchronous lock (in addition to synchronized (singleton. Class)). This is to avoid executing synchronized (singleton. Class) to obtain object lock after the instance has been instantiated, so as to improve performance.

Solution 3: static inner class

When loading classes in the JVM, the data will be synchronized. We use internal classes to create object instances in internal classes. As long as the internal class is not used in the application, the JVM will not load the singleton class and create singleton objects, so as to realize "lazy" delayed loading and thread safety.

Solution 4: enumeration types

The most concise and easy-to-use single instance implementation method (recommended by effective Java)

The usage is as follows:

5. Summary

This article gives a comprehensive introduction to the singleton pattern, including the principle and implementation method. Next, I will continue to explain other design patterns. If you are interested, you can continue to pay attention

Thank you for reading, hope to help you, thank you for your support to this site!

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>