Singleton design pattern
-
What is a design pattern
(1) Classic use of static methods and properties
(2) Design pattern is the code structure, programming style and thinking way to solve problems after summarizing and theorizing in a lot of practice. Design pattern is like a classic chess score. Different chess games need us to use different chess scores
-
What is singleton mode
Single instance (single instance)
-
The so-called single class design pattern of class is to take certain methods to ensure that there can only be one object instance for a class in the whole software system, and the class only provides a method to obtain its object instance
2. There are two single case modes: (1) hungry man mode (2) lazy man mode
-
-
The steps are as follows:
(1) Constructor privatization
(2) Class
(3) Expose a static public method
(4) Code implementation
-
example
public class Test { public static void main(String[] args) { BoyFriend getinstance = BoyFriend.getinstance(); System.out.println(getinstance); } } class BoyFriend{ private String name; private static BoyFriend boyFriend = new BoyFriend("karrywang"); private BoyFriend(String name) { this.name = name; } public static BoyFriend getinstance() { return boyFriend; } @Override public String toString() { return "MY BoyFriend'S{" + "name yes'" + name + '\'' + '}'; } }
-
Lack of hungry Chinese style: objects may be created but not used
-
An example of lazy is as follows:
public class Test { public static void main(String[] args) { System.out.println(BoyFriend.handsom()); } } class BoyFriend{ private String name; private static BoyFriend boyFriend; private BoyFriend(String name) { this.name = name; } public static BoyFriend handsom(){ if (boyFriend == null){ boyFriend = new BoyFriend("ROY"); } return boyFriend; } @Override public String toString() { return "MY BoyFriend'S{" + "name yes'" + name + '\'' + '}'; } }
-
The difference between the two
(1) The main difference between the two is that the time of creating objects is different: the hungry type creates object instances when the class is loaded, while the lazy type is created when it is used.
(2) There is no thread safety problem for the hungry type, and there is thread safety problem for the lazy type
(3) The hungry man type may waste resources, because if the programmer does not use any object instances, the object instances created by the hungry man type will be wasted, while the lazy man type does not need to consider this problem
final keyword
-
Basic introduction:
final can modify classes, properties, methods, and local variables
-
final may be used:
(1) When you do not want the class to be inherited
(2) When you do not want a method of a parent class to be overridden / overridden by a child class
(3) When you do not want the value of a property of a class to be modified
(4) When you do not want a local variable to be modified
-
Considerations and details of the final keyword
(1) The attribute modified by final is also called a constant, which is generally named XX_XX_XX
(2) When defining the final modified attribute, it must be given an initial value and cannot be modified later. It can be modified in the following position:
① When defining: for example, public final int rate = 8;
② In the constructor
③ In a code block
(3) If the final modified attribute is static, the initialization position can only be ① defined ② in the static code block and cannot be assigned in the constructor
(4) final classes cannot inherit, but can have instantiated objects
(5) If the class is not final but contains a final method, the method cannot be overridden but can be inherited.
(6) Generally speaking, if a class is already a final class, there is no need to modify the method to a final method
(7) final cannot modify a constructor (that is, a constructor)
(8) final and static are often used together, which is more efficient and will not lead to class loading (the underlying compiler has optimized)
example:
public class Test { public static void main(String[] args){ System.out.println(finaltest.a); } } class finaltest{ public final static int a = 10; static{ System.out.println("Class loading has already occurred..."); } }