There are five types of Creation modes: Factory method mode, abstract Factory mode, singleton mode, builder mode, prototype mode.
Single-case mode: Original Link: How to write the singleton pattern correctly
meaning : In its core structure, it contains only a special class called a singleton class. The singleton mode can ensure that there is only one instance of a class in the system, and the instance is easy to be accessed by the outside world, thus it is convenient to control the number of instances and save system resources .
recommended notation One (static inner class):
public class Singleton { private static class Singletonholder { private static final Singleton INSTANCE = new Sing Leton (); } Private Singleton () {} public static final Singleton getinstance () { return singletonholder.instance; } }
Recommended notation Two (enumeration):
public enum easysingleton{ INSTANCE;}
Factory mode:original link: In a comprehensible factory model
meaning : Factory mode is the most commonly used instantiation object pattern, and it is a pattern that replaces the new operation with a factory method . (Decide which instance to create based on incoming parameters)
Chestnuts:
Abstract product role public interface Car {public void drive (); }//Specific product role one public class Benz implements Car {public void Drive () {System.out.println ("Driving B Enz "); }}//Specific product role two public class BMW implements Car {public void Drive () {System.out.println ("Drivi ng BMW "); }}//factory class Role public class Driver {//factory method. Note Return type is abstract product role public static Car Drivercar (String s) throw s Exception {//Judgment logic, return specific product roles to client if (S.equalsignorecase ("Benz")) return new Benz (); else if (s.equalsignorecase ("BMW")) return new BMW (); else throw new Exception (); }}//Welcome upstart ... public class magnate {public static void main (string[] args) {Car car = D River.drivercar ("Benz");//Tell the driver that I am riding the Mercedes Car.drive today ();//Next command: drive}}
Cond.....
The creation pattern of Java design patterns