Design Patterns: Make the code reusable, making it easier for others to understand and ensure code reliability.
The classic design pattern is divided into three categories.
Create model: Singleton mode, Factory mode, etc.
Structural mode: Decoration mode, proxy mode, etc.
Behavioral Mode: Template method mode, iterator mode, etc.
Singleton design Pattern: intent: To ensure that a class has only one instance and provides a global access point to access it. Usability:
There can be only one instance at this moment and the customer can access it from a well-known. In any case, the class can only create one instance!
Singleton design Pattern Creation Step: 1. Defines a private static property of the current class type. 2 privatization construction method.
3. Define a static method that can get the current class instance. In this method, we can determine whether an instance has been created,
Once built, it is returned directly, thus achieving a single case effect.
private static Demosingleton obj;
or private static Demosingleton obj=new Demosingleton ();
Private Demosingleton () {}
public static Demosingleton getinstance () {
if (obj==null) {
Obj=new Demosingleton ();
}
return obj;
}
Template method Mode: Intent: Define the framework of an algorithm procedure in an operation, and defer some steps to the implementation of subclasses.
Similar to defining interfaces or abstract classes, subclasses implement abstract methods.
Single case mode and principle