创建型-综合题-AbstractFactory

题目

Java设计模式练习题:抽象工厂模式 - 操作系统UI控件库

一、案例背景

在跨平台应用开发中,不同操作系统(如Windows、Mac)下的用户界面控件(如按钮、文本框)表现形式不同。为了实现程序对平台的解耦,我们使用抽象工厂模式,构建一套统一的控件创建接口,并在每个平台上实现各自的控件外观和行为。

二、任务说明

1. 请使用抽象工厂模式完成以下功能:

· 定义两个抽象产品接口:Button 和 TextField,声明 render() 方法。

· 创建两个具体产品族:WindowsButton + WindowsTextField,MacButton + MacTextField。

· 定义抽象工厂接口 GUIFactory,声明创建按钮和文本框的方法。

· 实现两个具体工厂类:WindowsFactory 和 MacFactory,用于创建各自的控件对象。

· 客户端通过传入工厂对象,创建平台相关的控件并调用 render() 方法。

三、结构图(文字表示)

[Button] ← 抽象按钮接口
[TextField] ← 抽象文本框接口
  ↑                  ↑
[WindowsButton]  [WindowsTextField]
[MacButton]      [MacTextField]

[GUIFactory] ← 抽象工厂接口
  ↑               ↑
[WindowsFactory] [MacFactory]

[Client] ← 客户端程序

blob.png

四、示例运行结果

使用 WindowsFactory:
渲染 Windows 按钮
渲染 Windows 文本框

使用 MacFactory:
渲染 Mac 按钮
渲染 Mac 文本框

五、编程提示

• 接口命名规范:Button、TextField、GUIFactory。

• 工厂接口应包含 createButton() 与 createTextField() 方法。

• 客户端通过 GUIFactory 创建控件,保持平台无关。

代码

PlantUML

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
@startuml
!theme plain

' 1. 抽象产品接口:Button
interface Button {
+ render(): void
}

' 2. 抽象产品接口:TextField
interface TextField {
+ render(): void
}

' 3. 具体产品族:Windows UI 控件
class WindowsButton {
+ render(): void
}

class WindowsTextField {
+ render(): void
}

' 4. 具体产品族:Mac UI 控件
class MacButton {
+ render(): void
}

class MacTextField {
+ render(): void
}

' 5. 抽象工厂接口:GUIFactory
interface GUIFactory {
+ createButton(): Button
+ createTextField(): TextField
}

' 6. 具体工厂类:WindowsFactory
class WindowsFactory {
+ createButton(): Button
+ createTextField(): TextField
}

' 7. 具体工厂类:MacFactory
class MacFactory {
+ createButton(): Button
+ createTextField(): TextField
}

' 8. 客户端程序:Client
class Client_2 {
+ {static} main(args: String[]): void
}

' 关系定义

' 具体产品实现抽象产品接口
Button <|.. WindowsButton
Button <|.. MacButton

TextField <|.. WindowsTextField
TextField <|.. MacTextField

' 具体工厂实现抽象工厂接口
GUIFactory <|.. WindowsFactory
GUIFactory <|.. MacFactory

' 具体工厂创建具体产品
WindowsFactory ..> WindowsButton : creates >
WindowsFactory ..> WindowsTextField : creates >

MacFactory ..> MacButton : creates >
MacFactory ..> MacTextField : creates >

' 抽象工厂与抽象产品之间的关系 (工厂方法返回抽象产品)
GUIFactory ..> Button : creates >
GUIFactory ..> TextField : creates >

' 客户端使用抽象工厂和抽象产品
Client_2 ..> GUIFactory
Client_2 ..> Button
Client_2 ..> TextField

@enduml

代码源码

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
85
86
87
88
89
90
91
92
93
94
// 1. 抽象产品接口:Button  
interface Button {
void render();
}

// 2. 抽象产品接口:TextField
interface TextField {
void render();
}

// 3. 具体产品族:Windows UI 控件
class WindowsButton implements Button {
@Override
public void render() {
System.out.println("渲染 Windows 按钮");
}
}

class WindowsTextField implements TextField {
@Override
public void render() {
System.out.println("渲染 Windows 文本框");
}
}

// 4. 具体产品族:Mac UI 控件
class MacButton implements Button {
@Override
public void render() {
System.out.println("渲染 Mac 按钮");
}
}

class MacTextField implements TextField {
@Override
public void render() {
System.out.println("渲染 Mac 文本框");
}
}

// 5. 抽象工厂接口:GUIFactory
interface GUIFactory {
Button createButton();
TextField createTextField();
}

// 6. 具体工厂类:WindowsFactory
class WindowsFactory implements GUIFactory {
@Override
public Button createButton() {
return new WindowsButton();
}

@Override
public TextField createTextField() {
return new WindowsTextField();
}
}

// 7. 具体工厂类:MacFactory
class MacFactory implements GUIFactory {
@Override
public Button createButton() {
return new MacButton();
}

@Override
public TextField createTextField() {
return new MacTextField();
}
}

// 8. 客户端程序:Client
public class Client_2 {
public static void main(String[] args) {
System.out.println("--- 使用 WindowsFactory ---");

GUIFactory windowsFactory = new WindowsFactory();
Button windowsButton = windowsFactory.createButton();
TextField windowsTextField = windowsFactory.createTextField();

windowsButton.render();
windowsTextField.render();

System.out.println("\n--- 使用 MacFactory ---");

GUIFactory macFactory = new MacFactory();
Button macButton = macFactory.createButton();
TextField macTextField = macFactory.createTextField();

macButton.render();
macTextField.render();
}
}