The creation pattern is divided into five categories: Factory method mode, abstract Factory mode, singleton mode, builder mode, prototype mode
First, factory method mode: interface-Implementation Class-factory class
The purpose of the factory class is to produce sender objects, which are passed in by different parameters to produce different objects.
Changing the factory method in the factory class to multiple factory methods is the design pattern for multiple factory methods
Static Factory method design mode by changing the factory method in plant class to static
Second, abstract factory method mode: interface-Implementation Class-factory class
Features are:
The factory class implements an interface that can be extended later, such as a factory class that increases Bluetooth.
Factory design patterns are a special case of abstract factory design patterns.
Three, single case mode
Singleton object (Singleton), which guarantees that only one instance of the object exists in a JVM.
A hungry man mode: classes are created when loaded and slow to load classes, but run-time gets objects faster and thread-safe
/* Type: A hungry man mode */public class Singleton {//1. Privatization of the construction method, not allowing external direct creation of the object private Singleton () {}//2. Create a unique instance of the class, using the private static modifier private static Singleton instance=new Singleton ();//3. Provides a method for obtaining an instance, using public static to decorate public static Singleton GetInstance () {return instance;}}
Lazy mode: Class loading when not to create, loading classes faster, the first user gets to create an instance, the first time to get objects slower, thread insecure
/* Lazy mode */public class Singleton2 {//1. Privatization of construction, not allowing the creation of objects directly outside private Singleton2 () {}//2. Declaring a unique instance of a class, using private static modifier private static Singleton2 INSTANCE;//3. Provides a method for getting an instance, using public static to decorate public static Singleton2 getinstance () { if (instance==null) {instance=new Singleton2 ();} return instance;}}
The creation pattern of Java design patterns