"One day knowledge," succinctly and design patterns interview topics

Preface:

Classification of design patterns

Design patterns into three categories

Creational design patterns
and object creation related singleton, factory method pattern, abstract factory pattern, the builder pattern, prototype model including

Structural design patterns
structural design pattern is to solve the problem of the coupling between modules from the structure of the program, including an adapter mode, proxy mode, decorative patterns, appearance mode, bridge mode, and a combination pattern Flyweight

Behavioral design patterns
how to deal with the major classes or objects interact and how to allocate responsibilities, including strategic model, the template method pattern, observer pattern, iterative mode, the responsibility chain mode, command mode, the memo mode state mode, the visitor pattern, intermediary mode, the parser mode

"One day knowledge," succinctly and design patterns interview topics

Six principles of design patterns

a single responsibility principle: The reason is a class, it should be only one cause it change
a class to do one thing, to avoid too much responsibility. For example, this situation is not good, in a Activity in both the bean file, there http request, as well as adapter and so on, which led to a thing we need to modify any time will lead to changes in the Activity, so that the reason there are more changes caused by it, does not comply with the principle of single responsibility

b Open Closed Principle: classes, modules, functions should be able to expand, but can not be modified
for extension is open and is closed for modification. As far as possible in the face of changing demands, our code can remain relatively stable, scalable way to respond to changes through, rather than modifying the original code implementation

. c Richter substitution principle: all references to place the base class, the object must be transparent when its subclasses
Richter substitution principle is one important way to achieve open closed principle, we know where the use of the base class can be used subclasses to achieve, because all of the sub-class has a base class, the base class type used in programming to try to define the object, determination sub-class type at run time.

d. 依赖倒置原则:高层模块不应该依赖于底层模块,两者都应该依赖于抽象,抽象不应该依赖于细节,细节应该依赖于抽象
依赖倒置原则针对的是模块之间的依赖关系,高层模块指调用端,底层模块指具体的实现类,抽象指接口或抽象类,细节就是实现类。该原则的具体表现就是模块间的依赖通过抽象发生,直线类之间不发生直接依赖关系,依赖通过接口或抽象类产生,降低耦合,比如 MVP 模式下,View 层和 P 层通过接口产生依赖关系

e. 迪米特原则(最少知识原则):一个软件实体应该尽可能少的与其他实体发生相互作用
迪米特原则要求我们在设计系统时,尽量减少对象之间的交互

f. 接口隔离原则:一个类对另一个类的依赖应该建立在最小的接口上
接口隔离原则的关键是接口以及这个接口要小,如何小呢,也就是我们要为专门的类创建专门的接口,这个接口只对它有效,不要试图让一个接口包罗万象,要建立最小的依赖关系

1. 请列举出在 JDK 中几个常用的设计模式?

单例模式(Singleton pattern)用于 Runtime,Calendar 和其他的一些类中。工厂模式(Factory pattern)被用于各种不可变的类如 Boolean,像 Boolean.valueOf,观察者模式(Observer pattern)被用于 Swing 和很多的事件监听中。装饰器设计模式(Decorator design pattern)被用于多个 Java IO 类中。

2. 什么是设计模式?你是否在你的代码里面使用过任 何设计模式?

设计模式是世界上各种各样程序员用来解决特定设计问题的尝试和测试的方法。设计模式是代码可用性的延伸

3.Java 中什么叫单例设计模式?请用 Java 写出线程安全的单例模式

单例模式重点在于在整个系统上共享一些创建时较耗资源的对象。整个应用中只维护一个特定类实例,它被所有组件共同使用。Java.lang.Runtime 是单例模式的经典例子。从 Java5 开始你可以使用枚举(enum)来实现线程安全的单例。

4. 在 Java 中,什么叫观察者设计模式(observer design pattern )?

观察者模式是基于对象的状态变化和观察者的通讯,以便他们作出相应的操作。简单的例
子就是一个天气系统,当天气变化时必须在展示给公众的视图中进行反映。这个视图对象是一个主体,而不同的视图是观察者。

5. 使用工厂模式最主要的好处是什么?在哪里使用?

工厂模式的最大好处是增加了创建对象时的封装层次。如果你使用工厂来创建对象,之后你可以使用更高级和更高性能的实现来替换原始的产品实现或类,这不需要在调用层做任何修改。

6. 举一个用 Java 实现的装饰模式(decorator design pattern) ?它是作用于对象层次还是类层次?

装饰模式增加强了单个对象的能力。Java IO 到处都使用了装饰模式,典型例子就是Buffered 系列类如 BufferedReader 和BufferedWriter,它们增强了 Reader 和 Writer 对象,以实现提升性能的 Buffer 层次的读取和写入。

7. 在 Java 中,为什么不允许从静态方法中访问非静态变量?

Java 中不能从静态上下文访问非静态数据只是因为非静态变量是跟具体的对象实例关联的,而静态的却没有和任何实例关联。

8. 设计一个 ATM 机,请说出你的设计思路?

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.

Guess you like

Origin blog.51cto.com/14541311/2446522