23 design patterns_study notes

1. Seven principles of object-oriented

The principle of opening and closing: open for extension, closed for modification
. Richter's substitution principle: inheritance must ensure that the properties owned by the superclass are still valid in the subclass.
Dependency inversion principle: interface-oriented programming, not implementation-oriented programming;
single responsibility principle: control The granularity of the class, decoupling the object, and improving its cohesion; the
principle of interface isolation: the special interface required by each class should be established.
Dimit's rule: only talk to your friends, not to "strangers";
The principle of composite reuse: try to use combination or aggregation and other association relationships to achieve it first, and then consider using inheritance relationships to achieve it;

2. Classification of design patterns

  1. Creation mode: Describe how to create an object; it is characterized by the separation of object creation and use.
    Singleton, factory, abstract factory, builder, prototype mode
    理解:帮助程序员省去new对象的过程
  2. Structural mode: Describes the formation of a larger structure of classes or objects according to a certain layout; Role: Loose coupling from the structure of the program, which can expand the overall class structure to solve larger problems;
    adapters, bridges, Decoration, combination, appearance, Flyweight, agency model
  3. Behavioral mode: Describe how classes or objects can collaborate with each other to jointly complete tasks that a single object cannot complete, mainly assigning responsibility
    template method mode, command mode, iterator, observer mode, intermediary mode, memorandum mode, Decoder mode, state mode, strategy mode, responsibility chain mode, visitor mode;

1. Singleton mode

The most important idea is the privatization of the constructor (others cannot new this object, to ensure that there is only one object in memory)

1.1 Hungry Chinese singleton

New objects at the beginning;
Disadvantages: May cause waste of resources;

public class Hungry {
    
    
    private byte[] data= new byte[1024*1024];
    private byte[] dat2= new byte[1024*1024];
    private byte[] dat3= new byte[1024*1024];
    private Hungry() {
    
    

    }

    private final static Hungry HUNGRY = new Hungry();
    public static Hungry getInstance(){
    
    
        return HUNGRY;
    }
}

1.2 Lazy singleton

public class LazyMan {
    
    
    private LazyMan(){
    
    

    }
    private volatile static LazyMan lazyMan;
    public static LazyMan getInstance(){
    
    
        if(lazyMan==null){
    
    
            synchronized (LazyMan.class){
    
    
                if(lazyMan==null){
    
    
                    //这里不是原子性操作 
                    /*1.分配内存空间;2.执行构造方法,初始化对象;3把这个对象指向这个空间*/
                    lazyMan=new LazyMan();   
                }
            }
        }
        return  lazyMan;
    }
}

1.3 Singleton in the form of static inner class

public class Holder {
    
    
    private Holder(){
    
    

    }
    public static Holder getInstance(){
    
    
        return InnerClass.HOLDER;
    }
    public static class InnerClass{
    
    
        private static final Holder HOLDER = new Holder();
    }
}

Singleton is not safe, (reflection will break)

1.4 Singleton of enumerated type (safe)

public enum EnumSingle {
    
    
    INSTANCE;
    public EnumSingle getInstance(){
    
    
        return INSTANCE;
    }
}

2. Factory model

Role: The creator and the caller are separated
Core essence:

  • New is not applicable to instantiated objects, use factory methods instead
  • The implementation class will be selected and the objects will be created for unified management and control. So as to decouple the caller from our implementation class;

classification:

  • Simple factory mode (static factory) is
    used to produce any product in the same hierarchical structure (adding a new product requires modifying the existing code)

    1. The static method in the factory class is responsible for the new object, and consumers only need to call the corresponding method in the static factory.

  • The factory method mode
    does not modify the existing classes, and expands by adding new factory classes; in the
    factory method mode, each constructed type will have a corresponding factory, and the factory is responsible for the new object;
    every time an object is created, Need a factory of the corresponding type of new, and then call the method responsible for the new object in the factory;

    In the factory method mode, a unified factory interface and object interface are specified. The objects to be created have corresponding factories (methods in them) to help us create them.
    We need to manually create this type of factory and call the methods in the factory;

  • The abstract factory pattern
    cannot add products, but can add product families;
    Definition: The abstract factory pattern provides an interface for creating a series of related or interdependent objects without specifying their specific classes;

3. Builder mode

  • Definition: Separate the construction of a complex object from its representation, so that the same construction process can create different representations
  • Main function: The user can directly create complex objects without knowing the construction process and details of the object;
  • The user only needs to give the type and content of the specified complex object, and the builder mode is responsible for creating complex objects in order (hiding the internal construction process and details)

4. Prototype mode

5. Adapter mode

  • effect:

Guess you like

Origin blog.csdn.net/qq_40084325/article/details/113618489