Java design mode-singleton Mode
In the past two days, I have learned the simplest mode in Java design mode-singleton mode.
It is said that there are 23 Java Design Patterns in total: Singleton, abstract factory, builder, and prototype.
Design Pattern: it is actually a set of summaries that are repeatedly used, known by most people, written by category, and code design experience. The design pattern is used to make code reusable and make it easier for others to understand. And ensure code reliability.
Singleton mode:
In practical applications, we only need one object, such as configuration files, tool classes, thread pools, caches, log objects, and database connection objects.
If multiple instances are created, many problems may occur, such as occupying too many resources and inconsistent results. In this case, the singleton mode can be used to solve this problem, ensure that only one instance exists in the entire application.
Common Singleton mode:
Hunger and lazy
Usage:
I. Hunger Model
Package com. demo;/*** SingleTon ** Application Scenario: in actual applications, some objects only need one, such as configuration files and database connection objects. * function: ensure that an instance in the entire application has only one * type: Hunger mode, lazy mode * @ author likang * currently: hunger mode */public class SingleTon {/*** Step 1: privatize the constructor and prohibit external users from directly creating objects. Only classes can create their own */private SingleTon () {}/*** Step 2: create a unique instance of the class, which is created by the class itself within the class. The problem arises. If this is the case, external * Instances of the class cannot be directly called. Therefore, the static keyword must be added to modify the instance into a member object of the class, so the external * can directly use the class. the member variable obtains this instance */private static SingleTon instsnce = New SingleTon ();/*** Step 3: In order to control external instances that cannot directly invoke classes, you also need to encapsulate the Member objects of the class *. Here, you can only access the class by yourself through the private keyword. External access is not allowed directly. * In this way, external access is not allowed through the class. the member object to obtain the instance. Therefore, a public * method (modified by public) must be provided inside the class for external calls to obtain the instance, in addition, this method also needs to be modified using the static keyword * to become a member of the class (because it cannot be created using the new keyword .) * @ Return */public static SingleTon getInstance () {/*** hunger mode: Because the instance is declared as static, it belongs to all classes, static member variables * are loaded with the class. Whether the class is called or used by the user, once the class is loaded, * static member variables will also be loaded, just like they are not full, and are vividly called the "Hunger mode" */return instsnce ;}}
Ii. Lazy Model
Package com. demo;/*** lazy mode * @ author likang **/public class SingleTon2 {/*** Step 1: privatize the constructor, do not allow external users to directly create instances */private SingleTon2 () {}/*** Step 2: only declare the unique instance of the class and do not create it, use private static to modify */private static SingleTon2 instance;/*** Step 3: provide a method to obtain the instance for external calls, use public static to modify * @ return */public static SingleTon2 getInstance () {/*** lazy mode: When a class is loaded, only the instance of a class is declared, do not create, * but create a class instance when the user actually uses it and calls it. If this is the first time * is called, The instance is obviously empty. Create one, and then use it directly when calling it again. */If (instance = null) {instance = new SingleTon2 () ;}return instance ;}}
Test results:
Package com. demo; public class test {public static void main (String [] args) {/* SingleTon s1 = SingleTon. instsnce; SingleTon s2 = SingleTon. instsnce; */SingleTon s1 = SingleTon. getInstance (); SingleTon s2 = SingleTon. getInstance (); // determine whether the instances s1 and s2 are the same instance if (s1 = s2) {System. out. println ("s1 and s2 are the same instance");} else {System. out. println ("s1 and s2 are not the same instance");} SingleTon2 s3 = SingleTon2.getInstance (); SingleTon2 s4 = SingleTon2.getInstance (); // determine whether the s3 and s4 instances are the same instance if (s3 = s4) {System. out. println ("s3 and s4 are the same instance");} else {System. out. println ("s3 and s4 are not the same instance ");}}}
<喎?http: www.bkjia.com kf ware vc " target="_blank" class="keylink"> VcD4KPHA + pgltzybzcm9 "http://www.2cto.com/uploadfile/Collfiles/20141221/20141221091022286.jpg" alt = "[in block memory limit n blocks ...? Http://www.bkjia.com/ OS/"target =" _ blank "class =" keylink "> system resources. Thread Security
B. The Lazy style faces the security issue of multi-threaded access, and requires a dual lock to ensure security. Thread Security
C. The hungry Singleton class is enough to meet our needs.
Because the hunger mode creates an instance after the class is loaded, it will occupy a certain amount of system resources and be slower, but it can be used directly at runtime, and the speed will be faster.
In the lazy mode, when a class is loaded, the instance is declared and not created. Instead, the instance is created when the user calls the class for the first time, you can use it directly again.
I don't know much about thread security now. I will study it later. Well, it may be a bit cool, because my mind has never been very good, and I often need to spend more time to understand what others can understand, this approach is just to help you better understand the singleton design model. Well, keep improving!