"Design Mode 4" Observation, Combination, and Flyweight Mode

1.8 Observer Mode-Observer (Important)

Define the observer interface, which can be implemented by multiple observers. Observers are often used together with the chain of responsibility. Observers can be regarded as part of the chain of responsibility.

  • General events are the observer mode used, such as keyboard events, mouse events, etc.
  • Event source object, observer

1.8.1 Spring的AOP

  • Spring's Aop can be used as an implementation of the observer pattern, has been observing this aspect

1.8.2 Keyboard monitor

  • Many events are also used in the observer mode, such as the keyboard listener, to monitor keyboard events
  • Summary: Observer or Listenner or Hook or Callback are essentially observer modes

1.9 Combination Mode-Composite

  • Commonly used as organizational structure, organizational tree, tree structure
  • For example, there are several first-level subdirectories under the root directory, and there are subdirectories under the subdirectory, doing different things

1.10 Flyweight model-Flyweight

Reuse objects, commonly used in input tools, such as word, such as your keyboard hitting the letter A. So what will happen if there is no Flyweight, every time you tap, there will be an A object generated. Flyweight mode saves A ~ Z in advance and puts it in a pool. As shared metadata

1.10.1 Connection pool, thread pool: Pool

Connection pool, thread pool, are used to enjoy the yuan model

1.10.2 String in Java

Strings in Java are placed in the string constant pool

public static void main(String[] args) {
    String s1 = "abc";
    String s2 = "abc";
    String s3 = new String("abc");
    String s4 = new String("abc");
    
    // true
    System.out.println(s1 == s2);
    // false
    System.out.println(s1 == s3);
    // false
    System.out.println(s3 == s4);
    // true
    System.out.println(s3.intern == s1);
    // true
    System.out.println(s.intern == s4.intern);
}

Explanation:
s1 == s2 is because the string constant pool already has "abc", the same
s3 == s4 is used because the new operation reallocated memory, the memory address is different, so false
s3.intern == s1 is because s3.intern refers to the constant pool reference corresponding to s3, because although s3 opens up memory, the constant pointed to will also be taken from the constant pool, and no new "abc" will be created

1.10.3 Flyweight model combined with combined model

Fly yuan's meta objects can be combined into another meta object, so it can be combined with the combination

Guess you like

Origin www.cnblogs.com/darope/p/12731911.html