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
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
| // === 数据库访问抽象产品 ===
interface Connection {
void connect();
void disconnect();
String getConnectionInfo();
}
interface Command {
void execute(String sql);
String getDialect();
}
interface Transaction {
void begin();
void commit();
void rollback();
String getIsolationLevel();
}
// === MySQL 产品族 ===
class MySQLConnection implements Connection {
private String host;
private String database;
public MySQLConnection(String host, String database) {
this.host = host;
this.database = database;
}
@Override
public void connect() {
System.out.println("连接到 MySQL 数据库:mysql://" + host + "/" + database);
}
@Override
public void disconnect() {
System.out.println("断开 MySQL 连接");
}
@Override
public String getConnectionInfo() {
return "MySQL Connection - " + host + "/" + database;
}
}
class MySQLCommand implements Command {
@Override
public void execute(String sql) {
System.out.println("MySQL 执行:" + sql);
System.out.println("使用 MySQL 优化器处理查询");
}
@Override
public String getDialect() {
return "MySQL SQL 方言";
}
}
class MySQLTransaction implements Transaction {
@Override
public void begin() {
System.out.println("MySQL 开始事务:START TRANSACTION");
}
@Override
public void commit() {
System.out.println("MySQL 提交事务:COMMIT");
}
@Override
public void rollback() {
System.out.println("MySQL 回滚事务:ROLLBACK");
}
@Override
public String getIsolationLevel() {
return "REPEATABLE READ";
}
}
// === PostgreSQL 产品族 ===
class PostgreSQLConnection implements Connection {
private String host;
private String database;
public PostgreSQLConnection(String host, String database) {
this.host = host;
this.database = database;
}
@Override
public void connect() {
System.out.println("连接到 PostgreSQL 数据库:postgresql://" + host + "/" + database);
}
@Override
public void disconnect() {
System.out.println("断开 PostgreSQL 连接");
}
@Override
public String getConnectionInfo() {
return "PostgreSQL Connection - " + host + "/" + database;
}
}
class PostgreSQLCommand implements Command {
@Override
public void execute(String sql) {
System.out.println("PostgreSQL 执行:" + sql);
System.out.println("使用 PostgreSQL 查询规划器优化");
}
@Override
public String getDialect() {
return "PostgreSQL SQL 方言";
}
}
class PostgreSQLTransaction implements Transaction {
@Override
public void begin() {
System.out.println("PostgreSQL 开始事务:BEGIN");
}
@Override
public void commit() {
System.out.println("PostgreSQL 提交事务:COMMIT");
}
@Override
public void rollback() {
System.out.println("PostgreSQL 回滚事务:ROLLBACK");
}
@Override
public String getIsolationLevel() {
return "READ COMMITTED";
}
}
// === Oracle 产品族 ===
class OracleConnection implements Connection {
private String host;
private String database;
public OracleConnection(String host, String database) {
this.host = host;
this.database = database;
}
@Override
public void connect() {
System.out.println("连接到 Oracle 数据库:oracle://" + host + "/" + database);
}
@Override
public void disconnect() {
System.out.println("断开 Oracle 连接");
}
@Override
public String getConnectionInfo() {
return "Oracle Connection - " + host + "/" + database;
}
}
class OracleCommand implements Command {
@Override
public void execute(String sql) {
System.out.println("Oracle 执行:" + sql);
System.out.println("使用 Oracle CBO 优化器处理");
}
@Override
public String getDialect() {
return "Oracle PL/SQL 方言";
}
}
class OracleTransaction implements Transaction {
@Override
public void begin() {
System.out.println("Oracle 开始事务(自动开始)");
}
@Override
public void commit() {
System.out.println("Oracle 提交事务:COMMIT");
}
@Override
public void rollback() {
System.out.println("Oracle 回滚事务:ROLLBACK");
}
@Override
public String getIsolationLevel() {
return "READ COMMITTED";
}
}
// === 数据库工厂抽象类 ===
abstract class DatabaseFactory {
public abstract Connection createConnection();
public abstract Command createCommand();
public abstract Transaction createTransaction();
// 模板方法 - 执行数据库操作
public void performDatabaseOperations() {
Connection conn = createConnection();
Command cmd = createCommand();
Transaction tx = createTransaction();
try {
conn.connect();
tx.begin();
cmd.execute("SELECT * FROM users");
cmd.execute("UPDATE users SET last_login = NOW()");
tx.commit();
System.out.println("数据库操作成功完成");
} catch (Exception e) {
tx.rollback();
System.out.println("操作失败,已回滚");
} finally {
conn.disconnect();
}
}
// 静态工厂方法
public static DatabaseFactory getFactory(String dbType, String host, String database) {
switch (dbType.toLowerCase()) {
case "mysql":
return new MySQLFactory(host, database);
case "postgresql":
return new PostgreSQLFactory(host, database);
case "oracle":
return new OracleFactory(host, database);
default:
throw new IllegalArgumentException("不支持的数据库类型: " + dbType);
}
}
}
// === 具体数据库工厂 ===
class MySQLFactory extends DatabaseFactory {
private String host;
private String database;
public MySQLFactory(String host, String database) {
this.host = host;
this.database = database;
}
@Override
public Connection createConnection() {
return new MySQLConnection(host, database);
}
@Override
public Command createCommand() {
return new MySQLCommand();
}
@Override
public Transaction createTransaction() {
return new MySQLTransaction();
}
}
class PostgreSQLFactory extends DatabaseFactory {
private String host;
private String database;
public PostgreSQLFactory(String host, String database) {
this.host = host;
this.database = database;
}
@Override
public Connection createConnection() {
return new PostgreSQLConnection(host, database);
}
@Override
public Command createCommand() {
return new PostgreSQLCommand();
}
@Override
public Transaction createTransaction() {
return new PostgreSQLTransaction();
}
}
class OracleFactory extends DatabaseFactory {
private String host;
private String database;
public OracleFactory(String host, String database) {
this.host = host;
this.database = database;
}
@Override
public Connection createConnection() {
return new OracleConnection(host, database);
}
@Override
public Command createCommand() {
return new OracleCommand();
}
@Override
public Transaction createTransaction() {
return new OracleTransaction();
}
}
// 使用示例
public class DatabaseFactoryDemo {
public static void main(String[] args) {
String[][] databases = {
{"mysql", "localhost", "ecommerce"},
{"postgresql", "127.0.0.1", "analytics"},
{"oracle", "db-server", "enterprise"}
};
for (String[] db : databases) {
System.out.println("=== " + db[0].toUpperCase() + " 数据库操作 ===");
DatabaseFactory factory = DatabaseFactory.getFactory(db[0], db[1], db[2]);
factory.performDatabaseOperations();
System.out.println();
}
}
}
|