Interviewer: design patterns on these issues for you to answer this question

Foreword

A few days ago a small partner told me that his interview when the interviewer asked a question related to design patterns, Jidao A feeling is not very good, could not get the offer;

Fortunately, the interviewer kindness, barely got the offer, I heard is to look at his attitude is better, give him extra points points, but not a good grasp of design patterns the problem still exists; small series is the latest finishing design video mode gave him the only solution of

Xiao Bian asked him to design patterns related issues sorted out, to share to you, for your reference

Design Patterns face questions

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) Swing and is used in many event listeners. Decorator 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. Design Patterns

Extending the availability of the code

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. Only a whole application maintenance

A 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 Java

5 You can start using 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. Simple Example

Son is a weather system, when the weather changes must be reflected in the view presented to the public. This view object

It is a subject, and the observer are different views.

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, after

You can use more advanced and higher-performance implementations to replace the original product realization or the like, which do not require any call level

He modified.

6. For a decorative pattern (decorator design pattern) Java implementation with? It is the role of the object or class hierarchy

level?

Decorator strong increase in the capacity of a single object. Java IO everywhere using decorative patterns, a typical example is the

Buffered series such as BufferedReader class and BufferedWriter, they enhance Reader and Writer objects,

In order 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 from a static context only because non-static variables are associated with a specific object instance

Rather static and was not associated with any instance.

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 it is still off

In all other cases, ATM should maintain the correct state (affairs), think about the lock (locking), affairs

(Transaction), error conditions (error condition), the boundary condition (boundary condition) and so on. in spite of

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 rewrite

(Overriding), while the heavy-duty (overloading) is doing the same thing with a different input. In Java, the overloaded side

Different method signature, 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,

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

在 Java 中,你只能继承一个类,但可以实现多个接口。所以一旦你继承了一个类,你就

失去了继承其他类的机会了。

接口通常被用来表示附属描述或行为如:Runnable、Clonable、Serializable 等等,因此当你

使用抽象类来表示行为时,你的类就不能同时是 Runnable 和 Clonable(注:这里的意思是指

如果把 Runnable 等实现为抽象类的情况),因为在 Java 中你不能继承两个类,但当你使用

接口时,你的类就可以同时拥有多个不同的行为。

在一些对时间要求比较高的应用中,倾向于使用抽象类,它会比接口稍快一点。

如果希望把一系列行为都规范在类继承层次内,并且可以更好地在同一个地方进行编码,

那么抽象类是一个更好的选择。有时,接口和抽象类可以一起使用,接口中定义函数,而

在抽象类中定义默认的实现。

小编最新整理的设计模式视频

webp


Guess you like

Origin blog.51cto.com/14544183/2456499