Design Patterns interview topics

1. Please list the common design pattern in several JDK?

Singleton (Singleton pattern) for Runtime, Calendar, and some other classes. Factory pattern (Factory pattern) are used in various categories such as immutable Boolean, as Boolean.valueOf, the observer pattern (Observer pattern) and are used for many Swing event listeners in. Decorative design pattern (Decorator design pattern) is used for a plurality of Java IO classes.

2. What is a design pattern? Are you using your code inside any design pattern?

Design patterns are tried and tested method of solving specific design problems in the world for a wide variety of programmers. The code pattern is designed to extend the availability of

3.Java in what Singleton design pattern? Please singleton pattern in Java to write thread-safe

Singleton focused on the more resource-intensive objects when share some created on the entire system. Across the application maintains only one specific instance of a class, which is commonly used for all components. Java.lang.Runtime is the classic example of the single-mode embodiment. From the beginning you can use Java 5 enumeration (enum) to achieve thread-safe singleton.

4. In Java, what is the observer design pattern (observer design 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. In Java, why not allow access to non-static variables from static method?

Java can not access non-static data simply because non-static variables are associated with a specific object instance, and still has not associated with any instance from a static context.

8. Design 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.

9. 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.

10. illustration will be more inclined to use abstract classes instead of interfaces 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 74 original articles · won praise 233 · views 20000 +

Guess you like

Origin blog.csdn.net/qq_43107323/article/details/104833504