2020 latest JAVA advanced essential design pattern interview questions (quality edition)

The latest JAVA basic advanced interview questions in 2020 (quality edition)

1. Please list several commonly used design patterns in JDK?

Singleton pattern (Singleton pattern) is used in Runtime, Calendar and some other classes. Factory pattern is used in various immutable classes such as Boolean, like Boolean.valueOf, Observer pattern is used in Swing and many event listeners. Decorator design pattern (Decorator design pattern) is used in multiple Java IO classes.

2. What is a design pattern? Have you used any design patterns in your code?

Design patterns are the attempts and testing methods used by various programmers in the world to solve specific design problems. The design pattern is

Extension of code availability

3. What is the singleton design pattern in Java? Please write a thread-safe singleton pattern in Java

The singleton mode focuses on sharing some objects that consume more resources when created on the entire system. Only one feature is maintained in the entire application

Classify instance, it is used by all components. Java.lang.Runtime is a classic example of the singleton pattern. Starting with Java 5, you can use enums to implement thread-safe singletons.

4. In Java, what is the observer design pattern?

The observer mode is based on the object's state changes and the observer's communication, so that they can make corresponding operations. Simple example

It is a weather system, and when the weather changes, it must be reflected in the view displayed to the public. This view object is a

A subject, and a different view is the observer.

5. What are the main benefits of using the factory model? Where is it used?

The biggest benefit of the factory pattern is to increase the packaging level when creating objects. If you use factories to create objects, then you

You can use more advanced and higher-performance implementations to replace the original product implementation or class, which does not require any modification at the call level

change.

6. Give a decorator design pattern implemented in Java? Does it act on the object level or the class level?

 The decoration mode increases the ability of a single object. The decoration pattern is used everywhere in Java IO. Typical examples are the Buffered series of classes such as BufferedReader and BufferedWriter, which enhance the Reader and Writer objects to achieve improved performance of Buffer-level reading and writing.

7. In Java, why is it not allowed to access non-static variables from static methods?

Non-static data cannot be accessed from a static context in Java because non-static variables are associated with specific object instances, while static variables are not associated with any instances.

8. To design an ATM machine, please tell us your design ideas?

For example, when designing financial systems, you must know that they should be able to work normally under any circumstances. Whether it’s a power failure or

In other cases, ATM should maintain the correct state (transaction), think about locking, transaction,

Error conditions, boundary conditions, etc. Although you can't think of a specific design, it would be great if you can point out non-functional requirements, ask some questions, and think about boundary conditions.

9. In Java, when do you use overloading and when do you use rewriting?

If you see that different implementations of a class have different ways to do the same thing, then you should use rewrite

(Overriding), and overloading (overloading) is to use different inputs to do the same thing. In Java, overloaded method signatures are different, while overriding is not.

10. Give examples to illustrate when you would prefer to use abstract classes instead of interfaces?

Both interfaces and abstract classes follow the design principle of "interface-oriented rather than implementation coding", which can increase code flexibility and

To adapt to changing needs. Here are a few points to help you answer this question:

In Java, you can only inherit one class, but you can implement multiple interfaces. So once you inherit a class, you lose

Go to the opportunity to inherit other classes. Interfaces are usually used to represent subsidiary descriptions or behaviors such as: Runnable, Clonable, Serializable, etc., so when you use abstract classes to represent behavior, your class cannot be Runnable and Clonable at the same time (Note: This means if Implementing Runnable as an abstract class), because in Java you cannot inherit two classes, but when you use interfaces, your class can have multiple different behaviors at the same time. In some time-critical applications, abstract classes tend to be used, which are slightly faster than interfaces.

If you want to standardize a series of behaviors in the class inheritance hierarchy, and can better code in the same place, then

What an abstract class is a better choice. Sometimes, interfaces and abstract classes can be used together, functions are defined in the interface, and the abstract

The default implementation is defined in the image class.

 

Guess you like

Origin blog.csdn.net/yunchan2675/article/details/108239949