java interview 07-- design patterns

1. What is a design pattern

Design model is proven predecessors numerous summed up the design process can be used repeatedly, design ways to solve specific problems.

2. What are the common design patterns

Example 2.1 Single mode (mode-fed, starving mode, dual lock mode)

https://blog.csdn.net/zhangliangzi/article/details/52438401

1. Construction method of privatization, so in addition to their classes can be created, other places can not be created;

2. Create a single instance in his class (well-fed model is created out of a single instance, but when a hungry man need only create mode);

3. To provide a method for obtaining the instance object (method requires synchronization pattern created starving).

// starving mode, hungry very anxious, i.e., creates an instance object class loading 
public  class Singleton1 { 
    
    Private  static Singleton1 Singleton = new new Singleton1 (); 
    
    Private Singleton1 () { 
        
    } 
    
    public  static Singleton1 the getInstance () {
         return Singleton; 
    } 
}
// -fed mode, do not worry very full, lazy loading, When are created by Shashi instance, the presence of security thread 
public  class Singleton2 { 
 
    Private  static Singleton2 Singleton; 
    
    Private Singleton2 () { 
        
    } 
    
    public  static  the synchronized Singleton2 the getInstance () {
         IF (Singleton == null ) 
            Singleton = new new Singleton2 ();
         return Singleton; 
    } 
}
//饱汉模式的双重锁模式,提高效率
public class Singleton3 {
    private static Singleton3 singleton;
    
    private Singleton3(){
        
    }
    
    public static Singleton3 getInstance(){
        if(singleton == null){
            synchronized(Singleton3.class){
                if(singleton == null){
                    singleton = new Singleton3();
                }
            }
        }
        return singleton;
    }
}

1, a hungry man is thread-safe mode, because the instance object is created in the class load, but returns the object directly in the getInstance () method reference. It is called "starving" because this model creates an instance of an object more "urgent", really hungry ......

Benefits: simple, without paying attention to thread safety issues.

Cons: If you use too many hungry singleton in a Chinese environment, it will produce too many instances of objects, whether you want to use them.

2, well-fed mode is not thread-safe, because it is the only instance of an object is generated when needed, prior to production will determine whether the object reference is empty, here, if multiple threads simultaneously enter judgment, it will generate multiple instances of an object, this is inconsistent with the idea of ​​a single case. So-fed mode in order to ensure thread-safe, use the synchronized keyword identifies the method. It is called "well-fed" because it is full, do not rush the production instance, will produce when needed.

Benefits: delay loading, when used will produce objects.

Disadvantages: the need to ensure synchronization, pay for efficiency.

3, double lock mode is to optimize the well-fed mode, double judge, when after the instance object already created without having to lock and improve efficiency. It is also a recommended way to use.

2.2 Factory Pattern

Spring IOC factory pattern is used

Creating an object to a factory to create, without creating their own

2.3 proxy mode

Spring AOP proxy mode is used

2.4 Packing Mode

 

Guess you like

Origin www.cnblogs.com/xinmomoyan/p/11516211.html