1. What design patterns do you know?

Just pick a few common or used ones from each of the three categories below.

What is the difference between the factory method pattern and the Abstract factory pattern?

Factory method pattern:

An abstract product class can be derived from multiple concrete product classes. An abstract factory class can be derived from multiple concrete factory classes. Only one instance of a concrete product class can be created per concrete factory class.

Abstract Factory Pattern:

Multiple abstract product classes, each of which can be derived from multiple concrete product classes. An abstract factory class can be derived from multiple concrete factory classes. Each concrete factory class can create multiple instances of a concrete product class.

The difference between:Copy the code

The factory method pattern has only one abstract product class, whereas the Abstract factory pattern has multiple. The concrete factory class of the Factory method pattern can create only one instance of a concrete product class, whereas the abstract factory pattern can create multiple instances.

3. What design patterns are used in the JDK? Almost every design pattern has been used in JDK source code. Here are some common ones:

Abstract Factory patternCopy the code

1.javax.xml.parsers.DocumentBuilderFactory#newInstance()

2. javax.xml.transform.TransformerFactory#newInstance()

Builder modelCopy the code

1.java.lang.StringBuilder#append() 2. java.lang.StringBuffer#append()

The prototype patternCopy the code
  1. java.lang.Object#clone()

    Adapter mode

    1.java.util.Arrays#asList() 2.java.util.Collections#list()

    Decorator mode

    1. A subclass of IO streams

    2. java.util.Collections#synchronizedXXX()

    The flyweight pattern

  2. java.lang.Integer#valueOf(int)

    The proxy pattern

  3. java.lang.reflect.Proxy

  4. javax.inject.Inject

Chain of Responsibility model

  1. java.util.logging.Logger#log()

  2. javax.servlet.Filter#doFilter()

4. What design patterns are used in Spring?

  1. Singleton design pattern: Beans in Spring are singleton by default;

  2. Proxy design pattern: Implementation of Spring AOP functionality;

  3. Factory design pattern: Spring uses the factory pattern to create Bean objects from the BeanFactory, ApplicationContext;

  4. Template method pattern: Spring’s jdbcTemplate, hibernateTemplate, and other classes that end in Template for database operations use the Template pattern.

  5. Decorator design pattern: Our project needs to connect to multiple databases, and different customers need to access different databases on each visit. This mode allows us to dynamically switch between different data sources based on customer needs;

  6. The Observer pattern: The Spring event-driven model is a classic application of the Observer pattern;

  7. Adapter pattern: The adapter pattern is used in Spring AOP enhancements or Advice, and is used in SpringMVC to adapt controllers.

5. What are the six principles of design patterns?

  1. Single responsibility principle: a method and a class are responsible for only one responsibility, and changes to each responsibility do not affect other programs.

  2. Open closed principle: Open for extensions, closed for modifications. That is, extending functionality without modifying one software entity.

  3. Richter’s substitution principle: in a software system, a place that can accept a base class object must accept a subclass object.

  4. Dependency inversion principle: Programming for interfaces that rely on abstractions rather than concrete ones.

  5. Interface isolation principle: Replace one unified interface with multiple isolated interfaces. Reduce coupling between classes.

  6. Demeter principle: an entity should interact with other entities as little as possible, so that the functional modules of the system are relatively independent.

  7. Advantages and disadvantages of the singleton pattern?

    Advantages:

Since only one object exists in system memory, system resources can be saved, and the singleton pattern can undoubtedly improve system performance for some objects that need to be created and destroyed frequently.

Disadvantages:Copy the code

Because there is no layer of abstraction in the singleton pattern, it is difficult to extend the singleton class. Abuse of singletons will bring some negative problems, for example, in order to save resources, the database connection pool object is designed as a singleton class, may lead to too many programs sharing the connection pool object and the connection pool overflow; If an instantiated object is not used for a long time, the system considers it garbage and collects it, resulting in a loss of object state.

8. Please write down the pattern of single example.

  1. Lazy: Create it when you need it

public class Singleton {

private static Singleton instance;

private Singleton(){};

public static synchronized Singleton getInstance(){

if(instance == null){

    instance = new Singleton();    

}      
Copy the code

return instance;

}

}

  1. Hungry: create at initialization and return when used

public class Singleton {

private static Singleton instance = new Singleton();

private Singleton(){};

public static Singleton getInstance(){

return instance;

}

}

  1. Static inner class

public class Singleton {

private static class SingletonHolder{

private static final Singleton INSTTANCE = new Singleton();

}

private Singleton(){};

public static final Singleton getInstance(){

return SingletonHolder.INSTTANCE;

}

}

  1. Double check lock

public class Singleton {

private volatile static Singleton singleton;

private Singleton(){};

public static Singleton getSingleton(){

if(singleton == null){

synchronized(Singleton.class){ if(singleton == null){ singleton = new Singleton(); }}Copy the code

}

return singleton;

}

}

9. Which design pattern does the tree file directory use?

The combination model is adopted. Trees are ubiquitous in software, for example: Directory structure in the operating system and application software, office system in the company organization structure and so on in the menu, how to use object-oriented way to deal with this kind of tree structure is portfolio model needs to solve problems, portfolio model by a clever design scheme allows the user to process the entire consistently tree structure or part of the tree structure, Leaf nodes (nodes that do not contain children) and container nodes (nodes that contain children) in a tree structure can also be handled consistently.