-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathfactory-method.ts
62 lines (47 loc) · 1.2 KB
/
factory-method.ts
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
// 参考: https://qiita.com/shoheiyokoyama/items/d752834a6a2e208b90ca
abstract class Product {
public abstract use (): String
}
abstract class Factory {
public create (owner: String): Product {
const product: Product = this.createProduct(owner)
this.registerProduct(product)
return product
}
protected abstract createProduct(owner: String): Product
protected abstract registerProduct(product: Product): void
}
class AccountFactory extends Factory {
private owners: String[]
constructor () {
super()
this.owners = []
}
protected createProduct (owner: String): Product {
return new Account(owner)
}
protected registerProduct (product: Product): void {
this.owners.push( (product as Account).getOwner() )
}
public getOwners(): String[] {
return this.owners
}
}
class Account extends Product {
private owner: String
constructor (owner: String) {
super()
this.owner = owner
}
public use (): String {
return `Use account: ${this.owner}`
}
public getOwner (): String {
return this.owner
}
}
export {
Factory,
AccountFactory,
Product
}