-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.ts
56 lines (46 loc) · 1.2 KB
/
index.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
// Product
interface CreditCard {
credit(value: number, onces: number): boolean
}
// Concrete Class Product (MasterCard)
class MasterCard implements CreditCard {
credit(value: number, onces: number) {
console.log(`Debiting with Master Card... | ${onces} x $ ${value / onces},00`)
return true
}
}
// Concrete Class Product (VisaCard)
class VisaCard implements CreditCard {
credit(value: number, onces: number) {
console.log(`Debiting with Visa Card... | ${onces} x $ ${value / onces},00`)
return true
}
}
// Abstract Class Creator
abstract class CreatorCard {
public abstract creatorCard(): CreditCard
credit(value: number, onces: number): string {
const card = this.creatorCard()
card.credit(value, onces)
return `${card}`
}
}
// Concrete Class MasterCard
class CreatorMasterCard extends CreatorCard {
creatorCard() {
return new MasterCard()
}
}
// Concrete Class MasterCard
class CreatorVisaCard extends CreatorCard {
creatorCard() {
return new VisaCard()
}
}
function clientCodeContext(creator: CreatorCard) {
creator.credit(200, 4)
}
export function factoryMethod() {
clientCodeContext(new CreatorMasterCard())
clientCodeContext(new CreatorVisaCard())
}