The Java design patterns and related interview questions

A. Because of the design model is too complicated, there is no organize themselves. Can learn about the two articles.
23 kinds of design patterns summary of JAVA design patterns
23 kinds of design patterns Insights

Overall, the design patterns into three categories:
to create a schema, a total of five categories: factory method pattern, abstract factory pattern, singleton, the builder pattern, prototype model.
Structural model, a total of seven kinds: adapter mode, decorative mode, proxy mode, the appearance mode, bridge mode, combination mode, Flyweight.
Behavioral patterns, a total of eleven kinds:
Strategy pattern, the template method pattern, observer mode, iterator pattern, the responsibility chain mode, command mode, the memo mode state mode, the visitor pattern, intermediary model to explain the mode

II. Common interview questions
1. Please list the common design pattern in several JDK?
Singleton pattern for Runtime, Calendar, and some other classes. Factory pattern is used in various categories such as immutable Boolean, like Boolean.valueOf method. The observer pattern is used to swing and a lot of time listening in. Decorator java IO is a plurality of classes.

4. Explain what the observer pattern?
The observer pattern is based on the object's state changes and communications observer, so that they can make the appropriate action. The simplest example is a weather system that must be reflected in the view presented to the public in when the weather changes. This view object is a subject, and different views are observers.

5. What are the benefits of using the factory pattern is the most important? Where to use?
The greatest benefit is to increase the factory pattern package level when the object is created.
If you use the factory to create an object, then you can replace the original product realization or the like to use more advanced and higher-performance implementations, which do not need to make any changes in the call level.

6. For a decorative pattern (decorator design pattern) Java implementation with? It is acting on the object level or class hierarchy?
Decorator strong increase in the capacity of a single object. Java IO everywhere using decorative patterns, a typical example is the series of classes such as Buffered BufferedReader and BufferedWriter, they enhance Reader and Writer objects to achieve the level of performance improvement Buffer read and write.

7. Design of an ATM machine, please state your design ideas.
For example, financial systems design, must know that they should all be able to work in any case. Whether power failure or other circumstances, ATM should maintain the correct state (affairs), think about the lock (locking), transaction (transaction), error conditions (error condition), the boundary condition (boundary condition) and so on. Although you can not think of a specific design, but if you can point out non-functional requirements, ask some questions, think about the boundary conditions, these will be very good.

8. In Java, when a heavy load, when to use rewrite?
If you see different implementations of a class have different ways to do the same thing, then it should be rather heavy load (overloading) is doing the same thing with a rewrite (overriding), with different input. In Java, method overloading of different signatures, but not rewritten.

9. illustrate be more inclined to use an abstract class instead of an interface under what circumstances?
Interfaces and abstract classes follow the "realization of interfaces rather than coding" design principle, it can increase the flexibility of the code can be adapted to changing needs. Here are a few points that can help you answer this question:

In Java, you can only inherit a class, but can implement multiple interfaces. So once you inherit a class, you will lose the chance to inherit the other classes. Interface is typically used to represent or describe the behavior of such subsidiary: Runnable, Clonable, Serializable etc., so when you use an abstract class to represent behavior, you can not simultaneously Runnable class and Clonables (Note: If it is meant herein refers to the Runnable etc. implemented as a case of abstract class), in Java because you can not inherit from two classes, but when you use the interface, your class can have a number of different behaviors at the same time. In some of the time high demand applications tend to use abstract class, it will be slightly faster than the interface. If you want to conduct a series of specifications are within the class inheritance hierarchy and can better be encoded in the same place, then the abstract class is a better choice. Sometimes, abstract classes and interfaces can be used together, an interface function is defined, the default implementation is defined in an abstract class.

Published 99 original articles · won praise 2 · Views 2621

Guess you like

Origin blog.csdn.net/weixin_41588751/article/details/105206194