Java设计模式面试题解析:轻松应对面试挑战

一、单例模式
单例模式(Singleton Pattern)是一种常用的设计模式,它确保一个类只有一个实例,并提供一个访问它的全局访问点。在Java面试中,单例模式是一个常考的问题。
问题:请实现一个单例类,并解释其原理。
解答:
```java
public class Singleton {
private static Singleton instance;
private Singleton() {}
public static Singleton getInstance() {
if (instance == null) {
instance = new Singleton();
}
return instance;
}
}
```
解析:上述代码实现了单例模式,其原理是:
1. 将构造函数设置为私有,防止外部直接通过new创建对象。
2. 使用一个静态变量来保存类的唯一实例。
3. 提供一个静态方法,用于获取类的唯一实例。
二、工厂模式
工厂模式(Factory Pattern)是一种创建型设计模式,它将对象的创建过程封装起来,使对象创建与对象使用分离。在Java面试中,工厂模式也是一个常见的问题。
问题:请实现一个工厂类,用于创建不同类型的图形对象。
解答:
```java
interface Shape {
void draw();
}
class Circle implements Shape {
public void draw() {
System.out.println("画圆形");
}
}
class Rectangle implements Shape {
public void draw() {
System.out.println("画矩形");
}
}
class ShapeFactory {
public static Shape getShape(String shapeType) {
if (shapeType.equalsIgnoreCase("CIRCLE")) {
return new Circle();
} else if (shapeType.equalsIgnoreCase("RECTANGLE")) {
return new Rectangle();
}
return null;
}
}
```
解析:上述代码实现了工厂模式,其原理是:
1. 定义一个接口,用于表示图形对象。
2. 实现该接口的不同类,表示不同类型的图形对象。
3. 创建一个工厂类,根据传入的参数创建对应的图形对象。
三、策略模式
策略模式(Strategy Pattern)是一种行为型设计模式,它定义了一系列算法,并将每个算法封装起来,使它们可以互换。在Java面试中,策略模式也是一个常考的问题。
问题:请实现一个策略模式,用于计算两个数的和、差、积、商。
解答:
```java
interface Strategy {
int calculate(int num1, int num2);
}
class AddStrategy implements Strategy {
public int calculate(int num1, int num2) {
return num1 + num2;
}
}
class SubtractStrategy implements Strategy {
public int calculate(int num1, int num2) {
return num1 - num2;
}
}
class MultiplyStrategy implements Strategy {
public int calculate(int num1, int num2) {
return num1 * num2;
}
}
class DivideStrategy implements Strategy {
public int calculate(int num1, int num2) {
return num1 / num2;
}
}
class Context {
private Strategy strategy;
public void setStrategy(Strategy strategy) {
this.strategy = strategy;
}
public int executeStrategy(int num1, int num2) {
return strategy.calculate(num1, num2);
}
}
```
解析:上述代码实现了策略模式,其原理是:
1. 定义一个接口,用于表示计算策略。
2. 实现该接口的不同类,表示不同的计算策略。
3. 创建一个上下文类,用于封装策略对象,并执行策略。
四、观察者模式
观察者模式(Observer Pattern)是一种行为型设计模式,它定义了对象之间的一对多依赖关系,当一个对象的状态发生改变时,所有依赖于它的对象都将得到通知并自动更新。在Java面试中,观察者模式也是一个常见的问题。
问题:请实现一个观察者模式,用于实现一个消息推送功能。
解答:
```java
interface Observer {
void update(String message);
}
class ConcreteObserver implements Observer {
private String name;
public ConcreteObserver(String name) {
this.name = name;
}
public void update(String message) {
System.out.println(name + "收到消息:" + message);
}
}
interface Subject {
void registerObserver(Observer observer);
void removeObserver(Observer observer);
void notifyObservers(String message);
}
class ConcreteSubject implements Subject {
private List
private String message;
public void registerObserver(Observer observer) {
observers.add(observer);
}
public void removeObserver(Observer observer) {
observers.remove(observer);
}
public void notifyObservers(String message) {
for (Observer observer : observers) {
observer.update(message);
}
}
public void setMessage(String message) {
this.message = message;
}
}
```
解析:上述代码实现了观察者模式,其原理是:
1. 定义一个观察者接口,用于接收消息。
2. 定义一个主题接口,用于维护观察者列表,并通知观察者。
3. 创建具体的观察者和主题类,实现相应的功能。
总结:
本文针对Java设计模式面试题进行了深入解析,通过实例代码详细讲解了单例模式、工厂模式、策略模式和观察者模式。掌握这些设计模式,有助于你在面试中轻松应对相关问题的挑战。






