I. single example mode
1. Hungry Chinese style (too hungry, create an instance when the class is loaded)
/** * Starved Han style single case model */ public class HungrySingleInstance { // Generate an instance when the class loads private final static HungrySingleInstance instance = new HungrySingleInstance(); //Because the class will have a public nonparametric construct by default, it will be overridden by private private HungrySingleInstance(){} //Call this method to return the singleton object generated when the class is loaded public static HungrySingleInstance getInstance(){ return instance; } }
The so-called starved Chinese single instance design pattern is to take the static instance of the class as a member variable of the class, that is to say, the instance of the class has been created when the JVM loads it, so it will not have the security problem of multithreading.
But if the instance is initialized or constructed in advance, if it is not used again, it will cause a waste of resources.
2. Lazy (too lazy to create an instance, only when it is called for the first time)
/** * Lazy singleton mode */ public class LazySingleInstance { // Generate a reference as null Example private static LazySingleInstance instance = null; //Because the class will have a public nonparametric construct by default, it will be overridden by private private LazySingleInstance(){ } public static LazySingleInstance getInstance(){ //When the instance is not empty, the instance will be returned directly if(null == instance){ //lock+Judge to avoid creating multiple instances when multiple threads pass the above judgment at the same time, causing security problems synchronized (LazySingleInstance.class){ //Only the first time you call this method, create an instance if(null == instance) { instance = new LazySingleInstance(); } } } return instance; } }
Lazy singleton mode solves the problem of efficiency and safety at the same time.
The singleton mode is commonly used in the following scenarios:
1. Frequently create and destroy objects;
2. Create objects that consume a lot of resources and are often used;
3. Objects commonly used to access databases or files.
II. Factory mode
1. Simple factory mode
Example: a simple factory mode for sending SMS messages.
/** * A send interface for sending SMS or email */ public interface Send { void send(); }
/** * The mail class implements the sending interface to send mail */ public class Mail implements Send{ @Override public void send() { System.out.println("this is a Mail message!"); } }
/** * SMS class realizes sending interface */ public class Sms implements Send{ @Override public void send() { System.out.println("this is a Sms message!"); } }
/** * Send factory class */ public class SendFactory { /** * Different instance objects are produced according to different parameter type s * @param type * @return */ public Send produce(MessageSenderType type){ if(MessageSenderType.SMS.equals(type)){ return new Sms(); }else if(MessageSenderType.MAIL.equals(type)) { return new Mail(); } return null; } }
/** * Send type enumeration class */ public enum MessageSenderType { //Short message SMS, //mailbox MAIL }
/** * Factory test */ public class FactoryTest { public static void main(String[] args) { SendFactory factory = new SendFactory(); factory.produce(MessageSenderType.SMS).send(); factory.produce(MessageSenderType.MAIL).send(); } }
In simple factory mode, when products need to be added, the source code should be modified to break the ocp principle (open to extension, closed to modification).
2. Factory method mode
Factory method pattern is similar to simple factory pattern. The difference is that factory class of factory method pattern consists of multiple methods, each method is used to produce a corresponding object. In the simple factory mode, different objects are produced according to different parameters. So their limitations are the same.