抽象工厂模式

  • 抽象工厂模式提供一个创建一系列相关或相互依赖对象的接口 而无须指定它们具体的类

实现

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
abstract class Database {
constructor(public username: string = 'admin', public password: string = '123456') {
this.username = username;
this.password = password;
}
public abstract getDetails(): void
public setDetails(username: string, password: string) {
console.log(username, password);
// 然后根据不同数据库执行不同的初始用户逻辑
}
}

class Sql extends Database {
constructor(username: string, password: string) {
super(username, password)
}
public getDetails() {
const sqlUserName = this.username + 'Sql'
const sqlPassWord = this.password + 'Sql'
this.setDetails(sqlUserName, sqlPassWord)
}
}

class Mysql extends Database {
constructor(username: string, password: string) {
super(username, password)
}
public getDetails() {
const mysqlUserName = this.username + 'Mysql'
const mysqlPassWord = this.password + 'Mysql'
this.setDetails(mysqlUserName, mysqlPassWord)
}
}

const ConnectSql = new Sql('aaa', 'bbb')
ConnectSql.getDetails()

const ConnectMysql = new Mysql('cccc', 'ddd')
ConnectMysql.getDetails()

参考链接