单例、工厂、观察者模式

单例、工厂、观察者模式

1. 单例模式

1.1 概述

单例模式是一种创建型设计模式,确保一个类只有一个实例,并提供一个全局访问点。适用于需要控制资源的情况,比如配置类、连接池等。

1.2 关键实现

有多种实现单例模式的方法,下面是最常见的几种:

1.2.1 饿汉式单例

在类加载时就实例化对象,线程安全。

1
2
3
4
5
6
7
8
9
public class Singleton {
private static final Singleton INSTANCE = new Singleton();

private Singleton() {} // 私有构造函数

public static Singleton getInstance() {
return INSTANCE;
}
}

1.2.2 懒汉式单例

在第一次调用时才创建实例,需处理线程安全。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
public class Singleton {
private static Singleton instance;

private Singleton() {}

public static Singleton getInstance() {
if (instance == null) {
synchronized (Singleton.class) {
if (instance == null) {
instance = new Singleton();
}
}
}
return instance;
}
}

1.3 总结

单例模式能有效控制实例化,保证唯一性。要注意线程安全及性能。


2. 工厂模式

2.1 概述

工厂模式是一种创建型设计模式,提供一个创建对象的接口,而不直接暴露对象的创建逻辑,允许子类决定实例化哪一个类。

2.2 工厂方法模式

定义一个用于创建对象的接口,让子类决定实例化哪一个类。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
// 产品接口
public interface Product {
void use();
}

// 具体产品
public class ConcreteProductA implements Product {
public void use() {
System.out.println("使用产品A");
}
}

public class ConcreteProductB implements Product {
public void use() {
System.out.println("使用产品B");
}
}

// 工厂接口
public interface ProductFactory {
Product createProduct();
}

// 具体工厂
public class ConcreteFactoryA implements ProductFactory {
public Product createProduct() {
return new ConcreteProductA();
}
}

public class ConcreteFactoryB implements ProductFactory {
public Product createProduct() {
return new ConcreteProductB();
}
}

2.3 总结

工厂模式帮助解耦,增加代码的扩展性。可以使用工厂方法模式来便于创建复杂对象。


3. 观察者模式

3.1 概述

观察者模式是一种行为型设计模式,定义了一种一对多的依赖关系,允许多个观察者同时监听某一个主题对象。主题对象在状态发生变化时通知所有观察者。

3.2 实现步骤

  1. 定义主题接口
  2. 定义观察者接口
  3. 实现具体主题和观察者
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
// 主题接口
public interface Subject {
void registerObserver(Observer observer);
void removeObserver(Observer observer);
void notifyObservers();
}

// 观察者接口
public interface Observer {
void update(String message);
}

// 具体主题
import java.util.ArrayList;
import java.util.List;

public class ConcreteSubject implements Subject {
private List<Observer> observers = new ArrayList<>();
private String state;

public void setState(String state) {
this.state = state;
notifyObservers();
}

public String getState() {
return state;
}

public void registerObserver(Observer observer) {
observers.add(observer);
}

public void removeObserver(Observer observer) {
observers.remove(observer);
}

public void notifyObservers() {
for (Observer observer : observers) {
observer.update(state);
}
}
}

// 具体观察者
public class ConcreteObserver implements Observer {
private String name;

public ConcreteObserver(String name) {
this.name = name;
}

public void update(String message) {
System.out.println(name + " 收到消息: " + message);
}
}

3.3 使用示例

1
2
3
4
5
6
7
8
9
10
11
12
public class Main {
public static void main(String[] args) {
ConcreteSubject subject = new ConcreteSubject();
ConcreteObserver observer1 = new ConcreteObserver("观察者1");
ConcreteObserver observer2 = new ConcreteObserver("观察者2");

subject.registerObserver(observer1);
subject.registerObserver(observer2);

subject.setState("状态改变了!");
}
}

3.4 总结

观察者模式适用于事件驱动的设计架构,如 GUI 和消息系统。实现了低耦合,可以灵活添加和移除观察者。

单例、工厂、观察者模式

https://zglg.work/java-one/26/

作者

AI教程网

发布于

2024-08-08

更新于

2024-08-10

许可协议