Java design mode-singleton mode and template Mode
1. Singleton mode method: the class construction method is private. A static class object in the class is used to save this class object, and this class provides a static method to obtain this class object.
/*** Singleton design mode ** @ author Admin **/public class Design1 {private String name; private static Design1 d; private Design1 (String name) {this. name = name;} public static Design1 getDesign () {if (d = null) {System. out. println (constructor); d = new Design1 (ddd);} return d ;}@ Overridepublic String toString () {return name + Singleton ;} public static void main (String [] args) {Design1 d = Design1.getDesign (); System. out. println (d );}}
Example result:
2. template design mode:
It describes how to implement the common functions of each subclass as much as possible when defining a parent class. Different functions are implemented by sub-classes. That is, the unchanged part is implemented by the parent class, and the variable part is implemented by the Child class, but it limits the randomness of the program design.