Java code is commonly used design patterns and understanding
A, singleton
1. hungry Chinese-style (too hungry, class loading when you create an instance)
/ ** * Example starving single mode * / public class HungrySingleInstance { // generated when an instance of the class loader Private Final static HungrySingleInstance instance = new new HungrySingleInstance (); // because there is a public class default constructor with no arguments, Therefore, covering it with a private private HungrySingleInstance () {} // this method is invoked, generating a return singleton object class loading public static HungrySingleInstance the getInstance () { return instance; } }
The so-called hungry man single design pattern is static instance of the class as a member variable of the class, which means it when it is already loaded instance of the class created in the JVM, so it does not exist multi-threaded safe problem.
But in advance of the instance is initialized or structure, if this instance and not to use, it will cause a waste of resources.
2. type lazy (lazy, do not take the initiative to create an instance, when the first call, only to create)
/ ** * Example lazy single mode * / public class LazySingleInstance { // generated when a reference is null class loader instance Private static LazySingleInstance instance = null ; // because there is a public class default constructor with no arguments, so covering it with a private private LazySingleInstance () { } public static LazySingleInstance the getInstance () { // when examples are not empty, return directly this example iF ( null == instance) { // lock + determination, when a plurality avoid threads through the above judgment, create multiple instances, cause safety problems the synchronized (LazySingleInstance. class ) { //Only the first modulation method, create an instance IF ( null == instance) { instance = new new LazySingleInstance (); } } } return instance; } }
Single cases of lazy mode on while addressing the efficiency and safety.
Singleton pattern used in the following scenarios:
1. create objects and often destroyed;
2. Create the need to consume a lot of resources, but also frequently used objects;
3. Object used to access the database or file.
Second, the factory pattern
1. Simple factory pattern
Example: a simple factory pattern to send SMS messages.
/ ** * a sending interface for sending an SMS message or * / public interface the Send { void Send (); }
/ ** * Send Message class implements the interface, email sent * / public class Mail the implements the Send { @Override public void Send () { System.out.println ( "the this IS A Mail Message!" ); } }
/ ** * Send SMS class implements the interface * / public class the Sms the implements the Send { @Override public void Send () { System.out.println ( "the this the Sms IS A Message!" ); } }
/ ** * Send factory class * / public class SendFactory { / ** * The different type parameters, examples of the production of different objects * @param type * @return * / public the Send Produce (MessageSenderType type) { IF (MessageSenderType.SMS .equals (type)) { return new new the Sms (); } the else IF (MessageSenderType.MAIL.equals (type)) { return new new Mail (); } return null ; } }
/ ** * type sent enumeration class * / public enum MessageSenderType { // SMS the SMS, // mailbox the MAIL }
/** * 工厂测试类 */ public class FactoryTest { public static void main(String[] args) { SendFactory factory = new SendFactory(); factory.produce(MessageSenderType.SMS).send(); factory.produce(MessageSenderType.MAIL).send(); } }
Simple factory pattern when the need to increase the product you want to modify the source code, undermine the principle of ocp (open for extension, closed for modification).
2. Factory Method pattern
Factory method with simple factory pattern similar pattern, except that the pattern of the factory class factory method, a plurality of methods, each method is used to produce a corresponding object. Simply factory mode, in one approach, different parameters are also passed in the production of different objects. So their limitations are the same.
3. Abstract Factory pattern