javascript 简单工厂模式

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
var Bicycle = new Interface("Bicycle",["assemble","wash","ride","repair"]);
var Lowride = function() {};
Lowride.prototype = {
    assemble: function() {
        document.write("Lowride assemble success!<br />")
    },
    wash: function() {
        document.write("Lowride wash success!<br />");
    },
    ride: function() {
        document.write("Lowride ride success!<br />");
    },
    repair: function() {
        document.write("Lowride repair success!<br />");
    }
};
var Speedstr = function() {};
Speedstr.prototype = {
    assemble: function() {
        document.write("Speedstr assemble success!<br />")
    },
    wash: function() {
        document.write("Speedstr wash success!<br />");
    },
    ride: function() {
        document.write("Speedstr ride success!<br />");
    },
    repair: function() {
        document.write("Speedstr repair success!<br />");
    }
}
var Comfortcruisor = function() {};
Comfortcruisor.prototype = {
    assemble: function() {
        document.write("Comfortcruisor assemble success!<br />")
    },
    wash: function() {
        document.write("Comfortcruisor wash success!<br />");
    },
    ride: function() {
        document.write("Comfortcruisor ride success!<br />");
    },
    repair: function() {
        document.write("Comfortcruisor repair success!<br />");
    }
}
var BicycleFactory = function() {};
BicycleFactory.prototype = {
    createBicycle: function(model) {
        var bicycle;
        switch(model){
            case "Lowride":
                bicycle = new Lowride();
                break;
            case "Speedstr":
                bicycle = new Speedstr();
                break;
            case "Comfortcruisor":
                bicycle = new Comfortcruisor();
                break;
        }
        Interface.ensureImplents(bicycle,Bicycle);
        return bicycle;
    }
}
var BicycleShop = new BicycleFactory();
var concertBicycle = BicycleShop.createBicycle("Speedstr");
concertBicycle.assemble();
concertBicycle.wash();
concertBicycle.ride();
concertBicycle.repair();

  

javascript 简单工厂模式,布布扣,bubuko.com

javascript 简单工厂模式

上一篇:JavaScript高级程序设计之作用域链


下一篇:排序算法(2) 堆排序 C++实现