工厂模式

工厂模式

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
let Basketball = function () {
this.name = 'basketball'
}
Basketball.prototype = {
getMember: function () {
console.log(5)
},
getBallSize: function () {
console.log('big')
},
}

let FootBall = function () {
this.name = 'football'
}
FootBall.prototype = {
getMember: function () {
console.log(11)
},
getBallSize: function () {
console.log('big')
},
}

let Tennis = function () {
this.name = 'tennis'
}
Tennis.prototype = {
getMember: function () {
console.log(1)
},
getBallSize: function () {
console.log('small')
},
}

let SportFactory = function (name) {
switch (name) {
case 'basketball':
return new Basketball()
case 'football':
return new FootBall()
case 'tennis':
return new Tennis()
}
}

let footBall = new SportFactory('football')
footBall.getMember()
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
// Product base attribute
interface Product {
operation(): string
}

class ConcreteProduct1 implements Product {
public operation(): string {
return 'This is Product1'
}
}
class ConcreteProduct2 implements Product {
public operation(): string {
return 'This is Product2'
}
}

abstract class Creator {
public abstract factoryMethod(): Product

public operation(): string {
const product = this.factoryMethod()

return product.operation()
}
}

class ConcreteCreator1 extends Creator {
public factoryMethod(): Product {
return new ConcreteProduct1()
}
}
class ConcreteCreator2 extends Creator {
public factoryMethod(): Product {
return new ConcreteProduct2()
}
}

// usage
function clientCode(creator: Creator) {
console.log(creator.operation())
}

clientCode(new ConcreteCreator1())
clientCode(new ConcreteCreator2())

安全工厂

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
let Factory = function (type, content) {
if (this instanceof Factory) {
// check if is instance Factory
return new this[type](content)
} else {
return new Factory(type, content)
}
}
Factory.prototype = {
Java: function (content) {
console.log(content)
},
JavaScript: function (content) {
console.log(content)
},
}

let data = [
{
type: 'Java',
content: 'This is Java',
},
{
type: 'JavaScript',
content: 'This is JavaScript',
},
]

// for test
for (let i = 0; i < data.length; i++) Factory(data[i].type, data[i].content)

抽象工厂

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
/**
* to extend superType
* @param {*} subType
* @param {*} superType
*/
let VehicleFactory = (subType, superType) => {
if (typeof VehicleFactory[superType] === 'function') {
function F() {}
F.prototype = new VehicleFactory[superType]()
subType.constuctor = subType
subType.prototype = new F()
} else {
throw new Error("don' not have this abstract class")
}
}

// create base class
VehicleFactory.Car = function () {
this.type = 'car'
}
VehicleFactory.Car.prototype = {
getPrice: function () {
return new Error('abstract')
},
getSpeed: function () {
return new Error('abstract')
},
}

let BMW = function (price, speed) {
this.price = price
this.speed = speed
}
VehicleFactory(BMW, 'Car') // interface
BMW.prototype = {
getPrice: function () {
return this.price
},
getSpeed: function () {
return this.speed
},
}

// for test
let myBmw = new BMW(10000, 100)
console.log(myBmw.getPrice())
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
// product interface
interface AbstractProductA {
functionA(): string
}

interface AbstractProductB {
/**
* base operation
* @return {string}
*/
functionA(): string

/**
* inject some operation
* @param {AbstractProductA} product
* @return {string}
*/
functionB(product: AbstractProductA): string
}

// concrete production
class ConcreteProductA1 implements AbstractProductA {
functionA(): string {
return 'This is functionA from ProductA1'
}
}
class ConcreteProductA2 implements AbstractProductA {
functionA(): string {
return 'This is functionA from ProductA2'
}
}
class ConcreteProductB1 implements AbstractProductB {
functionA(): string {
return 'This is functionA from ProductB1'
}

functionB(product: AbstractProductA): string {
return 'Function B2: ' + product.functionA()
}
}
class ConcreteProductB2 implements AbstractProductB {
functionA(): string {
return 'This is functionA from ProductB2'
}

functionB(product: AbstractProductA): string {
return 'Function B2: ' + product.functionA()
}
}

// factory interface
interface AbstractFactory {
createProductA(): AbstractProductA
createProductB(): AbstractProductB
}

// concrete factory
class ConcreteFactory1 implements AbstractFactory {
createProductA(): AbstractProductA {
return new ConcreteProductA1()
}

createProductB(): AbstractProductB {
return new ConcreteProductB1()
}
}
class ConcreteFactory2 implements AbstractFactory {
createProductA(): AbstractProductA {
return new ConcreteProductA2()
}

createProductB(): AbstractProductB {
return new ConcreteProductB2()
}
}

// usage
/**
* use with different facotry.
* @param {AbstractFactory} factory
*/
function clientCode(factory: AbstractFactory) {
const productA = factory.createProductA()
const productB = factory.createProductB()

console.log(productB.functionA())
console.log(productB.functionB(productA))
}

clientCode(new ConcreteFactory1())
clientCode(new ConcreteFactory2())