Design patterns frequently asked in Java interviews

Design patterns often asked in interviews

What is a design pattern?

​ Design patterns are the attempts and testing methods used by various programmers in the world to solve specific design problems. Design patterns are an extension of code availability

Singleton mode

​ Guaranteed to be created once, saving system overhead

​ Hungry Chinese style: No matter whether there is an object or not, a new object must be created directly.

​ Lazy man style: First determine whether an object has been created. If an object is created, use the originally created object, if not, create a new object.

​ 1) The singleton mode focuses on sharing some objects that consume more resources when created on the entire system. Only one specific class instance is maintained in the entire application, which is used by all components. Java.lang.RuntimeIt is a classic example of the singleton pattern. Since Java 5 you can useEnumTo achieve thread-safe singleton. (According to the test, the use of enumeration is the most perfect way to write singleton mode)

Observer mode

​ Defines one-to-many dependencies between objects, so that when an object changes, all its dependents will be notified and automatically updated.

​ 1) The observer mode is based on the object's state changes and the observer's communication, so that they can make corresponding operations. A simple example is a weather system. When the weather changes, it must be reflected in the view displayed to the public. This view object is a subject, and the different views are observers.

​ 2) An example of a child crying given by Teacher Ma Yingjun. Several observers (father, mother, puppy) observe the child. When the child starts crying, different observers respond in different ways. The father feeds the child milk powder. The mother hugged the child and the puppy barked.

Factory mode

Decoupling code.

​ 1) The biggest advantage of the factory pattern is to increase the packaging level when creating objects. If you use factories to create objects, you can later replace the original product implementations or classes with more advanced and higher-performance implementations, without any modification at the call level.
2) The following is an example of an ordinary factory:

1. Create a public interface

		/**
		 * 公共的发送接口
		 */
			public interface Sender {
    
    
			    public void send();
			}

2. Create two implementation classes

/**
 * 通过QQ发送
 */
public class sendQQ implements Sender {
    
    
    @Override
    public void send() {
    
    
        System.out.println("使用QQ开始发送消息...");
    }
}
/**
 1. 通过微信发送
 */
public class sendWeChat implements Sender {
    
    
    @Override
    public void send() {
    
    
        System.out.println("使用微信开始发消息...");
    }
}
  1. Create a factory
/**
 1. 发送信息工厂
 */
public class SendFactory {
    
    
    public Sender produce(String type){
    
    
        if("QQ".equals(type)){
    
    
            return new sendQQ();
        }else if ("WeChat".equals(type)){
    
    
            return new sendWeChat();
        }else{
    
    
            System.out.println("输入类型有误!");
            return null;
        }
    }
}
  1. Use factory test
 public class FactoryTest {
    
    
    public static void main(String[] args) {
    
    
        SendFactory sendFactory = new SendFactory();
        Sender qq = sendFactory.produce("WeChat");
        qq.send();

    }
}

Factory pattern classification

​ 1) Simple factory: It is used to produce any product in the same grade structure, and there is nothing it can do to add new products.

​ 2) Factory method: used to produce fixed products in the same hierarchical structure and support the addition of arbitrary products.

​ 3) Abstract Factory: It is used to produce all products of different product families, and cannot do anything to add new products; it supports the addition of product families

Decorator pattern

​ The decoration mode increases the ability of a single object. The decoration pattern is used everywhere in Java IO. A typical example is the Buffered series of classes such as BufferedReaderand BufferedWriter. They enhance the Readerand Writerobjects to achieve Buffer-level reading and writing with improved performance.

Reference source: https://zhuanlan.zhihu.com/p/94767927

To be continued...

Guess you like

Origin blog.csdn.net/weixin_45496190/article/details/106463942