js原生设计模式——13桥接模式(相同业务逻辑抽象化处理的职责链模式)

桥接模式之多元化类之间的实例化调用实例

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>桥接模式之多元化类之间的实例化调用</title>
    <script type="text/javascript">
    //多维类的声明和调用(有点像类职责链模式)
    //这些功能类也可看作是功能模块的抽象层
    //运动类模块
    var Speed = function(x,y){
        this.x = x;
        this.y = y;
    }
    Speed.prototype.run = function(){
        console.log('跑起来');
    }
    //着色类模块
    function Color(cl){
        this.color = cl;
    }
    Color.prototype.draw = function(){
        console.log('绘制颜色');
    }
    //变形类模块
    function Shape(sp){
        this.shape = sp;
    }
    Shape.prototype.change = function(){
        console.log('改变形状');
    }
    //说话类模块
    function Speek(wd){
        this.word = wd;
    }
    Speek.prototype.say = function(){
        console.log('我可以说话');
    }
    //具体实现层的几个类,在功能上调用上面的抽象类
    //接下来我们想创建一个球类,可以运动,可以着色
    function Ball(x,y,c){
        this.speed = new Speed(x,y);//实现运动单元
        this.color = new Color(c);//实现着色单元
    }
    Ball.prototype.init = function(){
        this.speed.run();  //实现运动方法
        this.color.draw(); //实现着色方法
    }
    //同样我们想创造一个人类,他可以运动可以说话
    function Person(x,y,f){
        this.speed = new Speed(x,y);
        this.speek = new Speek(f);
    }
    Person.prototype.init = function(){
        this.speed.run();
        this.speek.say();
    }
    //最后我们想创造一个精灵类,他可以运动可以说话可以着色可以变形
    function Spirite(x,y,c,f,s){
        this.speed = new Speed(x,y);
        this.speek = new Speek(f);
        this.color = new Color(c);
        this.shape = new Shape(s);
    }
    Spirite.prototype.init = function(){
        this.speed.run();
        this.speek.say();
        this.color.draw();
        this.shape.change();
    }
    //测试用例
    var p = new Person(10,12,16);
    p.init();
    //本例已经通过验证
    </script>
</head>
<body>
</body>
</html>