0%

安卓设计模式之工厂模式

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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
一、工厂模式核心思想
解决的问题:
当代码中需要根据不同条件创建不同对象时,避免到处使用 new 关键字,而是将对象创建过程封装起来。

生活类比:

普通方式:自己买面粉、糖、鸡蛋...做蛋糕(相当于直接 new 对象)

工厂模式:去蛋糕店直接点"巧克力蛋糕"(工厂帮你处理复杂创建过程)

二、三种工厂模式的Java实现
1️⃣ 简单工厂模式(静态工厂)
场景:根据类型创建不同类型的对话框

java
// 1. 定义产品接口
interface Dialog {
void show();
}

// 2. 具体产品实现
class AlertDialog implements Dialog {
@Override
public void show() {
System.out.println("显示警告对话框");
}
}

class ProgressDialog implements Dialog {
@Override
public void show() {
System.out.println("显示进度条对话框");
}
}

// 3. 简单工厂
class DialogFactory {
public static Dialog createDialog(String type) {
if ("alert".equals(type)) {
return new AlertDialog();
} else if ("progress".equals(type)) {
return new ProgressDialog();
}
return null;
}
}

// 4. 使用示例
public class Main {
public static void main(String[] args) {
Dialog dialog = DialogFactory.createDialog("alert");
if (dialog != null) {
dialog.show(); // 输出: 显示警告对话框
}
}
}
特点:

通过静态方法创建对象

新增类型需要修改工厂类

2️⃣ 工厂方法模式
场景:跨平台按钮创建(Android/iOS)

java
// 1. 抽象产品
interface Button {
void render();
}

// 2. 具体产品
class AndroidButton implements Button {
@Override
public void render() {
System.out.println("渲染Android风格按钮");
}
}

class IOSButton implements Button {
@Override
public void render() {
System.out.println("渲染iOS风格按钮");
}
}

// 3. 抽象工厂
interface ButtonFactory {
Button createButton();
}

// 4. 具体工厂
class AndroidButtonFactory implements ButtonFactory {
@Override
public Button createButton() {
return new AndroidButton();
}
}

class IOSButtonFactory implements ButtonFactory {
@Override
public Button createButton() {
return new IOSButton();
}
}

// 5. 使用示例
public class Main {
public static void main(String[] args) {
ButtonFactory factory = new AndroidButtonFactory(); // 只需切换工厂
Button button = factory.createButton();
button.render(); // 输出: 渲染Android风格按钮
}
}
特点:

每个产品对应一个工厂类

符合开闭原则(扩展时不修改原有代码)

3️⃣ 抽象工厂模式
场景:创建整套UI控件(按钮+文本框)

java
// 1. 抽象产品族
interface TextBox {
void display();
}

interface Button {
void click();
}

// 2. Android实现
class AndroidTextBox implements TextBox {
@Override
public void display() {
System.out.println("Android文本框");
}
}

class AndroidButton implements Button {
@Override
public void click() {
System.out.println("Android按钮被点击");
}
}

// 3. iOS实现
class IOSTextBox implements TextBox {
@Override
public void display() {
System.out.println("iOS文本框");
}
}

class IOSButton implements Button {
@Override
public void click() {
System.out.println("iOS按钮被点击");
}
}

// 4. 抽象工厂
interface UIFactory {
TextBox createTextBox();
Button createButton();
}

// 5. 具体工厂
class AndroidUIFactory implements UIFactory {
@Override
public TextBox createTextBox() {
return new AndroidTextBox();
}

@Override
public Button createButton() {
return new AndroidButton();
}
}

class IOSUIFactory implements UIFactory {
@Override
public TextBox createTextBox() {
return new IOSTextBox();
}

@Override
public Button createButton() {
return new IOSButton();
}
}

// 6. 使用示例
public class Main {
public static void main(String[] args) {
UIFactory factory = new AndroidUIFactory(); // 切换为IOSUIFactory即可换整套UI

TextBox textBox = factory.createTextBox();
Button button = factory.createButton();

textBox.display(); // 输出: Android文本框
button.click(); // 输出: Android按钮被点击
}
}
特点:

生产相关联的产品族

保证产品兼容性

三、Android实际应用案例
案例1:BitmapFactory
java
// 使用工厂隐藏复杂的Bitmap解码过程
Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher);
案例2:RecyclerView的ViewHolder创建
java
@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
// 根据viewType返回不同的ViewHolder(工厂方法模式)
if (viewType == TYPE_HEADER) {
return new HeaderViewHolder(inflater.inflate(R.layout.header, parent, false));
} else {
return new ItemViewHolder(inflater.inflate(R.layout.item, parent, false));
}
}

LayoutInflater.from(context)

NotificationCompat.Builder

Retrofit.create()
也是用的工厂模式
四、工厂模式对比总结
模式 优点 缺点 适用场景
简单工厂 调用简单 违反开闭原则 对象类型较少且稳定
工厂方法 扩展性强 类数量增多 需要灵活扩展(如插件系统)
抽象工厂 保证产品兼容性 复杂度高 成套产品(如UI主题/跨平台)