27 Java 设计模式进阶教程

27 Java 设计模式进阶教程

在这一节中,我们将重点学习三种常用的设计模式:装饰者模式适配器模式策略模式。我们会通过详细的解释和示例代码来帮助你理解这些模式的使用场景及实现。

装饰者模式

概述

装饰者模式(Decorator Pattern)是一种结构型设计模式,允许你在不改变对象自身的情况下,动态地给一个对象添加一些额外的职责。装饰者提供了比继承更灵活的替代方案。

适用场景

  • 需要在运行时动态地扩展对象的功能。
  • 需要对多个类使用相同的装饰功能。

实现步骤

  1. 定义一个组件接口,它是所有具体组件和装饰者的共同接口。
  2. 创建一个具体组件,实现组件接口。
  3. 创建一个装饰者抽象类,持有一个组件的引用,并实现组件接口。
  4. 创建一个或多个具体装饰者,扩展装饰者类。

示例

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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
// 组件接口
interface Coffee {
String getDescription();
double cost();
}

// 具体组件
class SimpleCoffee implements Coffee {
@Override
public String getDescription() {
return "Simple Coffee";
}

@Override
public double cost() {
return 1.00;
}
}

// 装饰者抽象类
abstract class CoffeeDecorator implements Coffee {
protected Coffee decoratedCoffee;

public CoffeeDecorator(Coffee coffee) {
this.decoratedCoffee = coffee;
}

@Override
public String getDescription() {
return decoratedCoffee.getDescription();
}

@Override
public double cost() {
return decoratedCoffee.cost();
}
}

// 具体装饰者
class MilkDecorator extends CoffeeDecorator {
public MilkDecorator(Coffee coffee) {
super(coffee);
}

@Override
public String getDescription() {
return decoratedCoffee.getDescription() + ", Milk";
}

@Override
public double cost() {
return decoratedCoffee.cost() + 0.50;
}
}

class SugarDecorator extends CoffeeDecorator {
public SugarDecorator(Coffee coffee) {
super(coffee);
}

@Override
public String getDescription() {
return decoratedCoffee.getDescription() + ", Sugar";
}

@Override
public double cost() {
return decoratedCoffee.cost() + 0.20;
}
}

// 测试代码
public class DecoratorTest {
public static void main(String[] args) {
Coffee coffee = new SimpleCoffee();
System.out.println(coffee.getDescription() + " $" + coffee.cost());

coffee = new MilkDecorator(coffee);
System.out.println(coffee.getDescription() + " $" + coffee.cost());

coffee = new SugarDecorator(coffee);
System.out.println(coffee.getDescription() + " $" + coffee.cost());
}
}

输出结果

1
2
3
Simple Coffee $1.0
Simple Coffee, Milk $1.5
Simple Coffee, Milk, Sugar $1.7

适配器模式

概述

适配器模式(Adapter Pattern)是一种结构型设计模式,用于将一个类的接口转换成客户希望的另一个接口。适配器模式使得原本由于接口不兼容而不能一起工作的类可以一起工作。

适用场景

  • 需要将一个已有的类与新的类结合使用。
  • 需要对接口进行一些适配,使其兼容不同的类。

实现步骤

  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
// 目标接口
interface Target {
void request();
}

// 源类
class Adaptee {
public void specificRequest() {
System.out.println("Calling specific request from Adaptee.");
}
}

// 适配器类
class Adapter implements Target {
private Adaptee adaptee;

public Adapter(Adaptee adaptee) {
this.adaptee = adaptee;
}

@Override
public void request() {
adaptee.specificRequest();
}
}

// 测试代码
public class AdapterTest {
public static void main(String[] args) {
Adaptee adaptee = new Adaptee();
Target adapter = new Adapter(adaptee);
adapter.request(); // Outputs: Calling specific request from Adaptee.
}
}

策略模式

概述

策略模式(Strategy Pattern)是一种行为设计模式,定义了一系列算法,将每一个算法封装起来,并使它们可互换。策略模式使得算法独立于使用它的客户端而变化。

适用场景

  • 需要在运行时选择算法的场景。
  • 有多个类只是在行为上有不同的实现。

实现步骤

  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
// 策略接口
interface Strategy {
int execute(int a, int b);
}

// 具体策略类
class AddStrategy implements Strategy {
@Override
public int execute(int a, int b) {
return a + b;
}
}

class SubtractStrategy implements Strategy {
@Override
public int execute(int a, int b) {
return a - b;
}
}

// 上下文类
class Context {
private Strategy strategy;

public Context(Strategy strategy) {
this.strategy = strategy;
}

public int executeStrategy(int a, int b) {
return strategy.execute(a, b);
}
}

// 测试代码
public class StrategyTest {
public static void main(String[] args) {
Context context = new Context(new AddStrategy());
System.out.println("10 + 5 = " + context.executeStrategy(10, 5));

context = new Context(new SubtractStrategy());
System.out.println("10 - 5 = " + context.executeStrategy(10, 5));
}
}

输出结果

1
2
10 + 5 = 15
10 - 5 = 5

总结

通过以上的示例和解释,我们了解了装饰者模式适配器模式策略模式的基本概念、适用场景及其实现方法。这些设计模式在实际开发中非常有用,能够提高代码的灵活性和可维护性。理解这些模式并熟练运用将显著提升你的编

27 Java 设计模式进阶教程

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

作者

AI教程网

发布于

2024-08-08

更新于

2024-08-10

许可协议