码迷,mamicode.com
首页 > 编程语言 > 详细

javascript 简单工厂模式

时间:2014-05-28 19:44:07      阅读:374      评论:0      收藏:0      [点我收藏+]

标签:c   class   blog   code   java   a   

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 简单工厂模式

标签:c   class   blog   code   java   a   

原文地址:http://www.cnblogs.com/jones-c/p/3754703.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!