1, Overview of design patterns
Design pattern: it is a summary of code design experience that is repeatedly used, classified and cataloged, known by most people. That is, the scenario based solution to the general problems faced by software developers in the process of software development.
Create pattern (focus object creation process) | Structural model (focus on the combination of classes and objects) | Behavioral model (focus on the communication process between objects) |
Factory method model Builder pattern Abstract factory pattern Prototype mode | Bridging mode proxy pattern Sharing element mode Appearance mode Decorator mode Combination mode Adapter mode | Memo mode Interpreter mode Command mode Intermediary model Observer mode Strategy mode State mode Template method pattern Visitor mode Iterative sub pattern Responsibility chain model |
2, Singleton mode:
- Purpose: to make an object of a class the only instance in the class system
- Definition: a class has and has only one instance, which is instantiated by itself and provided to the whole system.
main points:
① A class can only have 1 instance.
② You must create your own instance.
③ You must provide this instance to the entire system yourself
realization:
① Only private constructors are provided.
② Contains a static private object of this class.
③ Provide a static public method for creating and obtaining static private objects.
Code implementation scheme:
① Hungry Chinese style: instantiated during object creation. Space for time: when the class is loaded, the static instance object completes the instantiation operation, so the operation speed is relatively fast, and the existence cycle of the static instance object is relatively long,
② Lazy: instantiated on first use (instantiated in static public methods). Time for space: objects will not be instantiated when they are created, so class loading will not really open up the space of objects and save space. It takes time to judge the instance validity of the object. If you don't use it all the time, there will be no loss of space.
Example:
package com.singleton; //Hungry Chinese style: initialize directly when creating an object public class SingletonOne { //1. Create a private construct in the class private SingletonOne(){ } //2. Create a private static instance of the class private static SingletonOne instance=new SingletonOne(); //3. Create public static methods and return static instance objects public static SingletonOne getInstance() { return instance; } }
package com.singleton; //Lazy: the instance object in the class is not initialized directly when it is created. The initialization operation is not completed until the get method is called for the first time public class SingletonTwo { //1. Create private construction method private SingletonTwo() { } //2. Create a static instance object of this class private static SingletonTwo instance=null; //3. Create an open static method to provide an instance object public static SingletonTwo getInstance() { if (instance==null) //If it is empty, the instantiation operation will be performed instance=new SingletonTwo(); return instance; } }
package com.test; import com.singleton.SingletonOne; public class Test { public static void main(String[] args) { SingletonOne one=SingletonOne.getInstance(); //Call static methods by class name. SingletonOne two=SingletonOne.getInstance(); System.out.println(one); System.out.println(two); System.out.println("==============================="); SingletonOne a=SingletonOne.getInstance(); SingletonOne b=SingletonOne.getInstance(); System.out.println(a); System.out.println(b); } }
For multithreading:
The hungry Chinese style instantiates the object when the class is loaded. Therefore, even if multiple threads operate concurrently, the instance object is unique. The hungry Chinese style is thread safe.
Lazy mode is initialized only when it is used for the first time. Therefore, when multiple threads operate concurrently, there may be some thread risks due to the switching of time slices. It can be solved through synchronization lock, double check lock, static internal class and enumeration.
- Advantages of singleton mode:
① There is only one object in memory to save memory space.
② Avoid frequent creation and destruction of objects and improve performance.
③ Avoid multiple occupation of shared resources.
- Disadvantages of singleton mode:
① Expansion is difficult.
② If the instantiated object is not used for a long time, the system will recycle it as garbage by default, resulting in the loss of object state.
- Applicable scenarios:
① It takes too much resources to create an object, but it needs to be used at the same time.
② Unified read-write is required for resources in the system, such as read-write configuration.
③ When multiple instances exist, it may cause program logic errors, such as number generator.